Po dłuższej walce z projektem systemu antykolizyjnego do pojazdów RC doszedłem do momentu w którym jestem zmuszony zwrócić się do osób lepiej znających temat (jestem początkujący w temacie Arduino i mikrokontrolerów dlatego proszę o wyrozumiałość :) ).
W skład projektu wchodzą:
-aparatura FrSky Taranis
-odbiornik FrSky X8R
-arduino nano
-czujnik ir Sharp GP2Y0A710K0F 100-550cm
-auto rc
Zamysłem projektu było stworzenie algorytmu który sczyta sygnały z aparatury - w moim przypadku przeanalizuje sygnał kanału odpowiadającego na przepustnicę auta RC, i w razie konieczności za pomocą danych pochodzących z czujnika, wraz ze zbliżaniem się do przeszkody sukcesywnie zacznie przesterowywać silnik zapobiegając kolizji.
Do czytania wartości sygnałów wykorzystałem sketch arduino autorstwa właściciela następującego bloga:
http://rcarduino.blogspot.com/2012/01/h ... -with.html
Kod: Zaznacz cały
#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
volatile int nThrottleIn = NEUTRAL_THROTTLE; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long ulStartPeriod = 0; // set in the interrupt
volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
// we could use nThrottleIn = 0 in loop instead of a separate variable, but using bNewThrottleSignal to indicate we have a new signal
// is clearer for this first example
void setup()
{
// tell the Arduino we want the function calcInput to be called whenever INT0 (digital pin 2) changes from HIGH to LOW or LOW to HIGH
// catching these changes will allow us to calculate how long the input pulse is
attachInterrupt(THROTTLE_SIGNAL_IN,calcInput,CHANGE);
Serial.begin(9600);
}
void loop()
{
// if a new throttle signal has been measured, lets print the value to serial, if not our code could carry on with some other processing
if(bNewThrottleSignal)
{
Serial.println(nThrottleIn);
// set this back to false when we have finished
// with nThrottleIn, while true, calcInput will not update
// nThrottleIn
bNewThrottleSignal = false;
}
// other processing ...
}
void calcInput()
{
// if the pin is high, its the start of an interrupt
if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
{
// get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
// easy to understand and works very well
ulStartPeriod = micros();
}
else
{
// if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
// start time ulStartPeriod from the current time returned by micros()
if(ulStartPeriod && (bNewThrottleSignal == false))
{
nThrottleIn = (int)(micros() - ulStartPeriod);
ulStartPeriod = 0;
// tell loop we have a new signal on the throttle channel
// we will not update nThrottleIn until loop sets
// bNewThrottleSignal back to false
bNewThrottleSignal = true;
}
}
}]
Kod: Zaznacz cały
#include <SharpIR.h>
/* Instances */
SharpIR sharp(A0,100500);
/* Setup function */
void setup() {
pinMode( A0, INPUT);
Serial.begin(9600);
}
/* Infinite loop */
void loop() {
/* Check for obstacles */
int dist=sharp.distance();
Serial.println(dist);
/* Avoid reading before the new distance is sampled */
delay( 500 );
}
Bardzo proszę o pomoc jak rozwiązać powyżej przedstawione problemy oraz o wszelakie wskazówki.
Użyty kod podczas testów:
Kod: Zaznacz cały
#include<Servo.h>
#include <SharpIR.h>
#define THROTTLE_SIGNAL_IN 0 // INTERRUPT 0 = DIGITAL PIN 2 - use the interrupt number in attachInterrupt
#define THROTTLE_SIGNAL_IN_PIN 2 // INTERRUPT 0 = DIGITAL PIN 2 - use the PIN number in digitalRead
#define NEUTRAL_THROTTLE 1500 // this is the duration in microseconds of neutral throttle on an electric RC Car
volatile int nThrottleIn = NEUTRAL_THROTTLE; // volatile, we set this in the Interrupt and read it in loop so it must be declared volatile
volatile unsigned long ulStartPeriod = 0; // set in the interrupt
volatile boolean bNewThrottleSignal = false; // set in the interrupt and read in the loop
// we could use nThrottleIn = 0 in loop instead of a separate variable, but using bNewThrottleSignal to indicate we have a new signal
// is clearer for this first example
Servo myservo;
SharpIR sharp(A0,100500);
void setup()
{
// tell the Arduino we want the function calcInput to be called whenever INT0 (digital pin 2) changes from HIGH to LOW or LOW to HIGH
// catching these changes will allow us to calculate how long the input pulse is
attachInterrupt(THROTTLE_SIGNAL_IN,calcInput,CHANGE);
pinMode(10, OUTPUT);
pinMode(A0, INPUT);
myservo.attach(10);
Serial.begin(9600);
}
void loop()
{
// if a new throttle signal has been measured, lets print the value to serial, if not our code could carry on with some other processing
if(bNewThrottleSignal)
{
Serial.println(nThrottleIn);
myservo.writeMicroseconds(nThrottleIn);
// set this back to false when we have finished
// with nThrottleIn, while true, calcInput will not update
// nThrottleIn
bNewThrottleSignal = false;
}
// other processing ...
int dist = sharp.distance();
Serial.println(dist);
if (dist>=110 && dist<=130){
myservo.writeMicroseconds(NEUTRAL_THROTTLE);
Serial.println(dist);
}
}
void calcInput()
{
// if the pin is high, its the start of an interrupt
if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
{
// get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
// easy to understand and works very well
ulStartPeriod = micros();
}
else
{
// if the pin is low, its the falling edge of the pulse so now we can calculate the pulse duration by subtracting the
// start time ulStartPeriod from the current time returned by micros()
if(ulStartPeriod && (bNewThrottleSignal == false))
{
nThrottleIn = (int)(micros() - ulStartPeriod);
ulStartPeriod = 0;
// tell loop we have a new signal on the throttle channel
// we will not update nThrottleIn until loop sets
// bNewThrottleSignal back to false
bNewThrottleSignal = true;
}
}
}