CM-510 firmware and post updated
CM-900 is really tiny and cheap, so is perfect to use as communication bridge between any computer (Raspberry Pi, Pandaboard, etc. included, of course) and the Dynamixel bus. Whatever it receives from the Serial USB (usually commands and queries) is sent to the Dynamixel bus, and what it receives from the Dynamixel bus is sent to the SerialUSB (usually answers)
Here is the source code of the little program for CM-900 IDE:
int counter; bool onlyOnceHappened; void blinkOnce() { digitalWrite(BOARD_LED_PIN, LOW); delay_us(100); digitalWrite(BOARD_LED_PIN, HIGH); } void setup() { pinMode(BOARD_LED_PIN, OUTPUT); onlyOnceHappened=false; counter=0; //USB Serial initialize SerialUSB.begin(); // SerialUSB.attachInterrupt(USBDataReceived); //DXL initialize Dxl.begin(1); } byte aByte=0; uint8 aUint8; void loop() { // SerialUSB.println (counter++); if (onlyOnceHappened==false) { blinkOnce(); onlyOnceHappened=true; delay (3000); //Some time to the user to activate the monitor/console SerialUSB.println ("v1.1.1 Orders receiver started"); } if (SerialUSB.available()) { aUint8=SerialUSB.read(); blinkOnce(); Dxl.writeRaw(aUint8); // delay(20); } if (Dxl.available()) { aByte=Dxl.readRaw(); blinkOnce(); SerialUSB.write(aByte); // delay(20); } }
In the next post I will include an improved version that could read sensors connected to the CM-900, “expanding” the Dynamixel protocol.
To cretate this workshop I’m using Ubuntu 12.04, GNU g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 (see Synaptic installer capture),
QT 5.0.1 and QT creator 2.6.2,
QT creator is a very easy, and good, free IDE with a great design tool for creating user interfaces:
Be careful with this bug: Qt Creator 2.0: UI layout is not updated before launching the application http://qt-project.org/forums/viewthread/292 Is very annoying and time consuming, the solution is in the last comment (setting the UI_DIR in the .pro file)
I also will use it with Windows 7.
I also use Boost to get some C++11 features for threading. For example:
pWorkThread = new thread(&Activity::doWork, this, parameter);
std::lock_guard < std::mutex > guard(myMutex);
Don’t bother if you don’t understand it right now, it’s in the guts of the AXControl_v2 library we will use it in a very easy way, as you will see below. Here you will find a lot of resources to learn basic and advanced C++
Example context fot std::lock_guard < std::mutex > guard(myMutex);
short DynamixelCommunication::readSensorValue(int id, AX12Address address, int port) { std::lock_guard < std::mutex > guard(myMutex); short value = -1; try { int size = getReadSensorWordCommand(buffer, (byte) (id), address, (byte) (port)); memset(&result, 0, MaxBufferSize); serialPort.query(buffer, result, size, WaitTimeReadSensor); value = getQueryResult(result, size); if (value <= 0) Debug::show("DynamixelCommunication.readSensorValue", value); } catch (exception e) { Debug::show("DynamixelCommunication.readSensorValue", e.what()); } return value; }
And context for pWorkThread = new thread(&Activity::doWork, this, parameter);:
class Activity { private: protected: std::thread *pWorkThread; ... } void Activity::start(int parameter) { working = true; pWorkThread = new thread(&Activity::doWork, this, parameter); Util::sleepMS(10); } Let's see how to open the connection
Let’s see how to open the connection to the CM-510:
void MainWindow::on_B_Open_clicked() { //QString output="Open it"; //QMessageBox::information(this, "Output", output, QMessageBox::Ok); //dynComm.open("/dev/ttyUSB0",57600); //open with these parameters if (pDynComm->open()) //open getting the parameter from the configuration fiel { //Connection opened setConnectionButtons(true); //User interface update updateAX12Parameters(ui->SB_AX12_1_ID->text().toInt()); //User interface update } else { //Show problem opening the connection string cantOpenPortName="I can't open port: "; cantOpenPortName+=pConf->getStringValue(DynamixelCommunication::ParameterSerialPortName); QMessageBox::information(this, "Error", QString::fromStdString(cantOpenPortName), QMessageBox::Ok); } }
and get a beep from the Robotis CM-510 (using this alternative firmware):
pDynComm->sendOrder(200, MyCommunications::BeepCM510, 0); //200 is the Dynamixel device ID used for the CM-510, 0 because beep doesn't need any additional value
Getting the AX12 position and showing it:
void MainWindow::on_B_AX12_1_GET_POS_clicked() { QString qStr; int id = getAX12_1_Id(); int position = pDynComm->readValue(id, MyCommunications::PresentPosition); ui->SB_AX12_1_POS->setValue(position);
Setting the AX12 position:
void MainWindow::on_B_AX12_1_SET_POS_clicked() { int id = getAX12_1_Id(); int position= ui->SB_AX12_1_POS->text().toInt(); pDynComm->sendOrder(id, MyCommunications::GoalPosition,position); }
Putting the selected AX12 LED on and off
void MainWindow::on_CH_AX12_1_LED_clicked(bool checked) { int id = getAX12_1_Id(); pDynComm->sendOrder(id, MyCommunications::LED, (checked?1:0));// if checked 1, else 0 }
And an auxiliary UI method to get the id from the selected AX-12
{ int MainWindow::getAX12_1_Id() { QString qStr=ui->SB_AX12_1_ID->text(); int id=Util::convertToInt(qStr.toStdString()); return id; }
You can download sources and Linux binaries here
A diagram with all the AXControl_v2 lib classes and their methods:
[Update]
[V.02 updates: AX C++ architecture, core classes diagram and HexaWheels scanning video]
C++, specially with the great improvements of the last C++11 standard, joins together a great efficiency in performance and a low memory footprint with advanced high level language features, making C++ a great tool for embedding, robotics, programming.
If you want to know how to use C++ very efficiently these two guides will help you:
– The JSF air vehicle C++ coding standards ( F-35 fighter aircraft)
– ISO C++ committee’s report on performance
.
.
Bioloid Premium is a wonderful kit for creating legged and wheeled robots, including (here full parts list):
– 18 powerful and versatile AX-12 servos
– an ATMega 2561 (CM-510) or, recently, an ARM STM32F103RE 32bits (CM-530), based controller. Also you can control the AX-12 with the USB2Dynamixel straight from your USB with a FTDI driver.
– And a lot of parts to create the structure of the robot
.
Raspberry Pi is the cheaper and more brilliant conceived SBC (more specifications here):
– Broadcom BCM2835 SoC full HD multimedia applications processor
– 700 MHz Low Power ARM1176JZ-F Applications Processor
– Dual Core VideoCore IV® Multimedia Co-Processor
– 256/512 MB SDRAM
One simple example:
C++ is a very powerful but complex programming language, so I think that the better approach is to start step by step, from the most easy features (yes, C++ could be used in an easy way) to the most advanced features it offers. What is C++? I will quote (I try to not explain anything that already is explained), Stroustrup, “his father”, from his book The C++ programming language 3th Edition:
“C++ is a general-purpose programming language with a bias towards systems programming that
– is a better C,
– supports data abstraction,
– supports object-oriented programming, and
– supports generic programming.”
And wikipedia:
C++ (pronounced “see plus plus”) is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features.[3] Developed by Bjarne Stroustrup starting in 1979 at Bell Labs, it adds object oriented features, such as classes, and other enhancements to the C programming language.
If you want more C++ links, these found at JUCE will help you.
Free books and documents:
– Maintain stability and compatibility with C++98 and possibly with C;
– Improve C++ to facilitate systems and library design, rather than to introduce new features useful only to specific applications;
– Increase type safety by providing safer alternatives to earlier unsafe techniques;
– Increase performance and the ability to work directly with hardware
Books:
Advancing:
In robotics, and embedded programming in general, we will need some advanced knowledge and practices to reach our goals.
Free books and documents:
Books:
Well, this is really the goal, robotics programming.
As this is a workshop it will follow the creation of the the walker and vehicle Hexapod showed above in the video. This is currently the core architecture and the HexaWheels module (namespace classes):
And these are the core classes:
The workshop will include:
– Basics
Like communications with serial port and wireless, using Dynamixels, sensors, … Language features for robotics, like asynchronous communications and threads and… delays!.
– Intermediate
Combination of basics features using sensors (like scanning) and servos (walking motions). For example, scanning with a DMS sensor:
As a simple example:
– Advanced
Advanced perception and behaviours
I think this could very funny, using an advanced sensor like Asus Xtion, to detect certain objects to interact, and create configurable and amusing behaviours.
CM-510 mirocontroller programming
– Tools:
PC, Raspberry Pi and Pandaboard, installation and configuration, tool and projects
– GNU C++, Boost
– Eclipse
– QT 5
The contents will come soon, very soon…
I have been searching the best UI (libraries and UI creating tools) multiplatform framework for several years, and QT 5 is the best option I have found. Its libraries are easy to use, specially the slots (UI widgets connection to code) of QT5. QT Creator is pretty easy to use and the UI designer is the free best tool I have tested. Really I’m learning to use it as creating these examples.
A very simple example:
The code uses two classes from the AXControl_v2 library:
– Configuration, it loads the parameters from the file HexaWheels.conf (currently in spanish):
Tiempo_Espera_ms=36 // wait time
Tiempo_Espera_Sensores_ms=60 // wait time for sensors
Nombre_Puerto_Serie=/dev/ttyUSB0 // serial port name
Baudios_Puerto_Serie=57600 // baud rate
– DynamixelCommunication, it offers the commands to control Dynamixel items
Connecting to the Dynamixel bus:
void MainWindow::on_B_Open_clicked() { pDynComm->open(); // open the connection using the parameters from the configuration file setConnectionButtons(true); // enable and disable conveniently the buttons }
Getting and setting the AX12 Dynamixel position:
void MainWindow::on_B_AX12_1_GET_POS_clicked() { QString qStr; int id=ui->SB_AX12_1_ID->text().toInt(); int position = pDynComm->readValue(id, MyCommunications::PresentPosition); string str=std::to_string(position); ui->E_AX12_1_POS->setText(qStr.fromStdString(str)); } void MainWindow::on_B_AX12_1_SET_POS_clicked() { QString qStr=ui->SB_AX12_1_ID->text(); int id=Util::convertToInt(qStr.toStdString()); int position= ui->E_AX12_1_POS->text().toInt(); pDynComm->sendOrder(id, MyCommunications::GoalPosition,position); }
And this is the (unfinished) main window code:
#include #include "mainwindow.h" #include "ui_mainwindow.h" #include "Configuration.h" #include "DynamixelCommunication.h" #include "Util.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); pConf=new Configuration ("/home/jose/proyectos_svn_win/trunk/bioloid/Comun/CPP/AXControl_v2/src/HexaWheels.conf"); pDynComm=new DynamixelCommunication (pConf); } MainWindow::~MainWindow() { delete ui; pDynComm->close(); delete pDynComm; delete pConf; } void MainWindow::setConnectionButtons(bool onOff) { ui->B_Open->setEnabled(!onOff); ui->B_Beep->setEnabled(onOff); ui->B_Close->setEnabled(onOff); } void MainWindow::on_B_Open_clicked() { //QString output="Open it"; //QMessageBox::information(this, "Output", output, QMessageBox::Ok); //dynComm.open("/dev/ttyUSB0",57600); pDynComm->open(); setConnectionButtons(true); } void MainWindow::on_B_Beep_clicked() { pDynComm->sendOrder(200, MyCommunications::BeepCM510, 5); } void MainWindow::on_B_Close_clicked() { pDynComm->close(); setConnectionButtons(false); QString output="Port closed!"; QMessageBox::information(this, "Output", output, QMessageBox::Ok); } void MainWindow::on_B_AX12_1_GET_POS_clicked() { QString qStr; int id=ui->SB_AX12_1_ID->text().toInt(); int position = pDynComm->readValue(id, MyCommunications::PresentPosition); string str=std::to_string(position); ui->E_AX12_1_POS->setText(qStr.fromStdString(str)); } void MainWindow::on_B_AX12_1_SET_POS_clicked() { QString qStr=ui->SB_AX12_1_ID->text(); int id=Util::convertToInt(qStr.toStdString()); int position= ui->E_AX12_1_POS->text().toInt(); pDynComm->sendOrder(id, MyCommunications::GoalPosition,position); }
But it’s “growing”:
I have uploaded the source and binary of this work in progress to
https://www.box.com/s/mdsdeoem0gg4o2rapipj/s/mdsdeoem0gg4o2rapipj