View unanswered posts | View active topics It is currently Sat Oct 25, 2014 6:13 am






Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next
Encoders aren't changing correctly 
Author Message
Rookie
User avatar

Joined: Thu Jan 02, 2014 11:04 pm
Posts: 15
Post Encoders aren't changing correctly
I created a program that ramped the robot's motors up to 125 then back down to 0. I added the encoder values to an array every 10ms and then at the end of the program i printed them out to the debug stream. Here is my program:

#pragma config(Hubs, S1, HTMotor, none, none, none)
#pragma config(Motor, motorA, , tmotorNXT, openLoop, encoder)
#pragma config(Motor, motorB, , tmotorNXT, openLoop, encoder)
#pragma config(Motor, motorC, , tmotorNXT, openLoop, encoder)
#pragma config(Motor, mtr_S1_C1_1, LeftMotor, tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S1_C1_2, RightMotor, tmotorTetrix, openLoop, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#define ARRAY_IDX_VAL 550
int Array[ARRAY_IDX_VAL];// Right Motor Encoder
int Array2[ARRAY_IDX_VAL];// Left Motor Encoder
int Array3[ARRAY_IDX_VAL];// Set Motor Power

int Index = 0;

task main()
{
int i;
int c;
int d;

clearDebugStream();

nMotorEncoder[RightMotor] = 0;
nMotorEncoder[LeftMotor] = 0;

for(i = 0; i<125; i = i + 5)
{
motor[RightMotor] = i;
motor[LeftMotor] = i;
for(c = 0; c<10; c++)
{
wait1Msec(10);
Array[Index] = nMotorEncoder[RightMotor];
Array2[Index] = nMotorEncoder[LeftMotor];
Array3[Index] = motor[RightMotor];
Index++;
}
}
for(i = 125; i>0; i = i - 5)
{
motor[RightMotor] = i;
motor[LeftMotor] = i;
for(d = 0; d<10; d++)
{
wait1Msec(10);
Array[Index] = nMotorEncoder[RightMotor];
Array2[Index] = nMotorEncoder[LeftMotor];
Array3[Index] = motor[RightMotor];
Index++;
}
}
wait1Msec(500);
motor[RightMotor] = 0;
motor[LeftMotor] = 0;
for(Index = 0; Index<ARRAY_IDX_VAL; Index++)
{
wait1Msec(10);
writeDebugStream("%d,", Index);
wait1Msec(10);
writeDebugStream("%d,", Array[Index]);
wait1Msec(10);
writeDebugStream("%d,", Array2[Index]);
wait1Msec(10);
writeDebugStream("%d\n", Array3[Index]);
}
}

the excel file that includes my data printed out to the debug stream is attached.
the first column is the index of the array (I shortened the Index in the excel file to 502)
the second column is the Right encoder values and the third column is the left encoders values
the fourth column is the power getting put into the motors.
the fifth column is the right encoder values change (current encoder value - previous encoder value)
the sixth column is the left encoder values change

the encoders aren't changing at the same rate and the changes seem to be all over the place
can someone help explain to me why this is happening?
Thank you so much,
kitbear23


Attachments:
RampData3.csv [11.38 KiB]
Downloaded 172 times
Thu Jan 02, 2014 11:43 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: Encoders aren't changing correctly
Hi kitbear23,
kitbear23 wrote:
the encoders aren't changing at the same rate and the changes seem to be all over the place
can someone help explain to me why this is happening?

We sometimes like to pretend that reality is a perfect place in order to make it easier to make predictions about it, but this is almost never actually the case. Even though you're applying the same amount of power to the left and right motors, both motors are not exactly the same: one may be slightly better than the other at converting electrical energy to movement, one might have more friction than the other. One or more of the robot's wheels may be slipping on the ground instead of gripping it fully - though this is less likely in your case since you're ramping the power to the motors up and down. All these and more small imperfections add up to create different behavior on the left and right sides of the robot.

One of the fundamental challenges in robotics is using sensors on the robot - such as the encoders - in order to compensate for these all these "real-world factors." In the same way that you can use your eyes in order to adjust the way you swing a baseball bat in order to hit a ball more accurately, or use your sense of balance in order to adjust your center of mass to keep a bicycle upright, you can program the robot to modify the amount of power being sent to its left and right wheels based on the readings of its encoders in order to drive straighter than it could when "driving blind."

Cheers,
--Ryan

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Fri Jan 03, 2014 1:04 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Encoders aren't changing correctly
There are several issues with your code. First, the maximum power you can give a motor is 100, not 125. You probably have noticed that from the Excel data. Secondly, I am guessing you are trying to figure out how linear is the motor. If that's the case, the wait1Msec(10) statements do not give you a constant sampling interval. So for the 5th and 6th columns, you really need to do the encoder difference divided by time interval. With those issues in mind, your data looked reasonable. Both left and right encoder values are incrementing as expected. The "speed columns" are indicating the correct trend (increasing with increased power) but the randomness is probably due to inconsistent time interval.


Fri Jan 03, 2014 1:16 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Encoders aren't changing correctly
It is kind of interesting because we are in the process of mapping the linearity of the motors ourselves. Instead of programming the motors by "Percentage power", we would like to program them with "Percentage of max speed". Since max speed may change depending on battery voltage and other factors, we want to dynamically adjust the "Percentage speed to motor power" map while the robot is running (i.e. Learning while running). So for example, let's say the maximum speed of the motor with a given robot design is 5000 encoder counts per second. So when programming the motor with a value of 50 (i.e. 50% power), we would treat it as 50% of maximum speed instead, so it would be 2500 encoder counts per second. If we then look up a mapping table to find out what power value we should be programming the motor in order to get 2500 encoder counts per second, we will have a very linear way of controlling the robot. This will make the robot go very straight because each motor in the drive train will have it's own power map. And it will also simplify our PID constant tuning especially when we are very close to target and the power becomes too low (i.e. the low end of the power curve of the motor).


Fri Jan 03, 2014 1:30 am
Profile
Rookie
User avatar

Joined: Thu Jan 02, 2014 11:04 pm
Posts: 15
Post Re: Encoders aren't changing correctly
Thank you for your answers! could I maybe help the encoders to read similar counts if I sync the motor? I am using tetrix motors, so I before tried to sync the motors by using syncDE (because they are motors D and E) and it gave me an error. Is there another way to sync the motors so the right and left motor encoder values will be a little more similar?

Thanks Again,
Kitbear23


Sat Jan 04, 2014 2:21 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Encoders aren't changing correctly
Quote:
So for example, let's say the maximum speed of the motor with a given robot design is 5000 encoder counts per second


just FYI, max speed is ~ 100 °/100ms = 1000°/s :)

I would control the speed by a pid controller!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Jan 04, 2014 2:31 pm
Profile
Rookie
User avatar

Joined: Thu Jan 02, 2014 11:04 pm
Posts: 15
Post Re: Encoders aren't changing correctly
I am also still confused why when the motors are running at a power of 90 (which is going pretty fast) that the encoder values stay the same. for example at index 323 and 324 (when motors are set to 90% power) the left encoder reads the same value (the encoders get read every 10ms). Are the encoders not working correctly?

Thank you for your feedback,
kitbear23


Sat Jan 04, 2014 2:35 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Encoders aren't changing correctly
kitbear23 wrote:
Thank you for your answers! could I maybe help the encoders to read similar counts if I sync the motor? I am using tetrix motors, so I before tried to sync the motors by using syncDE (because they are motors D and E) and it gave me an error. Is there another way to sync the motors so the right and left motor encoder values will be a little more similar?

Thanks Again,
Kitbear23

I believe the Sync motor function is available only to the LEGO motors, not the Tetrix motors. If you want to sync the Tetrix motors, you need to do it yourself in code (i.e. Doing your own PID).


Sat Jan 04, 2014 4:15 pm
Profile
Rookie
User avatar

Joined: Thu Jan 02, 2014 11:04 pm
Posts: 15
Post Re: Encoders aren't changing correctly
ok thank you. And what do you think about the encoder staying the same value even after 10ms has passed at motor power 90?

Thank you,
kitbear23


Sat Jan 04, 2014 4:38 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Encoders aren't changing correctly
kitbear23 wrote:
ok thank you. And what do you think about the encoder staying the same value even after 10ms has passed at motor power 90?

Thank you,
kitbear23

I am not familiar with the internals of RobotC but I believe the "nMotorEncoder" array is updated periodically by the firmware. I am guessing that the update period is probably about 10 msec (or longer) so you could be "sampling" the array within the same update period.


Sat Jan 04, 2014 5:36 pm
Profile
Rookie
User avatar

Joined: Thu Jan 02, 2014 11:04 pm
Posts: 15
Post Re: Encoders aren't changing correctly
so what do you suggest would be the right amount of time between my samples of the encoders?

Thanks again,
kitbear23


Sat Jan 04, 2014 5:54 pm
Profile
Rookie
User avatar

Joined: Thu Jan 02, 2014 11:04 pm
Posts: 15
Post Re: Encoders aren't changing correctly
I've also heard something about using I2C to read the encoders or see how fast they update. Can I use this? and if so, how can I use it?

Thank you for your feedback,
kitbear23


Sat Jan 04, 2014 5:57 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Encoders aren't changing correctly
kitbear23 wrote:
so what do you suggest would be the right amount of time between my samples of the encoders?

Thanks again,
kitbear23

Just to experiment, lengthen your wait to 100msec and see if you still see the encoders "not changing" its value.


Sat Jan 04, 2014 6:27 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Encoders aren't changing correctly
kitbear23 wrote:
I've also heard something about using I2C to read the encoders or see how fast they update. Can I use this? and if so, how can I use it?

Thank you for your feedback,
kitbear23

You can try reading I2C directly if you are comfortable with that but bear in mind that reading I2C involved polling the status so it will take time too. It probably will make your robot loop period unpredictable. So you also need to record the loop interval precisely if you want to check the "speed".


Sat Jan 04, 2014 6:33 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Encoders aren't changing correctly
so what encoders are you actually trying to read and to sync?
Lego encoder motors plugged to the motor ports
- or -
Tetrix motors plugged to the Tetrix motor controller, plugged to a NXT sensor port (and read by I²C) ?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Jan 04, 2014 6:39 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 43 posts ]  Go to page 1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.