- Notifications
You must be signed in to change notification settings - Fork0
MisTurtle/DobotNet
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
DobotNet is an Arduino library that enables the control of severalDobot Magician instances from a singleArduino Mega Board.
It was developped for a school project during the first semester of 2023, with the goal of showcasing the possibilities behind the collaboration between robots, even educational ones.
- Easy-to-use API
- Supports bothHardware andSoftware serials
- Includes 8 bits to manage an instance's state without additional code
- Callback system for an easy handling of a dobot's responses
You can easily add theDobotNet library right now to your Arduino project bydownloading a zipped version of this repository. Once it is done, you can hope in your Arduino IDE and go toSketch > Include Library > Add .zip Library
and double-click on theDobotNet.zip
file in yourDownloads
folder.
The following code snippets (and more) are available in theexamples
folder of this repository
The following program controls oneDobot Magician device whose TX and RX communication pins are respectively connected to the RX1 and TX1 pins on the Arduino Board
#include"DobotNet.h"// Serial port on which the Dobot will be connectedHardwareSerialWrapper dobotSerial{&Serial1};// Instantiate a Dobot instance of ID 0 that uses Serial 1DobotInstance dobot{&dobotSerial,0};// Loop countint count =0;voidsetup(){Serial.begin(9600);// Initialize the dobot network with 1 dobotDobotNet::Init(&dobot,1);// Send movement speed and acceleration parameters to the dobotsend_movement_parameters();}voidsend_movement_parameters(){dobot.SetPTPCoordinateParams(100,100,80,80,true);// XYZ moving speeddobot.SetPTPCommonParams(50,50,true);// RatiosDobotNet::Tick(nullptr);}voidloop(){float x;if(count++ %2) x =200;else x =300;// Move to a given point using the linear moving modedobot.MoveTo(MOVL_XYZ, x,0,50,0);// Flush all packets to every dobot connected to the board (here, one dobot)// `nullptr` indicates that no callback function needs to be performed when receiving the dobot's answerDobotNet::Tick(nullptr);// Wait 3 secondsdelay(3000);}
Similarly, the following program performs the same action as the one before, but on two Dobots at the same time. One should be plugged to the RX1 and TX1 pins, while the other should be on pins 12 and 13 of the Mega board.
#include"DobotNet.h"#include<SoftwareSerial.h>// Emulate Serial port on pins RX: 12 and TX: 13SoftwareSerial Serial5{12,13};// Serial ports on which the Dobots will be connectedHardwareSerialWrapper dobotS1{&Serial1};SoftwareSerialWrapper dobotS2{&Serial5};// Instantiate two Dobot instancesDobotInstance dobots[2] = {{&dobotS1,0}, {&dobotS2,1}};// Loop countint count =0;voidsetup(){Serial.begin(9600);// Initialize the dobot network with 2 dobotsDobotNet::Init(dobots,2);// Send movement speed and acceleration parameters to the dobotsend_movement_parameters();}voidsend_movement_parameters(){for(auto & dobot : dobots){dobot.SetPTPCoordinateParams(100,100,80,80,true);// XYZ moving speeddobot.SetPTPCommonParams(50,50,true);// Ratios}DobotNet::Tick(nullptr);}voidloop(){float x;if(count++ %2) x =200;else x =300;// Move to a given point using the linear moving modefor(auto & dobot: dobots) dobot.MoveTo(MOVL_XYZ, x,0,50,0);// Flush all packets to every dobot connected to the board (here, two dobots)// `nullptr` indicates that no callback function needs to be performed when receiving a dobot's answerDobotNet::Tick(nullptr);// Wait 3 secondsdelay(3000);}
In the context of this library, callbacks can be used to retrieve and treat informations provided by a dobot instance.Whether it is to check if a dobot is still connected, to get its position, its alarm state etc..., callbacks are the way to go here.
The following example will make the two dobots move only if they are both connected. If at any point a dobot gets disconnected from the board, the move action will not be sent.
#include"DobotNet.h"#include<SoftwareSerial.h>// Emulate Serial port on pins RX: 12 and TX: 13SoftwareSerial Serial5{12,13};// Serial ports on which the Dobots will be connectedHardwareSerialWrapper dobotS1{&Serial1};SoftwareSerialWrapper dobotS2{&Serial5};// Instantiate two Dobot instancesDobotInstance dobots[2] = {{&dobotS1,0}, {&dobotS2,1}};// Loop countint loopCount =0;// Connected dobot countint connectedDobots =0;voidHandleDobotRx(uint8_t dobotId, Message* msg){// Called when a response is received from a dobotif(msg->id == ProtocolGetPose) ++connectedDobots;}// List containing callback functions for each dobot// Can be different for each dobotDobotResponseCallback callbacks[2] = {HandleDobotRx, HandleDobotRx};voidsetup(){Serial.begin(9600);// Initialize the dobot network with 2 dobotsDobotNet::Init(dobots,2);}voidSendMovePacket(){float x;if(loopCount++ %2) x =200;else x =300;// Move to a given point using the linear moving modefor(auto & dobot: dobots) dobot.MoveTo(MOVL_XYZ, x,0,50,0);}voidloop(){if(connectedDobots ==2)SendMovePacket();connectedDobots =0;// Send a packet to check if both dobots respond upon receiving itfor(auto & dobot: dobots) dobot.SendGetterProtocol(ProtocolGetPose,true);// Flush all packets to every dobot connected to the board (here, two dobots)// If both dobots are connected, both the Move packet and GetPose packet will be sent// Otherwise, only the GetPose packet will be passedDobotNet::Tick(callbacks);// Wait 3 secondsdelay(3000);}
To take a look at the final repository for a project made using this library (Python, C++),click here