Doing one command after the other

Questions about using encoders with the Roboclaw product line
Post Reply
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Doing one command after the other

Post by Joe »

Hi, maybe someone can help me out with a piece of code ;-)

I'm using an arduino mega and a roboclaw with an encoder to control a slide on a rail. I want to move my slide to a specific position then do a measurement with a digital dial and the go to the next position and measure and so on.

My problem is if I put this whole process in a function like

Code: Select all

void measure(){
dialdata();
rc.SpeedAccelDeccelPositionM1(address, 1000, 8000, 1000, 10150, 0);	  
dialdata();
rc.SpeedAccelDeccelPositionM1(address, 1000, 8000, 1000, 19900, 0);
dialdata();
rc.SpeedAccelDeccelPositionM1(address, 1000, 8000, 1000, 29650, 0);
dialdata();
rc.SpeedAccelDeccelPositionM1(address, 1000, 8000, 1000, 39400, 0);
dialdata();
....
all movement commands run through the function because they are buffered and also all measurement commands run all through.

How can it be done to move, then measure, then move further, measure ....?
Sorry I'm a programming novice, hope someone can help!
Joe
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Doing one command after the other

Post by Basicmicro Support »

You use the ReadBuffers command to see of the motor is idle(eg the motor buffer state == 0x80). Then take your measurement. Rinse, Repeat.

Alternatively you could just read the current position and if it is near the target position, take your dial reading.
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Re: Doing one command after the other

Post by Joe »

I was doing that by reading the position but the readout loop sometimes crashes my measurement data. So how else could I read the position without using a loop?
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Re: Doing one command after the other

Post by Joe »

I got this working now for my first measurement but the next does not work. Why?

Code: Select all

void measure(){
  uint8_t depth1,depth2;
	rc.SpeedAccelDeccelPositionM1(address, 1000, 8000, 1000, 10150, 0);
	while(depth1!=0x80){	//loop until distance command completes
	
		rc.ReadBuffers(address,depth1,depth2);
						}
	dialdata();
		rc.SpeedAccelDeccelPositionM1(address, 1000, 8000, 1000, 12100, 0);
		while(depth1!=0x80){	//loop until distance command completes

			rc.ReadBuffers(address,depth1,depth2);
		}

		dialdata();
}
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Doing one command after the other

Post by Basicmicro Support »

You should use the buffer argument of value of 1 instead of 0. 0 tells the controller to buffer the commands and wait until the previous command has finished before starting the new command. If you position PID isnt tuned well enough or you dont have a large enough dead zone the first command is probably not finishing(eg its not getting to the exact position you specified) so it never starts the next command.

You should modify your program to output the return status of your commands and to see if it actually ever gets out of the first while loop.
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Re: Doing one command after the other

Post by Joe »

Hi,
1. the positioning commands work very well and exact
2. in my original sketch I printed the output of the buffer. This is also working for the first positioning command and when the buffer sent 0x80 the measurement command works and the next positioning command starts. After that no more buffer output happened and all following positioning commands run through no matter if I use 0 or 1 as buffer command.

Joe
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Doing one command after the other

Post by Basicmicro Support »

Please add print statements after each position command and inside the while loops. Post what it prints here with a description of what the motors are doing at each step.
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Re: Doing one command after the other

Post by Joe »

Hi this is the relating part of my sketch and the printed buffer. RC does only the first two position commands and the last to the starting position 400. The following two (5500 and 7500) are ignored and no more serial read out.

Code: Select all

void measure{
  uint8_t depth1,depth2;


	rc.SpeedAccelDeccelPositionM1(address, 5000, 8000, 5000, 1400, 1);

	while(depth1!=0x80)
		{	//loop until distance command completes
			//	displayspeed();
		rc.ReadBuffers(address,depth1,depth2);
		Serial.println(depth1,HEX);
		}
	dialdata();

	rc.SpeedAccelDeccelPositionM1(address, 5000, 8000, 5000, 3500, 1);

	while(depth1!=0x80){	//loop until distance command completes
		//	displayspeed();
		rc.ReadBuffers(address,depth1,depth2);
		Serial.println(depth1,HEX);
	}
	dialdata();

		rc.SpeedAccelDeccelPositionM1(address, 5000, 8000, 5000, 5500, 1);

		while(depth1!=0x80){	//loop until distance command completes
			//	displayspeed();
			rc.ReadBuffers(address,depth1,depth2);
			Serial.println(depth1,HEX);
		}
		dialdata();

		rc.SpeedAccelDeccelPositionM1(address, 5000, 8000, 5000, 7500, 1);

		while(depth1!=0x80){	//loop until distance command completes
			//	displayspeed();
			rc.ReadBuffers(address,depth1,depth2);
			Serial.println(depth1,HEX);
		}
		dialdata();


	rc.SpeedAccelDeccelPositionM1(address, 5000, 8000, 1000, 400, 1);
while(depth1!=0x80){	//loop until distance command completes
	//	displayspeed();
	rc.ReadBuffers(address,depth1,depth2);
	Serial.println(depth1,HEX);
		}
}

Code: Select all

81
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
80
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Doing one command after the other

Post by Basicmicro Support »

You have a logic flaw. After depth1 gets set to 0x80 in the first while loop you never reset it to 0 so all following while loops never enter the loop and just blow past them.

Either set depth1=0 after each while loop or change from a while loop to a do{ }while(x); loop
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Re: Doing one command after the other

Post by Joe »

Thank you that helped ;-)

Post Reply