![](https://img.haomeiwen.com/i1416675/cd3099ea50fdef3a.png)
DobotDemoV2.0--》DobotDemoForArduino--》DobotDemo--》DobotDemo.ino
串口不够用可以考虑使用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灯不会亮
![](https://img.haomeiwen.com/i1416675/01ebac85d7732278.png)
![](https://img.haomeiwen.com/i1416675/7e6f0d608e44cb27.png)
![](https://img.haomeiwen.com/i1416675/830eb2704edee59a.png)
4、因megapi pro板子的Serial被USB口占用,只能使用Rx2,Tx2或者Rx3,Tx3需要把DobotDemo.ino文件和Protocol.cpp的方法ProtocolProcess中的Serial1改为2或者3即可
二、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();
}
网友评论