|
发表于 2016-6-18 11:20:32
|
显示全部楼层
IN1接P36,IN2接P37,IN3接P21,IN4接P33,ENA和ENB接5V,在51duino板上随意找一个5V接上。
然后修改51duino_SDK_V2.0的2个地方的代码:
Motor.h:
- /*
- 版权声明:
- 深圳市小二极客科技(小R科技)旗下:WIFI机器人网·机器人创意工作室版权所有 www.wifi-robots.com
- 您可以任意修改本程序,并应用于自行研发的智能小车机器人及其他电子产品上,但是禁止用于商业牟利。
- 小R科技保留付诸法律起诉侵权的权利!2015-8-22
- By WIFI机器人网·机器人创意工作室
- */
- #ifndef _MOTOR_H_
- #define _MOTOR_H_
- #include "type.h"
- #include "config.h"
- void Motor_Init(void);//声明电机驱动函数
- void Delay_ForBarrier(uint32);//声明延时函数
- void Cruising_Mod(void);//全功能选择模式
- #define MOTOR_GO_FORWARD MOTOR_A_CON1=1;MOTOR_A_CON2=0;MOTOR_B_CON1=1;MOTOR_B_CON2=0;LED1=0;LED2=1;LED3=1;LED4=1 //车体前进
- #define MOTOR_GO_BACK MOTOR_A_CON1=0;MOTOR_A_CON2=1;MOTOR_B_CON1=0;MOTOR_B_CON2=1;LED1=1;LED2=0;LED3=1;LED4=1 //车体后退
- #define MOTOR_GO_RIGHT MOTOR_A_CON1=0;MOTOR_A_CON2=1;MOTOR_B_CON1=1;MOTOR_B_CON2=0;LED1=1;LED2=1;LED3=0;LED4=1 //车体左转
- #define MOTOR_GO_LEFT MOTOR_A_CON1=1;MOTOR_A_CON2=0;MOTOR_B_CON1=0;MOTOR_B_CON2=1;LED1=1;LED2=1;LED3=1;LED4=0 //车体右转
- #define MOTOR_GO_STOP MOTOR_A_CON1=0;MOTOR_A_CON2=0;MOTOR_B_CON1=0;MOTOR_B_CON2=0;LED1=1;LED2=1;LED3=1;LED4=1 //车体停止
- #define MOTOR2_GO_FORWARD Beep=1;Input_Detect0=0
- #define MOTOR2_GO_BACKWARD Beep=0;Input_Detect0=1
- #define MOTOR2_GO_STOP Beep=0;Input_Detect0=0
- #define MOTOR3_GO_FORWARD Input_Detect1=1;Input_Detect2=0
- #define MOTOR3_GO_BACKWARD Input_Detect1=0;Input_Detect2=1
- #define MOTOR3_GO_STOP Input_Detect1=0;Input_Detect2=0
- #endif
复制代码 Uart.c:- /*
- 版权声明:
- 深圳市小二极客科技(小R科技)旗下:WIFI机器人网·机器人创意工作室版权所有 www.wifi-robots.com
- 您可以任意修改本程序,并应用于自行研发的智能小车机器人及其他电子产品上,但是禁止用于商业牟利。
- 小R科技保留付诸法律起诉侵权的权利!2015-8-22
- By WIFI机器人网·机器人创意工作室
- */
- #include "uart.h"
- #include <stdio.h>
- #include "motor.h"
- #include "steer.h"
- #include "mem.h"
- #include "IR.h"
- #include "timer.h"
- #include "Servo.h"
- #include "Light.h"
- uint8 xdata buffer[3];
- uint8 rec_flag=0; //等于0等待接受 等于1正在接受
- extern uchar IR_Type;
- extern uchar IR_Num;
- extern uchar Cruising_Flag;
- extern uchar ServoConfig_Flag;
- extern uint16 Uart_timeout_count;
- extern uint8 Left_Speed; //左侧速度
- extern uint8 Right_Speed; //右侧速度
- extern uint8 Left_Speed_Hold; //左侧速度锁存
- extern uint8 Right_Speed_Hold; //右侧速度锁存
- void UART_init(void)
- {
- PCON |= 0x80; //使能波特率倍速位SMOD
- SCON = 0x50; //8位数据,可变波特率
- BRT = RELOAD_COUNT; //设定独立波特率发生器重装值
- AUXR |= 0x04; //独立波特率发生器时钟为Fosc,即1T
- AUXR |= 0x01; //串口1选择独立波特率发生器为波特率发生器
- AUXR |= 0x10; //启动独立波特率发生器
- ES = 1; //允许串口中断
- EA = 1; //开总中断
- TI = 1;
- EX0=0;
- EX1=0;
- }
- void UART_send_byte(uint8 byte)
- {
- ES = 0; //关串口中断
- TI = 0; //清零串口发送完成中断请求标志
- SBUF = byte;
- while(TI ==0); //等待发送完成
- TI = 1; //清零串口发送完成中断请求标志
- ES = 1; //允许串口中断
- }
- void UART_send(uint8 *Buffer, uint8 Length)
- {
- while(Length != 0)
- {
- UART_send_byte(*Buffer);
- Buffer++;
- Length--;
- }
- }
- void Communication_Decode(void)
- {
- uint8 i;
- if(buffer[0]==0x00)
- {
- switch(buffer[1])
- {
- case 0x01:Left_Speed=Left_Speed_Hold;Right_Speed=Right_Speed_Hold;MOTOR_GO_FORWARD; return;
- case 0x02:Left_Speed=Left_Speed_Hold;Right_Speed=Right_Speed_Hold;MOTOR_GO_BACK; return;
- case 0x03:Left_Speed=Left_Speed_Hold;Right_Speed=Right_Speed_Hold;MOTOR_GO_LEFT; return;
- case 0x04:Left_Speed=Left_Speed_Hold;Right_Speed=Right_Speed_Hold;MOTOR_GO_RIGHT; return;
- case 0x00:Left_Speed=Left_Speed_Hold;Right_Speed=Right_Speed_Hold;MOTOR_GO_STOP; return;
- case 0x05:Left_Speed=4;Right_Speed=10;MOTOR_GO_FORWARD;return;
- case 0x06:Left_Speed=4;Right_Speed=10;MOTOR_GO_BACK;return;
- case 0x07:Left_Speed=10;Right_Speed=4;MOTOR_GO_FORWARD;return;
- case 0x08:Left_Speed=10;Right_Speed=4;MOTOR_GO_BACK;return;
- case 0x09:MOTOR2_GO_FORWARD;return;
- case 0x0A:MOTOR2_GO_BACKWARD;return;
- case 0x0B:MOTOR2_GO_STOP;return;
- case 0x0C:MOTOR3_GO_FORWARD;return;
- case 0x0D:MOTOR3_GO_BACKWARD;return;
- case 0x0E:MOTOR3_GO_STOP;return;
- default: return;
- }
- }
- else if(buffer[0]==0x01)
- {
- if(buffer[2]>180)
- return;
-
- switch(buffer[1])
- {
- case 0x01:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(1))buffer[2]=GetServoMaxAngle(1);
- if(buffer[2]<GetServoMixAngle(1))buffer[2]=GetServoMixAngle(1);
- SetServoAngle(1,buffer[2]); return;
- }
- else
- {
- SetServoAngle(1,buffer[2]); return;//舵机角度设定模式下,不受角度约束
- }
-
- case 0x02:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(2))buffer[2]=GetServoMaxAngle(2);
- if(buffer[2]<GetServoMixAngle(2))buffer[2]=GetServoMixAngle(2);
- SetServoAngle(2,buffer[2]); return;
- }
- else
- {
- SetServoAngle(2,buffer[2]); return;
- }
- case 0x03:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(3))buffer[2]=GetServoMaxAngle(3);
- if(buffer[2]<GetServoMixAngle(3))buffer[2]=GetServoMixAngle(3);
- SetServoAngle(3,buffer[2]); return;
- }
- else
- {
- SetServoAngle(3,buffer[2]); return;
- }
- case 0x04:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(4))buffer[2]=GetServoMaxAngle(4);
- if(buffer[2]<GetServoMixAngle(4))buffer[2]=GetServoMixAngle(4);
- SetServoAngle(4,buffer[2]); return;
- }
- else
- {
- SetServoAngle(4,buffer[2]); return;
- }
- case 0x05:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(5))buffer[2]=GetServoMaxAngle(5);
- if(buffer[2]<GetServoMixAngle(5))buffer[2]=GetServoMixAngle(5);
- SetServoAngle(5,buffer[2]); return;
- }
- else
- {
- SetServoAngle(5,buffer[2]); return;
- }
- case 0x06:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(6))buffer[2]=GetServoMaxAngle(6);
- if(buffer[2]<GetServoMixAngle(6))buffer[2]=GetServoMixAngle(6);
- SetServoAngle(6,buffer[2]); return;
- }
- else
- {
- SetServoAngle(6,buffer[2]); return;
- }
- case 0x07:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(7))buffer[2]=GetServoMaxAngle(7);
- if(buffer[2]<GetServoMixAngle(7))buffer[2]=GetServoMixAngle(7);
- SetServoAngle(7,buffer[2]); return;
- }
- else
- {
- SetServoAngle(7,buffer[2]); return;
- }
- case 0x08:
- if(ServoConfig_Flag == SERVO_NORMAL_MODE)
- {
- if(buffer[2]>GetServoMaxAngle(8))buffer[2]=GetServoMaxAngle(8);
- if(buffer[2]<GetServoMixAngle(8))buffer[2]=GetServoMixAngle(8);
- SetServoAngle(8,buffer[2]); return;
- }
- else
- {
- SetServoAngle(8,buffer[2]); return;
- }
- default : return;
- }
- }
- else if(buffer[0]==0x02)
- {
- if(buffer[2]>10)
- return;
- switch(buffer[1])
- {
- case 0x01:Left_Speed_Hold=Left_Speed = buffer[2]; return;
- case 0x02:Right_Speed_Hold=Right_Speed = buffer[2]; return;
- default : return;
- }
- }
- else if (buffer[0]==0x04)//车大灯,拉电流形式,5V
- {
- if(buffer[1]==0x01)
- {
- OpenMainLight(); //开灯
- }
- else
- {
- CloseMainLight(); //关灯
- }
-
-
- }
- else if(buffer[0]==0x13)//模式切换开关
- {
- switch(buffer[1])
- {
- case 0x01: Cruising_Flag = 0x01; break;//跟随
- case 0x02: Cruising_Flag = 0x02; break;//巡线
- case 0x03: Cruising_Flag = 0x03; break;//避障
- case 0x04: Cruising_Flag = 0x04; break;//雷达避障
- case 0x00: Cruising_Flag = 0x00; break;//正常模式
- default:Cruising_Flag = 0x00; break;//正常模式
- }
- }
- else if(buffer[0]==0x32)//存储舵机上电默认角度
- {
- Earse_Section(50*512);
- for(i=1;i<=8;i++)
- {
- Write_Byte(50*512+i,GetServoAngle(i));
- }
- }
- else if(buffer[0]==0x33)//读取舵机上电默认角度并赋值
- {
- Init_Steer();
- }
- else if(buffer[0]==0x36)//进入舵机配置模式
- {
- ServoConfig_Flag=SERVO_CONFIG_MODE;
- }
- else if(buffer[0]==0x37)//从舵机配置模式切换到正常模式
- {
- ServoConfig_Flag=SERVO_NORMAL_MODE;
- }
- else if(buffer[0]==0x34) //保存最大舵机角度值
- {
- if(ServoConfig_Flag == SERVO_CONFIG_MODE)
- {
- SetServoMaxAngle();
- }
- }
- else if(buffer[0]==0x35) //保存最小舵机角度值
- {
- if(ServoConfig_Flag == SERVO_CONFIG_MODE)
- {
- SetServoMixAngle();
- }
- }
- else
- {
- return;
- }
- }
- void UART_Interrupt_Receive(void) interrupt 4
- {
- static uint8 i;
- if(RI==1)
- {
- RI = 0;
- if(rec_flag==0)
- {
- if(SBUF==0xff)
- {
- rec_flag=1;
- Uart_timeout_count = 0;
- i=0;
- }
- }
- else
- {
- if(SBUF==0xff)
- {
- rec_flag=0;
- if(i==3)
- {
- Communication_Decode();
- //UART_init(); // 解决串口死机问题
- }
- i=0;
- }
- else
- {
- buffer[i]=SBUF;
- i++;
- }
- }
- }
- else
- {
- TI = 0;
- }
-
- }
复制代码 修改后编译出固件。附件为已编译的固件,可以直接刷入51duino运行。
第三路电机正转/反转/停止指令为:FF000900FF/FF000A00FF/FF000B00FF
第四路电机正转/反转/停止指令为:FF000C00FF/FF000D00FF/FF000E00FF
可在上位机的“自定义命令”里面设置如上指令,就可以用上位机发送给下位机。
51duino_SDK.hex
(15.42 KB, 下载次数: 13)
|
|