nMotorEncoder vs SensorValue

Which should I use for IMEs? I didn’t know about the former when I began programming our team’s robot, so now the reference is everywhere in our functions. However, we need a way to reset motor encoders, and I’m not sure if SensorValue] can do so.

Is there a major advantage to nMotorEncoder]?

When you are using the Integrated Motor Encoders, you will need to use the nMotorEncoder command; when you are using the Quadrature Shaft encoders, you will need to use the sensorValue command.

The reason for this is essentially boils down to how the sensors work. The IME’s are attached directly to the motor and (assuming they are setup in the Motors and Sensors Setup for your motor’s internal gearing configuration) will return the same number of encoder counts per rotation regardless of what the motor ends up driving. If you change the external gearing or size of a wheel that an IME-enabled motor is driving, for instance, the IME will still return the same amount of encoder counts per revolution. Since the IME’s can only be attached to the endcap of a motor and nowhere else on the robot, they use the nMotorEncoder command.

When you are accessing a Quadrature Encoder, that value may change. One rotation of the motor’s output shaft may return 360 encoder counts (if attached directly to the motor’s output shaft), or it may return a greater or lesser value, depending on the external gearing of the robot’s drivetrain (or whatever part of the robot the Quadrature Encoder is attached to). Quadrature Encoders are attached to the Cortex’s Digital Sensors ports and are treated as a sensor; thus, they use the sensorValue command.

There’s a lot more to it in the behind-the-scenes header files in ROBOTC, and really it boils down to how the encoder data is passed to the Cortex, the range of values per revolution of the motor that each encoder can return, PID control, etc. But an easy way to remember it is that the Quadrature Encoders plug into a digital sensor port and any time you want to access a sensor’s value, you will use the sensorValue command.

Hope this helps!

So to reset the values, which should I use? At the moment, the SensorValue] works, but if nMotorEncoder] is more efficient, we’ll definitely switch.

John is mostly right, however, a few other things you should know.

You can use either SensorValue or nMotorEncoder with IMEs.

When using SensorValue, the parameter is the sensor port, ie. you may call SensorValue I2C_1 ]; When using nMotorEncoder, the parameter is the motor port, ie. nMotorEncoder port1 ], this will get the encoder value for whatever encoder is assigned to that motor (which can be either an IME or a red quadrature encoder).

The value read using nMotorEncoder will be sensitive to whether you have set the reversed flag on the motor. When sending positive speeds to the motor, nMotorEncoder will always count up no matter how this flag is set. SensorValue will not do this, setting the reverse flag and sending positive speeds (the motor will now turn the other way) will cause a decreasing count. This is a really useful feature, for example, lets say you have an IME on left and right sides of a drive, the right hand side may have the reverse motor flag set but, by using nMotorEncoder, both will seen to increment as the robot drives forward rather than one incrementing and the other decrementing.

You can use either.

Just a little tip:
Encoders are efficient and easy to program when added to the end of a transmission system, because of assembling clearance. Like mounting on wheel shaft is much better mounting on motor shaft. Especially in gear system, like a scissor lift. If you use IMEs, it might turn out that, the motor runs a round or even two, but the lift still not moving.
Thus, IMEs are better used in direct connection like wheels, or claws, where few gears are using.