14629| 11
|
[求助] 超声波信号控制舵机 |
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超声波传感器的影响,请问大神,怎么回事呀? |
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程序的基础上修改的程序,但是舵机工作不受控制,请问是怎么回事呀? } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed