2013-06-05 53 views
0

对于我们的移动反射镜项目,我们使用6个超声波传感器来旋转反射镜所在的伺服电机。6旋转伺服电机的超声波传感器

我们的理念是,如果1个传感器检测到某个人/他们面前的某个东西,那么这个镜子就会转离该人。虽然我似乎无法回避。

我已经使用了库“NewPing”,它可以在https://code.google.com/p/arduino-new-ping/可以看出,我使用的15传感器例如(https://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example

是编码本的正确的方式还是应该从头开始,并使用另一种方法?

在此先感谢!

验证码:

// --------------------------------------------------------------------------- 
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust 
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the 
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor 
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results 
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project 
// would normally process the sensor results in this function (for example, decide if a robot needs to 
// turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs 
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other 
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind. 
// --------------------------------------------------------------------------- 
// Sketch of 6 sensors with servo 
// --------------------------------------------------------------------------- 
// |   \    |    |   /   | 
// |  ----------- ----------- ----------- -----------   | 
// |  |Sensor 2 | |Sensor 3 | |Sensor 4 | |Sensor 5 |   | 
// | \  ----------- ----------- ----------- -----------   | 
// | \               / | 
// | -----------            ----------- | 
// | |Sensor 1 |            |Sensor 6 | | 
// | -----------    -----------     ----------- | 
// |        | Servo |         | 
// |        -----------         | 
// |                   | 
// | if Sensor 1 measures someone close the Servo should turn away from | 
// |       Sensor 1          | 
// --------------------------------------------------------------------------- 
#include <NewPing.h> 

#include <Servo.h> 

Servo myservo; 

#define SONAR_NUM  6 // Number or sensors. 
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping. 
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo). 

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor. 
unsigned int cm[SONAR_NUM];   // Where the ping distances are stored. 
uint8_t currentSensor = 0;   // Keeps track of which sensor is active. 
int val;       // The number which makes the Servomotor rotate 

NewPing sonar[SONAR_NUM] = {  // Sensor object array. 
    NewPing(1, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
    NewPing(3, 4, MAX_DISTANCE), 
    NewPing(5, 6, MAX_DISTANCE), 
    NewPing(7, 8, MAX_DISTANCE), 
    NewPing(9, 10, MAX_DISTANCE), 
    NewPing(11, 12, MAX_DISTANCE) 
    }; 

void setup() { 
    Serial.begin(115200); 
    pingTimer[0] = millis() + 75;   // First ping starts at 75ms, gives time for the Arduino to chill before starting. 
    for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor. 
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL; 
} 

void loop() { 
    for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors. 
    if (millis() >= pingTimer[i]) {   // Is it this sensor's time to ping? 
     pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged. 
     if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results. 
     sonar[currentSensor].timer_stop();   // Make sure previous timer is canceled before starting a new ping (insurance). 
     currentSensor = i;       // Sensor being accessed. 
     cm[currentSensor] = 0;      // Make distance zero in case there's no ping echo for this sensor. 
     sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo). 
    } 
    } 
    // -------------- BEGIN SELFMADE CODE ----------------------- 
    if(cm[1] <= 30) { 
    val = 60; 
    } 
    if(cm[2] <= 30) { 
    val = 100; 
    } 
    if(cm[3] <= 30) { 
    val = 140; 
    } 
    if(cm[4] <= 30) { 
    val = 120; 
    } 
    if(cm[5] <= 30) { 
    val = 80; 
    } 
    if(cm[6] <= 30) { 
    val = 40; 
    } 
    myservo.write(val); 
    // -------------- END SELFMADE CODE -------------------- 
} 

void echoCheck() { // If ping received, set the sensor distance to array. 
    if (sonar[currentSensor].check_timer()) 
    cm[currentSensor] = sonar[currentSensor].ping_result/US_ROUNDTRIP_CM; 
} 

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results. 
    for (uint8_t i = 0; i < SONAR_NUM; i++) { 
    Serial.print(i); 
    Serial.print("="); 
    Serial.print(cm[i]); 
    Serial.print("cm "); 
    } 
    Serial.println(); 
} 

回答

0

这是我会怎么构造的代码。

我只是将自制零件移动到oneSensorCycle()。我真的不喜欢它使用地球仪的方式,但它仍然看起来很干净。

#include <NewPing.h> 

#include <Servo.h> 

Servo myservo; 

#define SONAR_NUM  6 // Number or sensors. 
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping. 
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo). 

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen  for each sensor. 
unsigned int cm[SONAR_NUM];   // Where the ping distances are stored. 
uint8_t currentSensor = 0;   // Keeps track of which sensor is active. 
//int val;       // The number which makes the Servomotor rotate 

NewPing sonar[SONAR_NUM] = {  // Sensor object array. 
NewPing(1, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
NewPing(3, 4, MAX_DISTANCE), 
NewPing(5, 6, MAX_DISTANCE), 
NewPing(7, 8, MAX_DISTANCE), 
NewPing(9, 10, MAX_DISTANCE), 
NewPing(11, 12, MAX_DISTANCE) 
}; 

void setup() { 
Serial.begin(115200); 
pingTimer[0] = millis() + 75;   // First ping starts at 75ms, gives time for the Arduino to chill before starting. 
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor. 
     pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL; 
} 

void loop() { 
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors. 
    if (millis() >= pingTimer[i]) {   // Is it this sensor's time to ping? 
     pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged. 
     if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results. 
     sonar[currentSensor].timer_stop();   // Make sure previous timer is canceled before starting a new ping (insurance). 
     currentSensor = i;       // Sensor being accessed. 
     cm[currentSensor] = 0;      // Make distance zero in case there's no ping echo for this sensor. 
     sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo). 
    } 
} 
} 

void echoCheck() { // If ping received, set the sensor distance to array. 
if (sonar[currentSensor].check_timer()) 
    cm[currentSensor] = sonar[currentSensor].ping_result/US_ROUNDTRIP_CM; 
} 

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results. 
int val =0; 
for (uint8_t i = 0; i < SONAR_NUM; i++) { 
    Serial.print(i); 
    Serial.print("="); 
    Serial.print(cm[i]); 
    Serial.print("cm "); 
// -------------- BEGIN SELFMADE CODE ----------------------- 

if(cm[0] <= 30) { 
    val = 60; 
} 
if(cm[1] <= 30) { 
    val = 100; 
} 
if(cm[2] <= 30) { 
    val = 140; 
} 
if(cm[3] <= 30) { 
    val = 120; 
} 
if(cm[4] <= 30) { 
    val = 80; 
} 
if(cm[5] <= 30) { 
    val = 40; 
} 
myservo.write(val); 

// -------------- END SELFMADE CODE ----------------------- 


} 
Serial.println(); 
} 
+0

因此,这将检查每个传感器,如果条件满足,伺服将转动。对? – Azylak

+0

是的,我认为是这样,但if语句很奇怪。例如,假设cm [3] = 20和cm [6] = 20。然后val将被第三if语句设置为140。但如果达到第六名,则会变为40。 –

+0

我错过了一些事情。我需要做一些修复。数组从0开始,但测试从1开始,这意味着我们正在测试一个不在数组中的值。此外,var未正确初始化。我不知道为什么,但它是全球性的。我会解决这两个问题。 –