Suggestion for more Arduino library example sketches

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

Suggestion for more Arduino library example sketches

Post by Joe » Fri Jun 26, 2020 4:43 am

I'm developing a machine with two motors and a Roboclaw 2x30. I'm having problems moving the motors with velocitiy and position commands. Espacially with setting buffered or unbuffered commands.
My suggestion to the Basicmicro developers is at least one further example in the Roboclaw Arduino library where two motors are controlled simultaniously and independent with positioning commands and let's say blinking onboard led on the Arduino for different states of the motors in kind of a state machine.
I think this would help many others to understand programming of Roboclaws in general.
What do you think?

User avatar
Basicmicro Support
Posts: 1387
Joined: Thu Feb 26, 2015 9:45 pm

Re: Suggestion for more Arduino library example sketches

Post by Basicmicro Support » Fri Jun 26, 2020 8:17 am

This is a modified version of the Position example:

Code: Select all

//See BareMinimum example for a list of library functions

//Includes required to use Roboclaw library
#include <SoftwareSerial.h>
#include "RoboClaw.h"

//See limitations of Arduino SoftwareSerial
SoftwareSerial serial(10,11);	
RoboClaw roboclaw(&serial,10000);

#define address 0x80

//Display Encoder and Speed for Motor 1
void displayspeed(void)
{
  uint8_t status1,status2;
  bool valid1,valid2,valid1_2,valid2_2;
  int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
  int32_t speed1 = roboclaw.ReadSpeedM1(address, &status2, &valid2);
  int32_t enc2 = roboclaw.ReadEncM2(address, &status1, &valid1_2);
  int32_t speed2 = roboclaw.ReadSpeedM2(address, &status2, &valid2_2);
  
  if(valid1){
    Serial.print("Encoder1:");
    Serial.print(enc1,DEC);
    Serial.print(" ");
    Serial.print(status1,HEX);
    Serial.print(" ");
  }
  if(valid2){
    Serial.print("Speed1:");
    Serial.print(speed1,DEC);
    Serial.print(" ");
  }
  if(valid1_2){
    Serial.print("Encoder2:");
    Serial.print(enc2,DEC);
    Serial.print(" ");
    Serial.print(status2,HEX);
    Serial.print(" ");
  }
  if(valid2_2){
    Serial.print("Speed2:");
    Serial.print(speed2,DEC);
    Serial.print(" ");
  }
  
  Serial.println();
}

//This is the first function arduino runs on reset/power up
void setup() {
  //Open Serial and roboclaw at 38400bps
  Serial.begin(57600);
  roboclaw.begin(38400);
  
  Serial.println("Starting...");
}

void loop() {
  roboclaw.SpeedAccelDeccelPositionM1(address,0,12000,0,11000,1);
  roboclaw.SpeedAccelDeccelPositionM2(address,0,12000,0,1000,1);
  roboclaw.SpeedAccelDeccelPositionM1(address,0,12000,0,1000,0);
  roboclaw.SpeedAccelDeccelPositionM2(address,0,12000,0,11000,0);
  roboclaw.SpeedAccelDeccelPositionM1(address,32000,12000,32000,11000,0);
  roboclaw.SpeedAccelDeccelPositionM2(address,32000,12000,32000,1000,0);
  roboclaw.SpeedAccelDeccelPositionM1(address,32000,12000,32000,1000,0);
  roboclaw.SpeedAccelDeccelPositionM2(address,32000,12000,32000,11000,0);
  long last = millis();
  while(millis()-last<5000){
    displayspeed();
    delay(50);
  }
}
The position exmple shows using buffered commands. Those commands with a buffer value of 1 start immediately(one for each motor channel). All other commands( with buffer argument 0) are added to the command buffer and will execute in the order they were received. Each motor channel has its own command buffer.

Sending the movement commands to the Roboclaw only takes a few milliseconds.

At that point, the program spends 5 seconds in the While loop. You could replace the while loop with any other code you need to run or you could change the while loop condition to check if the motor movements have completed(by using the GetBuffers command) instead of the arbitrary 5 second wait use in the example of course.

The displayspeed function is effectively your blinking led except instead of blinking an LED it is printing out data being read from the motor controller(eg the current encoder count and speed of each motor) all while the motors are executing the movements they were previouslt sent.

Post Reply