小鲤鱼2013 发表于 2016-6-11 11:28:35

网上买的shieldbot小车,做的循迹小车为什么往后跑。。。

求大神分析一下原因,本来想用这个来做毕业设计的,急死了
网上买的shieldbot小车,做的循迹小车为什么往后跑。。。用的Arduino Uno控制器,5路灰度传感器,L298P驱动芯片。本来用的ARDUINO里面的循迹程序,小车往后跑,后来自己也改了一下,程序都弄好之后,循迹的时候小车还是往后跑,直接跑偏轨道啊。。。新手实在没办法,
程序如下;
Arduino IDE界面中的程序:
#include <ShieldbotV1_0.h>

Shieldbot shieldbot = Shieldbot();        //decares a Shieldbot object
int S1,S2,S3,S4,S5;        //values to store state of sensors

void setup()
{
Serial.begin(9600);//Begin serial comm for debugging
shieldbot.setMaxSpeed(200,200);//255 is max, if one motor is faster than another, adjust values
}

void loop()
{
//Read all the sensors
S1 = shieldbot.readS1();//Read the first infrared sensor value,and assigned the value to S1
S2 = shieldbot.readS2();//Read the second infrared sensor value,and assigned the value to S2
S3 = shieldbot.readS3();//Read the third infrared sensor value,and assigned the value to S3
S4 = shieldbot.readS4();//Read the fourth infrared sensor value,and assigned the value to S4
S5 = shieldbot.readS5();//Read the fifth infrared sensor value,and assigned the value to S5

//Note about IR sensors
//if a sensor sees HIGH, it means that it just sees a reflective surface background(ex. whie)
//if a sensor sees LOW, it means that it sees a non-reflective surface or empty space (ex. black tape line, or empty space off ledge)

if(S1 == LOW && S5 == LOW)
{        //if both the two outer IR line sensors see empty space (like edge of a table) stop moving
      shieldbot.stop();
      delay(100);
}
else if(S1 == HIGH && S2 == HIGH &&S3 == HIGH &&S4 == HIGH &&S5 == HIGH)
{    //if all the IR line sensors see white paper,just to stop it
   shieldbot.stop();
      delay(100);
}
else if(S1 == LOW)
{        //if the most right IR line sensors see black tape, turn right
    shieldbot.drive(80,0);// to turn right, left motor goes forward and right stop
    delay(50);
}
else if(S2== LOW)
{    //if the second IR line sensors see black tape, turn right
    shieldbot.drive(80,40);// to turn right a little, left motor goes forward fast than right a little
    delay(50);
}
else if(S5 == LOW)
{        //ifthemost left IR line sensors see black , turn left
    shieldbot.drive(0,80);// to turn left, right motor goes forward and left motor stop
    delay(50);
}
else if(S4==LOW)
{   //ifthe fourth IR line sensors see black , turn left
    shieldbot.drive(40,80);// to turn left, right motor goes forward fast than left motor a little
    delay(50);
}
else        //otherwise just go forward
    shieldbot.drive(80,80);
}

下面是ShieldbotV1_0.h库中的程序
// Only modify this file to include
// - function definitions (prototypes)
// - include files
// - extern variable definitions
// In the appropriate section

#ifndef _ShieldbotV1_0_H_
#define _ShieldbotV1_0_H_
#include "Arduino.h"
//add your includes for the project ShiledbotV1_0 here


//#define SHIELDBOTDEBUG 0
// for shieldbot Version 0.9 and 1.0

#define right1 5                        //define I1 interface
#define speedPinRight 6        //enable right motor (bridge A)
#define right2 7                      //define I2 interface
#define left1 8                      //define I3 interface
#define speedPinLeft 9        //enable motor B
#define left2 10                   //define I4 interface

// for shieldbot Version 1.1, we changed to the driver pins ,
//if you shieldbot is ver1.0 or 0.9, please using the above pin mapping;
/*#define right1 8                        //define I1 interface
#define speedPinRight 9         //enable right motor (bridge A)
#define right2 11                      //define I2 interface
#define left1 12                      //define I3 interface
#define speedPinLeft 10        //enable motor B
#define left2 13                    //define I4 interface */

#define finder1 A0                   //define line finder S1
#define finder2 A1                   //define line finder S2
#define finder3 A2                   //define line finder S3
#define finder4 A3                   //define line finder S4
#define finder5 4                    //define line finder S5

extern int speedmotorA; //define the speed of motorA
extern int speedmotorB; //define the speed of motorB
externint k,n;
externuint8_t flag;



//end of add your includes here
#ifdef __cplusplus
extern "C" {
#endif
void loop();
void setup();
#ifdef __cplusplus
} // extern "C"
#endif

//add your function definitions for the project Shiledbot here
// library interface description

class Shieldbot
{
// user-accessible "public" interface
public:
        Shieldbot();
        int readS1();
        int readS2();
        int readS3();
        int readS4();
        int readS5();
        void setMaxSpeed(int);
        void setMaxSpeed(int, int);
        void setMaxLeftSpeed(int);
        void setMaxRightSpeed(int);
        void rightMotor(char);
        void leftMotor(char);
        void drive(char, char);
        void forward();
        void backward();
        void stop();
        void stopRight();
        void stopLeft();
        void fastStopLeft();
        void fastStopRight();
        void fastStop();

};

下面是ShieldbotV1_0.cpp库中的程序:
// Do not remove the include below
#include "ShieldbotV1_0.h"

int speedmotorA = 255; //define the speed of motorA
int speedmotorB = 255; //define the speed of motorB

Shieldbot::Shieldbot()
{
pinMode(right1,OUTPUT);
pinMode(right2,OUTPUT);
pinMode(speedPinRight,OUTPUT);
pinMode(left1,OUTPUT);
pinMode(left2,OUTPUT);
pinMode(speedPinLeft,OUTPUT);
pinMode(finder1,INPUT);
pinMode(finder2,INPUT);
pinMode(finder3,INPUT);
pinMode(finder4,INPUT);
pinMode(finder5,INPUT);
}

//sets same max speed to both motors
void Shieldbot::setMaxSpeed(int both){
setMaxLeftSpeed(both);
setMaxRightSpeed(both);
}

void Shieldbot::setMaxSpeed(int left, int right){
setMaxLeftSpeed(left);
setMaxRightSpeed(right);
}

void Shieldbot::setMaxLeftSpeed(int left){
speedmotorB=left;
}

void Shieldbot::setMaxRightSpeed(int right){
speedmotorA=right;
}

int Shieldbot::readS1(){
return digitalReadA(finder1);
}

int Shieldbot::readS2(){
return digitalRead(finder2);
}

int Shieldbot::readS3(){
return digitalRead(finder3);
}

int Shieldbot::readS4(){
return digitalRead(finder4);
}

int Shieldbot::readS5(){
return digitalRead(finder5);
}

void Shieldbot::drive(char left, char right){
rightMotor(right);
leftMotor(left);
}

//char is 128 to 127
//mag is the direction to spin the right motor
//-128 backwards all the way
//0 dont move
//127 forwards all the way
void Shieldbot::rightMotor(char mag){
int actualSpeed = 0;
if(mag >0){ //forward
    float ratio = (float)mag/128;
    actualSpeed = (int)(ratio*speedmotorA); //define your speed based on global speed
        #if SHIELDBOTDEBUG
      Serial.print("forward right: ");
      Serial.println(actualSpeed);
        #endif
    analogWrite(speedPinRight,actualSpeed);
    digitalWrite(right1,LOW);
    digitalWrite(right2,HIGH);//turn right motor clockwise
}else if(mag == 0){ //neutral
        #if SHIELDBOTDEBUG
      Serial.println("nuetral right");
        #endif
        stopRight();
}else { //meaning backwards
    float ratio = (float)abs(mag)/128;
    actualSpeed = ratio*speedmotorA;
        #if SHIELDBOTDEBUG
      Serial.print("backward right: ");
      Serial.println(actualSpeed);
        #endif
    analogWrite(speedPinRight,actualSpeed);
    digitalWrite(right1,HIGH);
    digitalWrite(right2,LOW);//turn right motor counterclockwise
}
}

//TODO shouldnt these share one function and just input the differences?
void Shieldbot::leftMotor(char mag){
int actualSpeed = 0;
if(mag >0)
{ //forward
    float ratio = (float)(mag)/128;
    actualSpeed = (int)(ratio*speedmotorB); //define your speed based on global speed
        #if SHIELDBOTDEBUG
      Serial.print("forward left: ");
      Serial.println(actualSpeed);
        #endif
    analogWrite(speedPinLeft,actualSpeed);
    digitalWrite(left1,LOW);
    digitalWrite(left2,HIGH);//turn left motor counter-clockwise
}
else if(mag == 0)
{ //neutral
        #if SHIELDBOTDEBUG
      Serial.println("nuetral left");
        #endif
        stopLeft();
}
else
{ //meaning backwards
    float ratio = (float)abs(mag)/128;
    actualSpeed = ratio*speedmotorB;
        #if SHIELDBOTDEBUG
      Serial.print("backward left: ");
      Serial.println(actualSpeed);
        #endif
    analogWrite(speedPinLeft,actualSpeed);
    digitalWrite(left1,HIGH);
    digitalWrite(left2,LOW);//turn left motor counterclockwise
}
}

void Shieldbot::forward(){
leftMotor(127);
rightMotor(127);
}

void Shieldbot::backward(){
leftMotor(-128);
rightMotor(-128);
}

void Shieldbot::stop(){
stopLeft();
stopRight();
}

void Shieldbot::stopLeft(){
analogWrite(speedPinLeft,0);
}

void Shieldbot::stopRight(){
analogWrite(speedPinRight,0);
}

//may be dangerous to motor if reverse current into hbridge exceeds 2A
void Shieldbot::fastStopLeft(){
digitalWrite(left1,LOW);
digitalWrite(left2,LOW);//turn DC Motor B move clockwise
}

//may be dangerous to motor if reverse current into hbridge exceeds 2A
void Shieldbot::fastStopRight(){
digitalWrite(right1,LOW);
digitalWrite(right2,LOW);//turn DC Motor A move anticlockwise
}

//may be dangerous to motor if reverse current into hbridge exceeds 2A
void Shieldbot::fastStop(){
        fastStopRight();
        fastStopLeft();
}
求大神分析一下!

xinhoujue 发表于 2016-6-13 00:22:04

不是没人懂,是你粗心把线接错了!程序没问题的!!你模电基础差,有待提高!!!

小鲤鱼2013 发表于 2016-6-11 11:32:57

下面是ShieldbotV1_0.cpp库中的程序:
// Do not remove the include below
#include "ShieldbotV1_0.h"

int speedmotorA = 255; //define the speed of motorA
int speedmotorB = 255; //define the speed of motorB

Shieldbot::Shieldbot()
{
pinMode(right1,OUTPUT);
pinMode(right2,OUTPUT);
pinMode(speedPinRight,OUTPUT);
pinMode(left1,OUTPUT);
pinMode(left2,OUTPUT);
pinMode(speedPinLeft,OUTPUT);
pinMode(finder1,INPUT);
pinMode(finder2,INPUT);
pinMode(finder3,INPUT);
pinMode(finder4,INPUT);
pinMode(finder5,INPUT);
}

//sets same max speed to both motors
void Shieldbot::setMaxSpeed(int both){
setMaxLeftSpeed(both);
setMaxRightSpeed(both);
}

void Shieldbot::setMaxSpeed(int left, int right){
setMaxLeftSpeed(left);
setMaxRightSpeed(right);
}

void Shieldbot::setMaxLeftSpeed(int left){
speedmotorB=left;
}

void Shieldbot::setMaxRightSpeed(int right){
speedmotorA=right;
}

int Shieldbot::readS1(){
return digitalReadA(finder1);
}

int Shieldbot::readS2(){
return digitalRead(finder2);
}

int Shieldbot::readS3(){
return digitalRead(finder3);
}

int Shieldbot::readS4(){
return digitalRead(finder4);
}

int Shieldbot::readS5(){
return digitalRead(finder5);
}

void Shieldbot::drive(char left, char right){
rightMotor(right);
leftMotor(left);
}

//char is 128 to 127
//mag is the direction to spin the right motor
//-128 backwards all the way
//0 dont move
//127 forwards all the way
void Shieldbot::rightMotor(char mag){
int actualSpeed = 0;
if(mag >0){ //forward
    float ratio = (float)mag/128;
    actualSpeed = (int)(ratio*speedmotorA); //define your speed based on global speed
      #if SHIELDBOTDEBUG
      Serial.print("forward right: ");
      Serial.println(actualSpeed);
      #endif
    analogWrite(speedPinRight,actualSpeed);
    digitalWrite(right1,LOW);
    digitalWrite(right2,HIGH);//turn right motor clockwise
}else if(mag == 0){ //neutral
      #if SHIELDBOTDEBUG
      Serial.println("nuetral right");
      #endif
      stopRight();
}else { //meaning backwards
    float ratio = (float)abs(mag)/128;
    actualSpeed = ratio*speedmotorA;
      #if SHIELDBOTDEBUG
      Serial.print("backward right: ");
      Serial.println(actualSpeed);
      #endif
    analogWrite(speedPinRight,actualSpeed);
    digitalWrite(right1,HIGH);
    digitalWrite(right2,LOW);//turn right motor counterclockwise
}
}

//TODO shouldnt these share one function and just input the differences?
void Shieldbot::leftMotor(char mag){
int actualSpeed = 0;
if(mag >0)
{ //forward
    float ratio = (float)(mag)/128;
    actualSpeed = (int)(ratio*speedmotorB); //define your speed based on global speed
      #if SHIELDBOTDEBUG
      Serial.print("forward left: ");
      Serial.println(actualSpeed);
      #endif
    analogWrite(speedPinLeft,actualSpeed);
    digitalWrite(left1,LOW);
    digitalWrite(left2,HIGH);//turn left motor counter-clockwise
}
else if(mag == 0)
{ //neutral
      #if SHIELDBOTDEBUG
      Serial.println("nuetral left");
      #endif
      stopLeft();
}
else
{ //meaning backwards
    float ratio = (float)abs(mag)/128;
    actualSpeed = ratio*speedmotorB;
      #if SHIELDBOTDEBUG
      Serial.print("backward left: ");
      Serial.println(actualSpeed);
      #endif
    analogWrite(speedPinLeft,actualSpeed);
    digitalWrite(left1,HIGH);
    digitalWrite(left2,LOW);//turn left motor counterclockwise
}
}

void Shieldbot::forward(){
leftMotor(127);
rightMotor(127);
}

void Shieldbot::backward(){
leftMotor(-128);
rightMotor(-128);
}

void Shieldbot::stop(){
stopLeft();
stopRight();
}

void Shieldbot::stopLeft(){
analogWrite(speedPinLeft,0);
}

void Shieldbot::stopRight(){
analogWrite(speedPinRight,0);
}

//may be dangerous to motor if reverse current into hbridge exceeds 2A
void Shieldbot::fastStopLeft(){
digitalWrite(left1,LOW);
digitalWrite(left2,LOW);//turn DC Motor B move clockwise
}

//may be dangerous to motor if reverse current into hbridge exceeds 2A
void Shieldbot::fastStopRight(){
digitalWrite(right1,LOW);
digitalWrite(right2,LOW);//turn DC Motor A move anticlockwise
}

//may be dangerous to motor if reverse current into hbridge exceeds 2A
void Shieldbot::fastStop(){
      fastStopRight();
      fastStopLeft();
}
我应该改哪里才能让小车往前走啊

小鲤鱼2013 发表于 2016-6-11 13:41:06

好像没人懂。。。:dizzy:

zzk22510 发表于 2016-6-12 10:15:03

把电机的接线调换一下。

JKM123 发表于 2016-9-7 18:09:05

电机转反了只要把A,B相反接,还有库文件直接一个压缩包嘛

mep 发表于 2016-9-7 18:48:39

xinhoujue 发表于 2016-6-13 00:22
不是没人懂,是你粗心把线接错了!程序没问题的!!你模电基础差,有待提高!!!

这已经不能用基础差来解释了:lol
不过在论坛问问题抓住了重点:)
页: [1]
查看完整版本: 网上买的shieldbot小车,做的循迹小车为什么往后跑。。。