View Full Version : Autonomous problem
railerobotics
06-02-2006, 20:36
I have written this for atonomous code but the the robot does not stop when the counter reaches 2000. Anyone know why?
/* Add your own autonomous code here. */
switch (Step)
{
case 1:
{
stop = 2000;
crawl = (stop - 200);
slow = (stop - 100);
Left_Count = Get_Left_Encoder_Count();
Right_Count = Get_Right_Encoder_Count();
if (Left_Count < slow)
pwm03 = 220;
if (Left_Count > slow && Left_Count < crawl)
pwm03 =200;
if (Left_Count > crawl && Left_Count < stop)
pwm03 =150;
if (Left_Count >= stop)
pwm03 =127;
printf("Left Counter %ld\r", Left_Count);
if (Right_Count < slow)
pwm04 =220;
if (Right_Count > slow && Right_Count < crawl)
pwm04 =200;
if (Left_Count > crawl && Right_Count < stop)
pwm04 =150;
if (Right_Count >= stop)
pwm04 =127;
printf("Right Counter %ld\r", Right_Count);
Jared Russell
06-02-2006, 20:44
Make sure that your encoders are counting the direction that you expect them to.
Are you changing the values of pwm03 or pwm04 anywhere else?
railerobotics
06-02-2006, 20:54
I am not changing the pwm's anywhere else and the encoders are counting in the correct direction. Actually i am using hall effect sensors with encoder code.
Chriszuma
06-02-2006, 21:32
Where did the "step" variable come from? I don't see it being used anywhere in that code block.
EDIT: It would help if you posted the rest of that part of the code so we could tell more about the problem (don't worry, we aren't going to steal your code for our own use)
Alan Anderson
06-02-2006, 21:58
Step needs to be set to 1 somewhere, but I don't see it being done in the code fragment you posted.
Dillon Compton
07-02-2006, 00:35
Actually i am using hall effect sensors with encoder code.
Will that work?
I dont know, I'm just asking...if the count isnt updating due to code not interfacing properly with your sensors, that could be the issue.
Keith Watson
07-02-2006, 01:03
Let's look at your algorithm first. I have changed your mode variables to hard coded numbers which makes the code more obvious.
/* Add your own autonomous code here. */
switch (Step)
{
case 1:
{
stop = 2000;
crawl = 1800; // are you sure these
slow = 1900; // are in the correct order?
Left_Count = Get_Left_Encoder_Count();
Right_Count = Get_Right_Encoder_Count();
if (Left_Count < 1900)
pwm03 = 220;
if (Left_Count > 1900 && Left_Count < 1800)
pwm03 =200;
if (Left_Count > 1800 && Left_Count < 2000)
pwm03 =150;
if (Left_Count >= 2000)
pwm03 =127;
printf("Left Counter %ld\r", Left_Count);
if (Right_Count < 1900)
pwm04 =220;
if (Right_Count > 1900 && Right_Count < 1800)
pwm04 =200;
if (Left_Count > 1800 && Right_Count < 2000)
pwm04 =150;
if (Right_Count >= 2000)
pwm04 =127;
printf("Right Counter %ld\r", Right_Count);
Notice that this shows problems with initialization of the crawl and slow values. Some lines are unreachable and some condition statements are executed twice. A simpler and more manageable way to use multiple if statements on the same variables is this form:
if (count < slow) {
// fast
}
else if (count < crawl) {
// slow
}
else if (count < stop) {
// crawl
}
else {
// stop
}There is nothing in your algorithm preventing a pwm of 127 being set if the count is actually incrementing. If your printf statements are being reached and show the counts go over 2000 then the next thing to check is if setting those pwm's to 127 really does stop the motors.
railerobotics
07-02-2006, 08:36
thanks for your help and step is defined at the top of user routines fast.
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.