材料 单片机、RoboModule、RM35电机
基本步骤:
注意:
1)发送指令与模式选择指令,模式选择指令与发送数据指令之间要有延时函数。
2)连续发送的时间间隔不能小于 2 毫秒
注释:
1)该片代码一共配置了3个底盘电机。DIL、DIR、DB为电机ID
2)底盘电机图示:
3)底盘电机速度
应用代码:
void D_motor_Reset()//全部底盘电机循环体内部的初始化重配置(含延时)
{
CAN_RoboModule_DRV_Reset(DIL); //对DIL复位,下同
CAN_RoboModule_DRV_Reset(DIR);
CAN_RoboModule_DRV_Reset(DB);
delay_ms(1000); //发送复位指令后的延时必须要有,等待驱动器复位完毕。
CAN_RoboModule_DRV_Config(DIL,100,0); //配置为1s传回一次数据
CAN_RoboModule_DRV_Config(DIR,100,0);
CAN_RoboModule_DRV_Config(DB,100,0);
delay_ms(1);
CAN_RoboModule_DRV_Mode_Choice(DIL,OpenLoop_Mode); //选择进入模式
CAN_RoboModule_DRV_Mode_Choice(DIR,OpenLoop_Mode);
CAN_RoboModule_DRV_Mode_Choice(DB,OpenLoop_Mode);
delay_ms(1000); //发送模式选择指令后,要等待驱动器进入模式就绪。所以延时也不可以去掉。
}
void D_sport(short x,short y,short d,short s)
{
if(d==0) s=(-s);
CAN_RoboModule_DRV_OpenLoop_Mode(x,y,s);
}
#define PI 3.1415926f
int D_move(short s,short theta) //让车子以全速的(0.02x)%,角度(0.1*theta)%度移动.注意90度为前进
{
float Vx,Vy,v1,v2,v3;
Vx=cos(0.1*(float)theta*PI/180.0)*(float)s;
Vy=sin(0.1*(float)theta*PI/180.0)*(float)s;
#define AFA 60
v1 = (float)(-(cos(AFA / 180.0f*PI) * Vx) - (sin(AFA / 180.0f*3.1415926f) * Vy));
v2 = (float)Vx;
v3 = (float)(-(cos(AFA / 180.0f *PI) * Vx) + (sin(AFA / 180.0f*3.1415926f) * Vy));
D_sport(DILD,v1);
D_sport(DIRD,v3);
D_sport(DBD,v2);
}
void CAN_RoboModule_DRV_Reset(unsigned char Group,unsigned char Number)
{
unsigned short can_id = 0x000;
CanTxMsg tx_message;
tx_message.IDE = CAN_ID_STD; //标准帧
tx_message.RTR = CAN_RTR_DATA; //数据帧
tx_message.DLC = 0x08; //帧长度为8
if((Group<=7)&&(Number<=15))
{
can_id |= Group<<8;
can_id |= Number<<4;
}
else
{
return;
}
tx_message.StdId = can_id; //帧ID为传入参数的CAN_ID
tx_message.Data[0] = 0x55;
tx_message.Data[1] = 0x55;
tx_message.Data[2] = 0x55;
tx_message.Data[3] = 0x55;
tx_message.Data[4] = 0x55;
tx_message.Data[5] = 0x55;
tx_message.Data[6] = 0x55;
tx_message.Data[7] = 0x55;
can_tx_success_flag = 0;
CAN_Transmit(CAN1,&tx_message);
while(can_tx_success_flag == 0); //如果CAN芯片是TJA1050,注释掉这个判断。
}
模式选择指令函数:
void CAN_RoboModule_DRV_Mode_Choice(unsigned char Group,unsigned char Number,unsigned char Mode)
{
unsigned short can_id = 0x001;
CanTxMsg tx_message;
tx_message.IDE = CAN_ID_STD; //标准帧
tx_message.RTR = CAN_RTR_DATA; //数据帧
tx_message.DLC = 0x08; //帧长度为8
if((Group<=7)&&(Number<=15))
{
can_id |= Group<<8;
can_id |= Number<<4;
}
else
{
return;
}
tx_message.StdId = can_id; //帧ID为传入参数的CAN_ID
tx_message.Data[0] = Mode;
tx_message.Data[1] = 0x55;
tx_message.Data[2] = 0x55;
tx_message.Data[3] = 0x55;
tx_message.Data[4] = 0x55;
tx_message.Data[5] = 0x55;
tx_message.Data[6] = 0x55;
tx_message.Data[7] = 0x55;
can_tx_success_flag = 0;
CAN_Transmit(CAN1,&tx_message);
while(can_tx_success_flag == 0); //如果CAN芯片是TJA1050,注释掉这个判断。
}
开环模式下的数据指令函数:
void CAN_RoboModule_DRV_OpenLoop_Mode(unsigned char Group,unsigned char Number,short Temp_PWM)
{
unsigned short can_id = 0x002;
CanTxMsg tx_message;
tx_message.IDE = CAN_ID_STD; //标准帧
tx_message.RTR = CAN_RTR_DATA; //数据帧
tx_message.DLC = 0x08; //帧长度为8
if((Group<=7)&&(Number<=15))
{
can_id |= Group<<8;
can_id |= Number<<4;
}
else
{
return;
}
tx_message.StdId = can_id; //帧ID为传入参数的CAN_ID
if(Temp_PWM > 5000)
{
Temp_PWM = 5000;
}
else if(Temp_PWM < -5000)
{
Temp_PWM = -5000;
}
tx_message.Data[0] = (unsigned char)((Temp_PWM>>8)&0xff);
tx_message.Data[1] = (unsigned char)(Temp_PWM&0xff);
tx_message.Data[2] = 0x55;
tx_message.Data[3] = 0x55;
tx_message.Data[4] = 0x55;
tx_message.Data[5] = 0x55;
tx_message.Data[6] = 0x55;
tx_message.Data[7] = 0x55;
can_tx_success_flag = 0;
CAN_Transmit(CAN1,&tx_message);
while(can_tx_success_flag == 0); //如果CAN芯片是TJA1050,注释掉这个判断。
}
参考资料:11-RoboModule-RMDS系列驱动器-CAN总线通信协议说明