Roboclaw encoder read out

General discussion of using Roboclaw motor controllers
Post Reply
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Roboclaw encoder read out

Post by Joe »

Hi, I'm controlling the roboclaw with an external software with arduino over serial connection.
I'm using a slider control in my software to move the motor forward and backwards. When moving the motor I'd like to read the current encoder values and print this over a serial monitor.
The value 'speed' is controled by continous serial commands from the slider.

Code: Select all

    rc.SpeedAccelM2(address,3200,speed);
readEnc2();

void readEnc2() // read encoder 2
{
    while(enc2 != new_enc2)
  {
new_enc2 = rc.ReadEncM2(address);
delay(100);
enc2 = rc.ReadEncM2(address);
Serial.println(enc2);
  }
 }
I need to use a function and avoid the loop section because I only want to run the print command if the motor moves.

What I noticed is that the encoder values are only printed if I give a stop command. Then the elapsed encoder values are printed but I need to print the values while the motor moves. If I put my code in the arduino loop section without the while it works but is running on and on.

Can anybody help me why my code is not working? Sorry, I'm a beginner.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Roboclaw encoder read out

Post by Basicmicro Support »

In general to do things the right way is much more complex than what you are doing now. Going into that would be a book all by itself so Im just going to give you an updated read_enc function to debug your problem.

void readEnc2(uint32_t distance) // read encoder 2
{
Serial.print("Reading Encoder: ")
enc2 = rc.ReadEncM2(address);
Serial.println(enc2);
do
{
new_enc2 = rc.ReadEncM2(address);
Serial.println(new_enc2);
delay(100);
}while(new_enc2 >= enc2+distance)
}

Try this out. I did not test it on an arduino but its fairly simple and I dont think I made any typos. Let me know the results.

Also note that this function now takes a distance value. And it is only valid for a forward movement(because of the do/while loop comparison.

Your old code woul never end. The reasons were two fold. One you are always comparing an older encoder position to a newer one and the motor is moving so they would never equal each other. Also relying on reaching an exact encoder position is never going to work(thats why I used a greater than or equal to comparison).

You should probably describe in more detail exactly what you are trying to accomplish.
Joe
Posts: 34
Joined: Fri Dec 23, 2016 1:00 am
Re: Roboclaw encoder read out

Post by Joe »

Hi, thank you. I will try that end of this week and report.

Post Reply