Trouble reading packet serial data with Arduino Mega

General discussion of using Roboclaw motor controllers
Post Reply
akreager
Posts: 2
Joined: Wed Apr 22, 2020 5:16 pm

Trouble reading packet serial data with Arduino Mega

Post by akreager » Sat May 23, 2020 8:24 am

I am having trouble reading data out of a 2x30A Roboclaw V5D using an Arduino Mega. I am able to send command data to the RC using packet serial and make both motors move as expected. However, I am unable to read any data from the Roboclaw.
Below is the example sketch for reading the version out, modified slightly to use a serial-enabled lcd to display the output because I don't have a computer near the robot.

Code: Select all

//See BareMinimum example for a list of library functions

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

RoboClaw roboclaw(&Serial1, 10000);

#define address 0x80

#define COMMAND 0xFE
#define CLEAR   0x01
#define LINE0   0x80
#define LINE1   0xC0

void setup() {
  //Communicate with serial LCD at 9600 baud and clear display
  Serial2.begin(9600);
  delay(100);
  Serial2.write(COMMAND);
  Serial2.write(CLEAR);
  delay(100);
  
  //Communicate with roboclaw at 230400 baud
  roboclaw.begin(230400);
  delay(100);
}

void loop() {
  char version[32];

  if (roboclaw.ReadVersion(address, version)) {
    Serial2.write(COMMAND);
    Serial2.write(LINE0);
    Serial2.print(version);
  }

  delay(100);
}
Below is another sketch I wrote mostly from scratch trying to read the battery voltage, which is my end goal of this endeavor. I switched to the read version sketch to see if my issue was with my lack of coding skillz. The output of this is always invalid.

Code: Select all

//Sketch to read data from Roboclaw motor driver
//Basicmicro Arduino library and examples:
//https://github.com/basicmicro/roboclaw_arduino_library

#include "RoboClaw.h"

RoboClaw roboclaw(&Serial1, 10000);

#define address 0x80

#define COMMAND 0xFE
#define CLEAR   0x01
#define LINE0   0x80
#define LINE1   0xC0

int led = 13;
unsigned int rawBattery = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);

  //Communicate with roboclaw at 230400 baud
  roboclaw.begin(230400);
  delay(100);

  //Communicate with serial LCD at 9600 baud
  Serial2.begin(9600);
  clear_lcd();
  delay(100);

  Serial2.write(COMMAND);
  Serial2.write(LINE0 + 2);
  Serial2.print(F("Main Battery:"));

  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  bool valid1;	
  rawBattery = roboclaw.ReadMainBatteryVoltage(address, &valid1);
  int mainBattery = rawBattery / 10;
  if (valid1) {
    Serial2.write(COMMAND);
    Serial2.write(LINE1);
    Serial2.print(rawBattery);
    Serial2.write(COMMAND);
    Serial2.write(LINE1 + 7);
    Serial2.print(mainBattery);
    Serial2.print(F("."));
    Serial2.print(rawBattery - (mainBattery * 10));
    Serial2.print(F(" V   "));
  }
  else {
    Serial2.write(COMMAND);
    Serial2.write(LINE1 + 4);
    Serial2.print(F("INVALID! "));
  }
  delay(1000);
}

void clear_lcd(void)
{
  Serial2.write(COMMAND);
  Serial2.write(CLEAR);
}
Any help with this would be greatly appreciated.

Thanks,
Allen

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

Re: Trouble reading packet serial data with Arduino Mega

Post by Basicmicro Support » Tue May 26, 2020 7:59 am

First change your Roboclaw baudrate. Arduino cant do 230400. It rounds up to 250000 IIRC. 115200 usually is ok but that rounds down to 111111 on the arduino.

Next if you have an oscope check the S2 pin on the Roboclaw is outputing anything when you send a readversion command. If the pin is always low make sure you haven't enabled multiunit mode. If you did you need to disable it or add a pullup resistor.

If the pin is high and nothing is coming out when looking at a scope there may be something wrong with the S2 pin on your Roboclaw. In that case you can send it in with your contact information and we will try to determine what the problem is. If it is simple there will be no charge other than shipping.

akreager
Posts: 2
Joined: Wed Apr 22, 2020 5:16 pm

Re: Trouble reading packet serial data with Arduino Mega

Post by akreager » Tue May 26, 2020 3:05 pm

Baud rate did it. The lesson here is that just because your speedometer goes to 230mph, doesn't mean you should always drive that fast. Thanks!

Post Reply