Trouble reading packet serial data with Arduino Mega
Posted: 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.
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.
Any help with this would be greatly appreciated.
Thanks,
Allen
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);
}
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);
}
Thanks,
Allen