DobotDemoV2.0--》DobotDemoForArduino--》DobotDemo--》DobotDemo.ino
兩個arduino的rx tx交叉連接,同時兩個arduino通過usb連接在pc上,用pc向其中一個arduino發送一個數字(稱此arduino爲主機),主機將此數字通過tx串口發送給另一個arduino(稱此arduono爲從機),然後從機通過rx串口接收?
串口不夠用可以考慮使用SoftwareSerial
一、連線
1、dobot連接電源
2、mega 2560板子upload程序DobotDemo.ino
3、dobo 的Rx連接mega 2560板子的Tx1,GND連接起來,(板子的Rx1可以不用和Dobot的Tx相連)打開dobot開關可以看到dobot動起來了。如果usb連着電腦mega板子上面的tx燈會亮,如果mega板子用電源供電,未用usb連着電腦tx燈不會亮
板子上Tx1連接Dobot Rx
板子上Tx1連接Dobot Rx,Rx1連接Dobot Tx
二、setup()
1、 Serial.begin(115200); Arduino菜鳥通俗版解讀系列(4)串口通信---USART
2、Serial1.begin(115200);
3、 printf_begin(); 在Arduino上使用printf格式化輸出到串口
4、FlexiTimer2::set(100,Serialread); FlexiTimer2.h 定時器 使用
FlexiTimer2::start(); 和delay(100)功能相同,但用delay時mcu會被佔用,只能傻等在那兒,使用定時器中斷可以在這100ms同時做其他的事情,隔100ms來執行一下Serialread函數。
每隔100ms執行1次Serialread函數:讀Serial1的數據病存入
void Serialread(){
while(Serial1.available()) {
uint8_t data = Serial1.read(); mega的Rx從dobot讀到的數據
if (RingBufferIsFull(&gSerialProtocolHandler.rxRawByteQueue) == false) {
RingBufferEnqueue(&gSerialProtocolHandler.rxRawByteQueue, &data);
} }}
RingBuffer.h:RingBufferIsFull,RingBufferEnqueue
Protocol.h:gSerialProtocolHandler
ProtocolDef.h:rxRawByteQueue
執行loop期間隔100ms會去執行1次Serialread函數,所以以上這些參數的初始化都在loop的: 2、ProtocolInit()中
二、loop()
執行loop期間隔100ms會去執行1次Serialread函數
1、InitRAM(); 本程序中的函數,進行一些參數的初始化
2、 ProtocolInit(); 見Protocol.cpp
3、設置dobot的運動參數
command.cpp:幾個Set...把gJOGJointParams存到Message對象的指令隊列中
然後通過message.cpp和doBot進行通信,控制doBot
SetJOGJointParams(&gJOGJointParams, true, &gQueuedCmdIndex);
SetJOGCoordinateParams(&gJOGCoordinateParams, true, &gQueuedCmdIndex);
SetJOGCommonParams(&gJOGCommonParams, true, &gQueuedCmdIndex);
printf("\r\n======Enter demo application======\r\n");
SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
for(; ;)
{
static uint32_t timer = millis();
static uint32_t count = 0;
#ifdef JOG_STICK
if(millis() - timer > 1000)
{
timer = millis();
count++;
switch(count){
case 1:
gJOGCmd.cmd = AP_DOWN;
gJOGCmd.isJoint = JOINT_MODEL;
SetJOGCmd(&gJOGCmd, true, &gQueuedCmdIndex);
break;
case 2:
gJOGCmd.cmd = IDEL;
gJOGCmd.isJoint = JOINT_MODEL;
SetJOGCmd(&gJOGCmd, true, &gQueuedCmdIndex);
break;
case 3:
gJOGCmd.cmd = AN_DOWN;
gJOGCmd.isJoint = JOINT_MODEL;
SetJOGCmd(&gJOGCmd, true, &gQueuedCmdIndex);
break;
case 4:
gJOGCmd.cmd = IDEL;
gJOGCmd.isJoint = JOINT_MODEL;
SetJOGCmd(&gJOGCmd, true, &gQueuedCmdIndex);
break;
default:
count = 0;
break;
}
}
#else
if(millis() - timer > 3000)
{
timer = millis();
count++;
if(count & 0x01)
{
gPTPCmd.x += 100;
SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
}
else
{
gPTPCmd.x -= 100;
SetPTPCmd(&gPTPCmd, true, &gQueuedCmdIndex);
}
}
#endif
ProtocolProcess();
}