I don't know why I cannot move the motor - Roboclaw 2x30

General discussion of using Roboclaw motor controllers
Post Reply
mihai
Posts: 3
Joined: Sat Sep 24, 2016 12:01 am
I don't know why I cannot move the motor - Roboclaw 2x30

Post by mihai »

Hi there,

I created a small C++ example to control the board, but from an unknown reason I cannot move a motor.

I can read the firmware version (it returns USB Roboclaw 2x30a v4.1.16), I can read the temperature, main battery voltage (LIPO 4S battery), but I cannot move the motor.

Here is my code:

Code: Select all

// header file
#include "rs232.h"

class t_roboclaw_controller{

private:
	// version number of the library
	char library_version[20];

	// port number of the serial connection
	int port_number;

public:

	t_roboclaw_controller(void);
	~t_roboclaw_controller(void);
	const char* get_library_version(void);

	bool connect(int port, int baud_rate);
	void close_connection(void);
	double get_temperature(void);
	double get_main_battery_voltage(void);
	void get_firmware_version(char *firmware_version);

	void drive_forward_M1(unsigned char speed);
	void drive_forward_M2(unsigned char speed);
};

Code: Select all

// cpp implementation

#include "roboclaw_controller.h"
#include <stdint.h>

//--------------------------------------------------------------
uint16_t CRC16(unsigned char *packet, int nBytes)
{
	uint16_t crc = 0;
	for (int byte = 0; byte < nBytes; byte++) {
		crc = crc ^ ((uint16_t)packet[byte] << 8);
		for (unsigned char bit = 0; bit < 8; bit++) {
			if (crc & 0x8000) {
				crc = (crc << 1) ^ 0x1021;
			}
			else {
				crc = crc << 1;
			}
		}
	}
	return crc;
}
//--------------------------------------------------------------
t_roboclaw_controller::t_roboclaw_controller(void)
{
	strcpy(library_version, "2016.09.22.0"); // year.month.day.build number
}
//--------------------------------------------------------------
t_roboclaw_controller::~t_roboclaw_controller(void)
{

}
//--------------------------------------------------------------
const char* t_roboclaw_controller::get_library_version(void)
{
	return library_version;
}
//--------------------------------------------------------------
bool t_roboclaw_controller::connect(int port, int baud_rate)
{
	char mode[] = { '8', 'N', '1', 0 };

	port_number = port;

	return RS232_OpenComport(port, baud_rate, mode) == 0;
}
//--------------------------------------------------------------
void t_roboclaw_controller::close_connection(void)
{
	RS232_CloseComport(port_number);
}
//--------------------------------------------------------------
double t_roboclaw_controller::get_temperature(void)
{
	unsigned char s[10];
	s[0] = 0x80;// port
	s[1] = 82;
	RS232_SendBuf(port_number, s, 2);
	Sleep(10);

	RS232_PollComport(port_number, s, 10);

	return (double)(s[0] << 8 | s[1]) / 10.0;
}
//--------------------------------------------------------------
double t_roboclaw_controller::get_main_battery_voltage(void)
{
	unsigned char s[10];
	s[0] = 0x80;// port
	s[1] = 24;
	RS232_SendBuf(port_number, s, 2);
	Sleep(10);

	RS232_PollComport(port_number, s, 10);

	return (double)(s[0] << 8 | s[1]) / 10.0;
}
//--------------------------------------------------------------
void t_roboclaw_controller::get_firmware_version(char *firmware_version)
{
	firmware_version[0] = 0x80;// port
	firmware_version[1] = 21;
	RS232_SendBuf(port_number, (unsigned char*)firmware_version, 2);
	Sleep(10);

	unsigned char s[32]; 
	RS232_PollComport(port_number, s, 32);
	strcpy(firmware_version, (char*)s);
}
//--------------------------------------------------------------
void t_roboclaw_controller::drive_forward_M1(unsigned char speed)
{
	unsigned char buffer[5];
	buffer[0] = 0x80;// port
	buffer[1] = 0;
	buffer[2] = speed;
	unsigned int crc = CRC16(buffer, 3);
	buffer[3] = crc >> 8;
	buffer[4] = crc;
	RS232_SendBuf(port_number, buffer, 5);
}
//--------------------------------------------------------------
void t_roboclaw_controller::drive_forward_M2(unsigned char speed)
{
	unsigned char buffer[5];
	buffer[0] = 0x80; // port
	buffer[1] = 1;    // command
	buffer[2] = speed;

	uint16_t crc = CRC16(buffer, 3);
	buffer[3] = crc >> 8;
	buffer[4] = crc;
	
	RS232_SendBuf(port_number, buffer, 5);
}
//--------------------------------------------------------------

Code: Select all

// main program
#include "roboclaw_controller.h"



//--------------------------------------------------------------
int main(void)
{
	t_roboclaw_controller roboclaw;

	if (!roboclaw.connect(18, 38400)) { // real - 1
		printf("Cannot connect! Game over.");
		getchar();
		return 1;
	}

	char buffer[100];
	buffer[0] = 0;
	int buffer_length = 100;

	// firmware version
	roboclaw.get_firmware_version(buffer);
	printf("Firmware version = %s\n", buffer);

	// temperature
	Sleep(10);
	double temperature = roboclaw.get_temperature();
	Sleep(10);
	printf("Temperature = %lf\n", temperature);

	// main battery voltage
	Sleep(10);
	double main_battery_voltage = roboclaw.get_main_battery_voltage();
	printf("Battery voltage = %lf\n", main_battery_voltage);

	// move motor 2 forward
	Sleep(10);
	roboclaw.drive_forward_M2(10);
	Sleep(1000);

	printf("Program over. Press Enter.");
	getchar();

	return 0;
}
//--------------------------------------------------------------
When I run the program I get the followings:


Firmware version = USB Roboclaw 2x30a v4.1.16

Temperature = 36.200000
Battery voltage = 15.200000
Program over. Press Enter.


But, the motor does not move.


Please help me.

thanks,
mihai
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: I don't know why I cannot move the motor - Roboclaw 2x30

Post by Basicmicro Support »

If the CRC is not valid the write commands(eg commands to run the motors) will be ignored. You can check your CRC calculation by comparing the read back CRC with your own calculation using your CRC function. You should always calculate and compare the CRC returned by a read command any way.
mihai
Posts: 3
Joined: Sat Sep 24, 2016 12:01 am
Re: I don't know why I cannot move the motor - Roboclaw 2x30

Post by mihai »

In the meantime I have found the problem: I have not sent the correct command for moving the motor #2.

Now all working. Great board !

thanks,
mihai
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: I don't know why I cannot move the motor - Roboclaw 2x30

Post by Basicmicro Support »

Ok. Let me know if you need anything else.

Post Reply