|
楼主 |
发表于 2016-3-17 10:25:11
|
显示全部楼层
164335413 发表于 2016-3-11 19:09
问题不好找,你后来贴的代码不全,也没看到你修改后的代码。
不好意思今天才看到,前几天课还比较多,没弄这个。问题还是这样启动后,舵机立刻转,并不是小车先走,前方设定范围内有障碍物时停下来,舵机再转 。我买了个32路舵机控制板,用来控制三个mg996。超声波模块的sg90舵机用uno控制。步进电机的analog write我删掉后发现转速太快了,也没有解决问题。所以还是加上了
int Left_motor_P=6;
int Left_motor_N=3;
int Right_motor_P=5;
int Right_motor_N=11;
int Ultrasonic_Echo = A0;
int Ultrasonic_Trig =A1;
int Front_Distance = 0;
int pos = 0;
int servopin=9;
int myangle;
int pulsewidth;
int val;
void servopulse(int servopin,int myangle)
{
pulsewidth=(myangle*11)+500;
digitalWrite(servopin,HIGH);
delayMicroseconds(pulsewidth);
digitalWrite(servopin,LOW);
delay(20-pulsewidth/1000);
}
void setup()
{
Serial.begin(115200);
pinMode(Left_motor_P,OUTPUT);
pinMode(Left_motor_N,OUTPUT);
pinMode(Right_motor_P,OUTPUT);
pinMode(Right_motor_N,OUTPUT);
pinMode(Ultrasonic_Echo, INPUT);
pinMode(Ultrasonic_Trig, OUTPUT);
pinMode(servopin,OUTPUT);
}
void go(int a)
{
digitalWrite(Right_motor_P,HIGH);
digitalWrite(Right_motor_N,LOW);
analogWrite(Right_motor_P,80);
analogWrite(Right_motor_N,0);
digitalWrite(Left_motor_P,HIGH);
digitalWrite(Left_motor_N,LOW);
analogWrite(Left_motor_P,90);
analogWrite(Left_motor_N,0);
delay(a * 100);
}
void left(int c)
{
digitalWrite(Right_motor_P,LOW);
digitalWrite(Right_motor_N,LOW);
digitalWrite(Left_motor_P,LOW);
digitalWrite(Left_motor_N,HIGH);
analogWrite(Left_motor_P,0);
analogWrite(Left_motor_N,80);
delay(c * 100);
}
void Stop (int f)
{
digitalWrite(Right_motor_P,LOW);
digitalWrite(Right_motor_N,LOW);
digitalWrite(Left_motor_P,LOW);
digitalWrite(Left_motor_N,LOW);
delay(f * 100);
}
void ask_pin_F()
{
for(int i=0;i<=25;i++) {
servopulse(servopin,90);
}
digitalWrite(Ultrasonic_Trig, LOW); // 讓超聲波發射低電壓2μs
delayMicroseconds(2);
digitalWrite(Ultrasonic_Trig, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
delayMicroseconds(10);
digitalWrite(Ultrasonic_Trig, LOW); // 維持超聲波發射低電壓
float Fdistance = pulseIn(Ultrasonic_Echo, HIGH); // 讀差相差時間
Fdistance= Fdistance/59; // 將時間轉為距離距离(單位:公分)
Front_Distance = Fdistance;
}
void loop()
{
while(1)
{
ask_pin_F();
if(Front_Distance<32)
{
Stop(1);
Serial.print("#9P2500T1000\r\n");
delay(1000);
Serial.print("#9P1500T1000\r\n");
delay(1000);
left(1);
}
else
{
go(8);
}
}
}
|
|