2013-06-05 2 views
0

이동 미러 프로젝트의 경우 6 개의 초음파 센서를 사용하여 미러가 켜져있는 서보 모터를 회전시킵니다.6 서보 모터를 회전시키는 초음파 센서

우리의 개념은 1 개의 센서가 누군가의/누군가의 앞쪽을 감지하면 그 사람에게서 거울이 멀어지는 것입니다. 나는 그것이 멀리 돌아서는 것처럼 보일 수는 없지만.

내가 https://code.google.com/p/arduino-new-ping/에서 알 수있는 라이브러리 "NewPing"를 사용하고 난 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으로 설정됩니다. 그러나 6 번째에 도달하면 40으로 변경됩니다. –

+0

나는 한참을 놓쳤다. 몇 가지 수정해야합니다. 배열은 0에서 시작하지만 테스트는 1부터 시작합니다. 즉, 배열에없는 값을 테스트 중입니다. 또한 var가 올바르게 초기화되지 않았습니다. 왜 그런지는 모르겠지만 전 세계적입니다. 두 가지 문제를 모두 해결할 것입니다. –

관련 문제