本帖最后由 ylsmwj 于 2013-7-31 10:43 编辑
本人小白一枚,最近DIY了一个四驱小车,用的驱动板是这个
主控板用ARDUINO最小系统版制作,驱动四个直流电机,前进,后退都正常,转弯的时候是控制一侧的两个电机正传,另一侧的两个电机反转,实现转弯,就是转弯的时候感觉阻力非常大,好像后轮是被前轮硬拽过来的一样,后来在坛子里看到一个帖子说小车的轴距过大会出现该问题(原帖地址:轮式小车[轮距]与[轴距]关系的简单分析),于是重新安装四个电机,将轴距缩成最小,该问题有所改善,但还是比较明显,我看到网上别人做的四驱小车视频转弯非常流畅毫无阻塞,甚至可以原地旋转,而且从视频中看,别人的小车轴距也不是特别短。
这是小车照片,因为还在测试阶段,所以找了块塑料版充当地盘,电机原来是按照长的那个方向安装的,后来为了缩小轴距把板子横了过来
下面是原程序,各位大大帮我看看问题出在什么地方?
此程序由OPENJUMER的小车控制程序修改而来
- String inString = ""; // string to hold input
- String inStr = "";
- int i;
- /*------------------------------------------------------------------
- define motor
- ------------------------------------------------------------------*/
- #define FORWARD 1
- #define BACKWARD 2
- #define BRAKE 3
- #define RELEASE 4
- int motor_speed = 200; //[modifid]motor speed 150-200,---min:100;max:255
- int motor_delay = 400; //[modifid]delay time in step
- /*------------------------------------------------------------------
- main program
- ------------------------------------------------------------------*/
- void setup()
- {
- Serial.begin(9600);
- motorInit();
- b_motor_stop();
- delay(1000); //waiting time
- Serial.println("Hello! WangJun!");
- }
- void loop()
- {
- while (Serial.available() > 0)
- {
- int inChar = Serial.read();
- if (inChar != '\n')
- {
- inString += (char)inChar;
- }
- if (inChar == '\n')
- {
- wifi_bt_car();
- inString = "";
- }
- }
- }
- void wifi_bt_car() {
- int num,number;
- num = 0;
- number = 0;
- for(i=0 ;i < inString.length() ;i++ )
- {
- if(inString[i] == 'W' || inString[i] == 'w'
- || inString[i] == 'S' || inString[i] == 's'
- || inString[i] == 'A' || inString[i] == 'a'
- || inString[i] == 'D' || inString[i] == 'd'
- || inString[i] == ' ')
- {
- Serial.println(inString[i]);
- b_motor_com(inString[i]);
- i++;
- }
- }
- }
- /*------------------------------------------------------------------
- motorInit();//initial motorL&motorR;
- motorLrun(cmd,spd);//cmd for BOXZ's action,spd for motorL speed;
- motorRrun(cmd,spd);//cmd for BOXZ's action,spd for motorR speed;
- b_motor_stop();//stop motorL&motorR;
- b_motor_com(keyword);//keyboard "w,a,s,d" for action;space for stop;
- ------------------------------------------------------------------*/
- void motorInit(){
- pinMode(2,OUTPUT);//L1
- pinMode(3,OUTPUT);//L1 PWM
- pinMode(4,OUTPUT);//Control DC motor L L2
- pinMode(5,OUTPUT);//PWM to control speed of DC motor L L2
- pinMode(7,OUTPUT);//Control DC motor R R1
- pinMode(6,OUTPUT);//PWM to control speed of DC motor R R1
- pinMode(8,OUTPUT);//R2
- pinMode(9,OUTPUT);//R2 PWM
- }
- void motorLrun(int cmd,int spd){ //cmd for FORWARD BACKWARD BRAKE(stop);
- switch (cmd){ //spd for control PWM and motor speed;
- case FORWARD:
- digitalWrite(2,HIGH);
- digitalWrite(3,LOW);
- digitalWrite(4,HIGH);
- digitalWrite(5,LOW);
- //analogWrite(5,LOW);
- break;
- case BACKWARD:
- digitalWrite(2,LOW);
- digitalWrite(3,HIGH);
- digitalWrite(4,LOW);
- digitalWrite(5,HIGH);
- //analogWrite(5,spd);
- break;
- case BRAKE:
- digitalWrite(2,LOW);
- digitalWrite(3,LOW);
- digitalWrite(4,LOW);
- digitalWrite(5,LOW);
- //analogWrite(5,spd);
- break;
- default :
- return;
- }
- }
- void motorRrun(int cmd,int spd){
- switch (cmd){
- case FORWARD:
- digitalWrite(7,HIGH);
- digitalWrite(6,LOW);
- //analogWrite(6,spd);
- digitalWrite(8,HIGH);
- digitalWrite(9,LOW);
- break;
- case BACKWARD:
- digitalWrite(7,LOW);
- //analogWrite(6,spd);
- digitalWrite(6,HIGH);
- digitalWrite(8,LOW);
- digitalWrite(9,HIGH);
- break;
- case BRAKE:
- digitalWrite(7,LOW);
- digitalWrite(6,LOW);
- //analogWrite(6,spd);
- digitalWrite(8,LOW);
- digitalWrite(9,LOW);
- default :
- return;
- }
- }
- void b_motor_stop(){
- motorLrun(BRAKE,0);
- motorRrun(BRAKE,0);
- }
- void b_motor_com(int keyword){
- switch (keyword){
- case 'w':
- motorLrun(FORWARD,motor_speed);
- motorRrun(FORWARD,motor_speed);
- Serial.println("FORWARD");
- break;
- case 's':
- motorLrun(BACKWARD,motor_speed);
- motorRrun(BACKWARD,motor_speed);
- Serial.println("BACKWARD");
- break;
- case 'a':
- motorLrun(BACKWARD,motor_speed);
- motorRrun(FORWARD,motor_speed);
- Serial.println("LEFT");
- break;
- case 'd':
- motorLrun(FORWARD,motor_speed);
- motorRrun(BACKWARD,motor_speed);
- Serial.println("RITGHT");
- break;
- case 'W':
- motorLrun(FORWARD,motor_speed);
- motorRrun(FORWARD,motor_speed);
- Serial.println("StepF");
- delay(motor_delay);
- b_motor_stop();
- break;
- case 'S':
- motorLrun(BACKWARD,motor_speed);
- motorRrun(BACKWARD,motor_speed);
- Serial.println("StepB");
- delay(motor_delay);
- b_motor_stop();
- break;
- case 'A':
- motorLrun(BACKWARD,motor_speed);
- motorRrun(FORWARD,motor_speed);
- Serial.println("StepL");
- delay(motor_delay);
- b_motor_stop();
- break;
- case 'D':
- motorLrun(FORWARD,motor_speed);
- motorRrun(BACKWARD,motor_speed);
- Serial.println("StepR");
- delay(motor_delay);
- b_motor_stop();
- break;
- case ' ':
- Serial.println("STOP");
- b_motor_stop();
- break;
- default :
- Serial.println("STOP");
- b_motor_stop();
- return;
- }
- }
复制代码
|