While loop does weird things with mav commands

10 replies [Last post]
ProfLee1992
ProfLee1992's picture
Title: NooBot
Joined: 04/21/2014
Posts:
BotPoints: 7
User offline. Last seen 5 years 30 weeks ago.

I run this simple little program.... and it DOES NOT work each time - and there is no consistency to the inconsistency.

Sometimes - completely fails to move forward but I know I am in the loop b/c I get the message.

Reacts to the touch sensor however.

Sometimes it runs at full speed (1 wheel only) and then both and then react to the sensor.

Other times it is just fine.

Latest firmware (KIPR link) - runs on the simulator each time -- If anyone has any insight it would be appreciated.
(and yes- have swapped out numerous motors - and yes everything is plugged in correctly)

int main()
{

while(digital(15) == 0)
{
printf("Moving Forward\n");
mav(1,500);
mmav(3,500);
}

ao();
printf("Object Encountered - All STOP\n");

msleep(2000); //give it a little rest before backing up

printf("Moving Backward\n");
mrp(1, 500, -4400);
mrp(3, 500, -4400);
bmd(1); bmd(3);

ao();
printf("Program Complete\n");

return 0;
}

ruler501
ruler501's picture
Title: NooBot
Joined: 01/29/2012
Posts:
BotPoints: 367
User offline. Last seen 8 years 3 weeks ago.

Try this code

  1. int main(){
  2. printf("Moving Forward\n");
  3. mav(1,500);
  4. mav(3,500);
  5. while(digital(15) == 0) msleep(15);
  6.  
  7. ao();
  8. printf("Object Encountered - All STOP\n");
  9.  
  10. msleep(2000); //give it a little rest before backing up
  11.  
  12. printf("Moving Backward\n");
  13. mrp(1, 500, -4400);
  14. mrp(3, 500, -4400);
  15. bmd(1); bmd(3);
  16. ao();
  17.  
  18. printf("Program Complete\n");
  19.  
  20. return 0;
  21. }

It should work better without calling mav constantly(also you misspelled one of the mav as mmav). You can move the printf if you'd rather have it in the loop.

ProfLee1992
ProfLee1992's picture
Title: NooBot
Joined: 04/21/2014
Posts:
BotPoints: 7
User offline. Last seen 5 years 30 weeks ago.

interesting - didn't think calling the mav command continuously would be a problem -- used to work this way on the old cbc units just fine -- is there something different about the KIPR links?

thanks

ruler501
ruler501's picture
Title: NooBot
Joined: 01/29/2012
Posts:
BotPoints: 367
User offline. Last seen 8 years 3 weeks ago.

I'm not sure if it will cause a problem, but it's best to avoid if you can.

stephenmac7
Title: NooBot
Joined: 11/19/2013
Posts:
BotPoints: 29
User offline. Last seen 9 years 5 weeks ago.

I've also had issues with the mav command. It seems broken.

ruler501
ruler501's picture
Title: NooBot
Joined: 01/29/2012
Posts:
BotPoints: 367
User offline. Last seen 8 years 3 weeks ago.

I haven't worked with a robot for almost a year now, but it didn't seem broken to me, just buggy when it comes to precision(and low velocities)

Terry
Terry's picture
Title: NooBot
Joined: 06/08/2009
Posts:
BotPoints: 55
User offline. Last seen 6 years 1 week ago.

My experience is that both mav and mrp commands work poorly at speed/velocity of less than 600 in the Link OS. I also had problems of no response if a while loop doesn't have a 10-15 msec msleep delay.
I never put these commands inside a while loop.
Did you know that they do run at speeds to 1500?

History is a race, between Education and Catastrophe

old_guy
Title: NooBot
Joined: 06/01/2014
Posts:
BotPoints: 10
User offline. Last seen 8 years 40 weeks ago.

I am having a similar problem with running mav() inside a while - I am trying to get a feel for the movement distances and build up some data.

But the motors seem to start randomly inside this loop.

Please see the code below:
int main()

{
//set up six tests
// by having the extra buttons available
extra_buttons_show();
int loop_count = 0;
// go straight
// each test will start with a one second wait after the button is pressed
while (1)
{
//press one of the 6 buttons - s_time is for sleep
int button_pressed = 0;
int s_time = 0;
loop_count++;
printf("loop number is %d \n",loop_count);
ao();

if (a_button()==1) button_pressed =1;
if (b_button()==1) button_pressed =2;
if (c_button()==1) button_pressed =3;
if (x_button()==1) button_pressed =4;
if (y_button()==1) button_pressed =5;
if (z_button()==1) button_pressed =6;

printf("button pressed is number %d \n", button_pressed);
s_time = button_pressed *500;

// after button press, wait one second before beginning movement
msleep(1000);
ao();

printf("go straight %d \n", s_time);
mav(1,1500);
mav(3,1500);
msleep(s_time);
ao();
// freeze(1);
// freeze(3);
}
printf("did this work?\n");
return 0;
}

Any thoughts?

JK

Terry
Terry's picture
Title: NooBot
Joined: 06/08/2009
Posts:
BotPoints: 55
User offline. Last seen 6 years 1 week ago.

I think what you need is a while loop to sample the buttons on the condition '!button_pressed' that includes each if statement.
As soon as one button is pressed the code will continue with the value selected and run the motors for 1/2 to 3 sec. That will keep looping in while(1) stopping after the loop count and waiting for the next button to be pressed, never printing the last statement.

History is a race, between Education and Catastrophe

ProfLee1992
ProfLee1992's picture
Title: NooBot
Joined: 04/21/2014
Posts:
BotPoints: 7
User offline. Last seen 5 years 30 weeks ago.

I've been getting around the wonkiness of 'mav' by using 'motor' command instead -- for whatever reason with the latest version of KISS - mav and while loops are not consistent --- especially when trying to also use an ET sensor to dynamically adjust the speed based on distance

Terry
Terry's picture
Title: NooBot
Joined: 06/08/2009
Posts:
BotPoints: 55
User offline. Last seen 6 years 1 week ago.

I forgot to say that if you wait for a "button_pressed", after using it to set the run time you need to reset it to '0' so the loop will wait for the next press.
I also want to say that I tried many more 'mav()' tests, including revising the PID gains. The net result is that I think the mav function does not initialize correctly, and thus always starts with a left-over error from the last use [not good if you want to drive two motors at the same rate to go straight or follow a repeatable turn. I suggest driving with 'motor()' function and using 'get_motor_position_counter()' to determine where to stop. rather than time.

History is a race, between Education and Catastrophe