Is there a simple way to invert the integrated encoders? At the moment I have one on each side of my drive and the left gives me positive values and the right negative values when I drive forward. With the normal encoders it was possible to simply switch the two wires to invert the direction but I can’t seem to find a way to do this with the new encoders. Thanks in advance
I’m not too sure if theres a physical way to invert it(the wires are keyed) but i believe if you are using robotc, you can invert the motor in the setup and the encoder value associated with that motor would also be inverted. Another way which i use is to just abs() and use the absolute value of the encoder.
You don’t say if you are using ROBOTC or EasyC so I will assume ROBOTC, however, the concept is the same in either case.
You cannot invert encoder counting by modifying the wiring with the IMEs. The IME communicates with the cortex using serial communication known as I2C (inter integrated circuit) with each of the 4 wires having a specific function, +5v, 0V, serial clock and serial data. The correct way to reverse the counter is to tell ROBOTC that one of the motors is reversed by setting the reverse checkbox in the motors & sensors dialog, if you already have the motors both running forwards correctly with your current code then you have reversed the motor by another method, either wiring backwards or inverting the command value sent to it in the code. Although this works, you will be far better off by having consistent wiring and commands and setting the reverse flag as I described.
One thing you can do, which is really using two “wrongs” to make a “right”, is to multiply the encoder counter by (-1). This will work in the the short term but will get you into trouble if you ever try and use the upcoming PID functionality as it is likely to assume that positive motor commands result in increasing encoder counts.
Yes I am using RobotC
That explains it, yes I have plugged one of them in the wrong way, I didn’t realise that reversing the motor would also reverse the encoder. Thanks
We are using RobotC & have the same question. The motors are reflected thru the Motors & Sensors set up where the right motor box is checked off as reversed. The encoders box is also checked off & the I2C box is selected. Both motors are connected to the Motor Controllers black wire to black wire. We ran our code & it seems to run well. However on checking the debug window we noticed the IME for the right motor which was reflected is negative while the left is positive. Does the processor somehow internally correct for this reflection because so far it seems to work but appears as though it shouldn’t because of the negative values on one side of the drive.
Values can go negative but should increase as the motors go forwards with a positive command value.
Here is the debug window from a four motor drive (ignore the two 269s).
[ATTACH]6832[/ATTACH]
Left hand motors are not reversed and rotate counter clockwise if viewed down the axle. Right hand motors are reversed. All motors are encoded and the encoder count increases with positive command values. The encoder count always starts from 0 so if driven backwards the all the encoder counts will decrease and show as negative. If the robot was turned clockwise then left hand encoders would increase and right hand decrease.
With the two wire ports 1 and 10 this works correctly if the red wire is to the center of the cortex, if the red is placed on the outside then the motor will turn the other way and everything runs backwards.
A not so simple way I did it in Easy C cortex was by using an assignment.
(my two right encoders needed to be inverted, and I was using four encoders)
Upper_right_encoder_invert = Upper_right_encoder * -1
Lower_right_encoder_invert = Lower_right_encoder * -1
Then for my program I replaced the old variables to the new inverted variable. Similar to this
If (((Left_forward_encoder + Left_back_encoder) / 2) > ((Upper_right_encoder_invert + Lower_right_encoder_invert) / 2))
Then reduce power to left side
Forgive me but I’m not seeing this. When the left motor is not reversed & the right motor is reversed in the Motor & Sensors dialog box with the encoder checked off & I2C selected, when we run positive command values our right encoder shows negative values in the debug window & the left shows positive. Yet the motors move forward as though both received positive commands. On turning left, both encoders show negative values & on turning right both show positive values. It appears as though the debug window is showing raw data but somehow the “nMotorEncoder” function is internally flipping the negative values to positive because it knows the motor was reversed in the Motor & Sensor set up. All of our forward, reverse, turn functions work as though the reversed motor encoder is going positive when there is a positive command value even if the debug window shows a negative value.
Ok, lets back up one step. Which version of ROBOTC do you have installed? V3.51 is the latest release and is what I’ve been using lately. Are any motors in the 2-wire ports? It’s possible to get the behavior you describe from those but I though you were using MC29s.
EDIT:
Another thought. Are you looking in the “sensors” debug window or the “Motors” debug window. The motors window will show the value of nMotorEncoder, the sensors window will show the raw, non-inverted value. Reading the encoder using SensorValue gives raw data.
I’m using RobotC vs 3.51. But you are right. I’m using the 3 wire ports with the Motor Controller. I was looking at the Sensor Debug window. Never occurred to me to check the Motor Debug window. Thanks for the explanation. Will check it tomorrow at school.