Answered Encoder Reading From A Single LEGO NXT Motor (w/ New NXT (v2) Drivers)

  • Friday, October 12, 2007 8:58 AM
     
     
    Hi, I'm having problems subscribing to the encoder of a single motor in NXT.

    Using drive = Microsoft.Robotics.Services.Sample.Lego.Nxt.Drive.Proxy, I was able to access the encoder reading of both motors through:

    Code Block

    drive.DriveOperations driveNotificationPort = new drive.DriveOperations();
    _driveA.Subscribe(driveNotificationPort);
    Activate(
        Arbiter.Receive<drive.DriveEncodersUpdate>(

            true,

            driveNotificationPort,

            EncoderHandler

        )

    );


    According to my understanding, I can do this because drive.DriveOperations can post drive.DriverEncodersUpdate items.

    With Microsoft.Robotics.Services.Sample.Lego.Nxt.Motor.Proxy, however, there's no MotorEncoderUpdate to receive and the MotorOperations does not post any encoder update items.

    If I want to read from only one motor encoder, my current solution would be to wrap it anway in a drive. But I'd really like to know a cleaner way to do this.

    Thank you.

All Replies

  • Saturday, October 13, 2007 6:59 PM
     
     Answered

    Hi echoseven,

     

    The Motor service implements the encoder alternate contract.  This means that when you start the motor service, you can partner with the encoder and receive notifications from the motor. 

     

    One way to discover this is to use VPL.  Drop an NXT brick, a motor, and a simple dialog on the page, then subscribe to the UpdateTickCount from the motor and send it to the dialog.  Next, choose Menu / Build / Compile as a service, and take a look at the code which is generated:

     

    Code Block

    using encoder = Microsoft.Robotics.Services.Encoder.Proxy;

     

     

    Code Block

    // Partner: NxtMotor, Contract: http://schemas.microsoft.com/robotics/2006/05/encoder.html

    [dssa.Partner("NxtMotor", Contract = encoder.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]

    encoder.EncoderOperations _nxtMotorPort = new encoder.EncoderOperations();

    encoder.EncoderOperations _nxtMotorNotify = new encoder.EncoderOperations();

     

     

    ...

     

    Code Block

            protected override void Start()

            {

                // If there was no initial state partner, then the service state will be null.

                if (_state == null)

                {

                    // The state MUST be created before the service starts processing messages.

                    _state = new DiagramState();

                }

     

                // Subscribe to partners

                _nxtMotorPort.Subscribe(_nxtMotorNotify);

     

                base.Start();

     

                // Add notifications to the main interleave

                base.MainPortInterleave.CombineWith(

                    new ccr.Interleave(

                        new ccr.ExclusiveReceiverGroup(

                        ),

                        new ccr.ConcurrentReceiverGroup(

                            ccr.Arbiter.ReceiveWithIterator<ENCODER.UpdateTickCount>(true, _nxtMotorNotify, NxtMotorUpdateTickCountHandler)

                        )

                    )

                );

            }

     

     

     

    Also, don't forget to check out the dll's which are referenced:

    • NxtBrick.Y2007.M07.Proxy
    • NxtCommon.Y2007.M07
    • RoboticsCommon.Proxy