回复
小车的经典解决方案 原创
netXeyes
发布于 2022-5-31 02:46
浏览
0收藏
@toc
一. 预备阶段
硬件:hi3861开发板等小车标配的套件,电脑
软件:VS code,openharmony 3.0,
二. 开发阶段
1.目标效果
可以实现舵机托举超声波扩展板左右旋转扫描达到避障的效果。可融入于地下隧道探测与扫地机器人的开发。
2.解决方案
主要问题:超声波与探测频率(比如说在舵机左右旋转时进行多少次探测)
代码解析:超声波模块触发信号需要大于10us的高电平,预先设定获取超声波传来信号的周期为500ms,需要将PWM信号的周期也设为500ms
PWM的频率是指每秒钟信号从高电平到低电平再回到高电平的次数
参考计算公式:T=(arr+1)(psc+1)/(time)M (单位为秒)
2.1 超声波原理如下图:
2.2 设备侧代码:
只例出pwm
void loop() {
// put your main code here, to run repeatedly:
avoidance();
}
void motorRun(int cmd,int value)
{
analogWrite(leftPWM, value); // Set PWM output, set speed
analogWrite(rightPWM, value);
switch(cmd){
case FORWARD:
Serial.println("FORWARD"); // signal output
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
break;
case BACKWARD:
Serial.println("BACKWARD"); // signal output
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, HIGH);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, HIGH);
break;
case TURNLEFT:
Serial.println("TURN LEFT"); // signal output
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, HIGH);
break;
case TURNRIGHT:
Serial.println("TURN RIGHT"); // signal output
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, HIGH);
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
break;
default:
Serial.println("STOP"); // signal output
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, LOW);
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, LOW);
}
}
void avoidance()
{
int pos;
int dis[3]; // long
motorRun(FORWARD,200);
myServo.write(90);
dis[1]=getDistance(); //middle
if(dis[1]<30)
{
motorRun(STOP,0);
for (pos = 90; pos <= 150; pos += 1)
{
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
dis[2]=getDistance(); // left
for (pos = 150; pos >= 30; pos -= 1)
{
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if(pos==90)
dis[1]=getDistance(); // middle
}
dis[0]=getDistance(); // right
for (pos = 30; pos <= 90; pos += 1)
三.成果展示
详见号主哔哩哔哩~
四.结果
测试成功~
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
已于2022-5-31 02:46:25修改
赞
2
收藏
回复
相关推荐