12674浏览
查看: 12674|回复: 11

[求助] 超声波信号控制舵机

[复制链接]
要实现:利用超声波接收的距离信号作为判断条件,让舵机程序运行,怎么处理if语句?

超声波传感器:URM37,只用Arduino UNO R3可以不用实现?

luna  初级技神

发表于 2016-12-22 16:28:48

有点没有听懂,能截图或者说详细一点吗
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-12-22 21:19:42

luna 发表于 2016-12-22 16:28
有点没有听懂,能截图或者说详细一点吗

@ 一下大神看看
回复

使用道具 举报

Rockets  NPC

发表于 2016-12-23 11:21:54

我的理解是,利用超声波的距离来控制舵机转角的控制。
这个程序不需要用if语句来完成,简单的用map函数就可以完成。
利用映射,将超声波距离和舵机转角进行连接即可。
回复

使用道具 举报

helloword  见习技师
 楼主|

发表于 2016-12-23 15:12:26


谢谢你的解答,能不能举一个具体的代码作为例子?
回复

使用道具 举报

helloword  见习技师
 楼主|

发表于 2016-12-23 16:59:41

luna 发表于 2016-12-22 16:28
有点没有听懂,能截图或者说详细一点吗


// # Editor    :Zrh from DFRobot
// # Data      :29.08.2014
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version :  1.0

// # Description:
// # The sketch for using the URM37 autonomous  mode from DFRobot  
// #   and writes the values to the serialport


// # Connection:
// #       Vcc (Arduino)      -> Pin 1 VCC (URM V4.0)
// #       GND (Arduino)      -> Pin 2 GND (URM V4.0)
// #       Pin 3 (Arduino)    -> Pin 4 ECHO (URM V4.0)
// #       Pin TX1 (Arduino)  -> Pin 8 RXD (URM V4.0)
// #       Pin RX0 (Arduino)  -> Pin 9 TXD (URM V4.0)
// # Working Mode: autonomous  mode.

int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm

unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0};    // distance measure command
   
   
    #include <Servo.h>
   Servo myservo;    // ***定义舵机对象,最多八个
   int pos=0;         // ***定义舵机转动位置

   
void setup(){                                 // Serial initialization
  Serial.begin(9600);                         // Sets the baud rate to 9600
  AutonomousMode_Setup();
  
   myservo.attach(9);  // ***设置舵机控制针脚
   
}

void loop()
{
AutonomousMode();
delay(30);
}                      //PWM mode setup function

void AutonomousMode_Setup(){
  pinMode(URECHO, INPUT);                      // Sending Enable PWM mode command
  for(int i=0;i<4;i++){
      Serial.write(EnPwmCmd);
   }
}
void AutonomousMode(){                              
    unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
    if(DistanceMeasured>=30000){              // the reading is invalid.
      Serial.print("Invalid");   
   }
    else{
      Distance=DistanceMeasured/50;           // every 50us low level stands for 1cm
      Serial.print("Distance=");
      Serial.print(Distance);
      Serial.println("cm");
   }

   
   if(Distance>=10)
   delay(10);
    for(pos = 0; pos < 40; pos += 1)  
     {  
        myservo.write(pos);  
        delay(30);
      }
      // 180到0旋转舵机,每次延时15毫秒
      for(pos = 40; pos>=1; pos-=1)
      {                              
        myservo.write(pos);
        delay(10);
  }
   
  
}


这是程序,舵机工作不受URM37超声波传感器的影响,请问大神,怎么回事呀?
回复

使用道具 举报

helloword  见习技师
 楼主|

发表于 2016-12-23 17:05:12

luna 发表于 2016-12-22 16:28
有点没有听懂,能截图或者说详细一点吗


// # Editor    :Zrh from DFRobot
// # Data      :29.08.2014
// # Product name:ultrasonic scanner
// # Product SKU:SEN0001
// # Version :  1.0

// # Description:
// # The sketch for using the URM37 autonomous  mode from DFRobot  
// #   and writes the values to the serialport


// # Connection:
// #       Vcc (Arduino)      -> Pin 1 VCC (URM V4.0)
// #       GND (Arduino)      -> Pin 2 GND (URM V4.0)
// #       Pin 3 (Arduino)    -> Pin 4 ECHO (URM V4.0)
// #       Pin TX1 (Arduino)  -> Pin 8 RXD (URM V4.0)
// #       Pin RX0 (Arduino)  -> Pin 9 TXD (URM V4.0)
// # Working Mode: autonomous  mode.

int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm

unsigned int Distance=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0};    // distance measure command
   
   
    #include <Servo.h>
   Servo myservo;    // ***定义舵机对象,最多八个
   int pos=0;         // ***定义舵机转动位置

   
void setup(){                                 // Serial initialization
  Serial.begin(9600);                         // Sets the baud rate to 9600
  AutonomousMode_Setup();
  
   myservo.attach(9);  // ***设置舵机控制针脚
   
}

void loop()
{
AutonomousMode();
delay(30);
}                      //PWM mode setup function

void AutonomousMode_Setup(){
  pinMode(URECHO, INPUT);                      // Sending Enable PWM mode command
  for(int i=0;i<4;i++){
      Serial.write(EnPwmCmd);
   }
}
void AutonomousMode(){                              
    unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
    if(DistanceMeasured>=30000){              // the reading is invalid.
      Serial.print("Invalid");   
   }
    else{
      Distance=DistanceMeasured/50;           // every 50us low level stands for 1cm
      Serial.print("Distance=");
      Serial.print(Distance);
      Serial.println("cm");
   }

   
   if(Distance>=10)
   delay(10);
    for(pos = 0; pos < 40; pos += 1)  
     {  
        myservo.write(pos);  
        delay(30);
      }
      // 180到0旋转舵机,每次延时15毫秒
      for(pos = 40; pos>=1; pos-=1)
      {                              
        myservo.write(pos);
        delay(10);
  }
   
这是我在URM37程序的基础上修改的程序,但是舵机工作不受控制,请问是怎么回事呀?
  
}
回复

使用道具 举报

gray6666  初级技神

发表于 2016-12-24 20:29:07

没遇到过,学习了
回复

使用道具 举报

helloword  见习技师
 楼主|

发表于 2016-12-26 19:36:06

gray6666 发表于 2016-12-24 20:29
没遇到过,学习了

找到问题了,控制舵机的if语句没有加大括号,尴尬:L:L
回复

使用道具 举报

多兰兰兰兰  学徒

发表于 2018-12-11 10:26:03

helloword 发表于 2016-12-26 19:36
找到问题了,控制舵机的if语句没有加大括号,尴尬

请问可以分享一下代码么
回复

使用道具 举报

gada888  版主

发表于 2018-12-12 11:41:35

写的全点就好了
回复

使用道具 举报

20060606  高级技匠

发表于 2020-8-9 06:31:26

Rockets 发表于 2016-12-23 11:21
我的理解是,利用超声波的距离来控制舵机转角的控制。
这个程序不需要用if语句来完成,简单的用map函数就可 ...

不明白您说的意思
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主

楼主的其它帖子

上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail