(RangeFinder progress!) |
(code, updates) |
||
Line 14: | Line 14: | ||
* original | * original sourcecode : http://etc.servehttp.com/Frank_Brain.pde.gz | ||
29/02/2012: Attached a Devantech SRF04 Ultrasonic Range Finder to Franks Arduino and got readings! Now working on more intelligent code. | * 29/02/2012: Attached a Devantech SRF04 Ultrasonic Range Finder to Franks Arduino and got readings! Now working on more intelligent code. | ||
* 01/03/2012: Uploaded code (see below), works, but _only_ when USB cable is attached, otherwise Frank _always_ retreats! | |||
/* | |||
Semi-intelligent steering for RC car | |||
bjornl | |||
01-03-2012 | |||
*/ | |||
#define NO_MEASUREMENT 0 | |||
#define TOO_CLOSE 2000 | |||
#define QUITE_CLOSE 5000 | |||
#define NOT_SO_CLOSE 15000 | |||
#define CAREFUL_FORWARD 200 | |||
#define STEP_FORWARD 400 | |||
#define RUN_FORWARD 800 | |||
// these pins are connected straight to the receiver chip of the RC car | |||
const int leftPin = 2; | |||
const int rightPin = 3; | |||
const int fwdPin = 4; | |||
const int bwdPin = 5; | |||
// between this pin and ground, there is a roller ball switch | |||
const int rollerPin = 13; | |||
// little 5mm led, red :) | |||
const int ledPin = 8; | |||
// rangefinder | |||
const int triggerPin = 9; | |||
const int echoPin = 10; | |||
void setup() { | |||
Serial.begin(9600); | |||
// set up the outputs | |||
pinMode( leftPin, OUTPUT); | |||
pinMode(rightPin, OUTPUT); | |||
pinMode( fwdPin, OUTPUT); | |||
pinMode( bwdPin, OUTPUT); | |||
pinMode( ledPin, OUTPUT); | |||
// set up the inputs | |||
pinMode(triggerPin, OUTPUT); | |||
pinMode(echoPin, INPUT); | |||
} | |||
void loop() { | |||
int measurement; | |||
// Get a measurement | |||
measurement = getMeasurement(); | |||
if (measurement == NO_MEASUREMENT) { | |||
// No valid signal was given, panic! | |||
panic(); | |||
} else if (measurement < TOO_CLOSE) { | |||
// Too close to wall, retreat! | |||
retreat(); | |||
} else { | |||
// Determine amount of time to run forward | |||
int runTime; | |||
if (measurement < QUITE_CLOSE) { | |||
runTime = CAREFUL_FORWARD; | |||
} else if (measurement < NOT_SO_CLOSE) { | |||
runTime = STEP_FORWARD; | |||
} else { | |||
runTime = RUN_FORWARD; | |||
} | |||
goForward(runTime); | |||
} | |||
} | |||
void panic() { | |||
Serial.println("Panic!"); | |||
// flash LED | |||
for (int i = 0; i < 10; i++) { | |||
digitalWrite(ledPin, HIGH); | |||
delay(100); | |||
digitalWrite(ledPin, LOW); | |||
delay(100); | |||
} | |||
} | |||
void retreat() { | |||
Serial.println("Retreat!"); | |||
// Go forward for the given amount of time | |||
digitalWrite(fwdPin, LOW); | |||
digitalWrite(bwdPin, HIGH); | |||
digitalWrite(rightPin, HIGH); | |||
digitalWrite(leftPin, LOW); | |||
delay(1000); | |||
digitalWrite(bwdPin, LOW); | |||
digitalWrite(rightPin, LOW); | |||
} | |||
void goForward(int runTime) { | |||
Serial.print("Forward: "); | |||
Serial.println(runTime); | |||
// Go forward for the given amount of time | |||
digitalWrite(fwdPin, HIGH); | |||
digitalWrite(bwdPin, LOW); | |||
digitalWrite(rightPin, LOW); | |||
digitalWrite(leftPin, LOW); | |||
delay(runTime); | |||
digitalWrite(fwdPin, LOW); | |||
} | |||
int getMeasurement() { | |||
Serial.print("Measurement: "); | |||
int measurement; | |||
// wait for some time, make it look like we are thinking ;) | |||
digitalWrite(ledPin,HIGH); | |||
delay(500); | |||
// send trigger pulse | |||
digitalWrite(triggerPin, HIGH); | |||
delayMicroseconds(10); | |||
digitalWrite(triggerPin, LOW); | |||
measurement = pulseIn(echoPin, HIGH); | |||
Serial.println(measurement); | |||
digitalWrite(ledPin, LOW); | |||
return measurement; | |||
} |
Revision as of 20:42, 1 March 2012
NURDspace Project | |
---|---|
Participants | Bjornl, User:buzz |
Skills | |
Status | |
Niche | |
Purpose | |
Tool | |
Location | |
Cost | |
Tool category |
{{{Name}}}Property "Tool Name" (as page type) with input value "{{{Name}}}" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process. Property "Tool Image" (as page type) with input value "File:{{{Picture}}}" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process. {{{Picture}}} {{#if:{{{Tool}}} | [[Tool Owner::bjornl buZz | }} {{#if:{{{Tool}}} | [[Tool Cost::{{{Cost}}} | }}
buZz has an autonomous electric toy car, "Frank". Frank should be upgraded with sensors to avoid collisions and software to act more intelligently (and possibly execute small furry animals simple tasks).
This is probably a good way to get acquainted with Arduino programming.
- create new, more intelligent programs
- add sensors and programming for those sensors
- original sourcecode : http://etc.servehttp.com/Frank_Brain.pde.gz
- 29/02/2012: Attached a Devantech SRF04 Ultrasonic Range Finder to Franks Arduino and got readings! Now working on more intelligent code.
- 01/03/2012: Uploaded code (see below), works, but _only_ when USB cable is attached, otherwise Frank _always_ retreats!
/* Semi-intelligent steering for RC car bjornl 01-03-2012 */ #define NO_MEASUREMENT 0 #define TOO_CLOSE 2000 #define QUITE_CLOSE 5000 #define NOT_SO_CLOSE 15000 #define CAREFUL_FORWARD 200 #define STEP_FORWARD 400 #define RUN_FORWARD 800 // these pins are connected straight to the receiver chip of the RC car const int leftPin = 2; const int rightPin = 3; const int fwdPin = 4; const int bwdPin = 5; // between this pin and ground, there is a roller ball switch const int rollerPin = 13; // little 5mm led, red :) const int ledPin = 8; // rangefinder const int triggerPin = 9; const int echoPin = 10; void setup() { Serial.begin(9600); // set up the outputs pinMode( leftPin, OUTPUT); pinMode(rightPin, OUTPUT); pinMode( fwdPin, OUTPUT); pinMode( bwdPin, OUTPUT); pinMode( ledPin, OUTPUT); // set up the inputs pinMode(triggerPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { int measurement; // Get a measurement measurement = getMeasurement(); if (measurement == NO_MEASUREMENT) { // No valid signal was given, panic! panic(); } else if (measurement < TOO_CLOSE) { // Too close to wall, retreat! retreat(); } else { // Determine amount of time to run forward int runTime; if (measurement < QUITE_CLOSE) { runTime = CAREFUL_FORWARD; } else if (measurement < NOT_SO_CLOSE) { runTime = STEP_FORWARD; } else { runTime = RUN_FORWARD; } goForward(runTime); } } void panic() { Serial.println("Panic!"); // flash LED for (int i = 0; i < 10; i++) { digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(100); } } void retreat() { Serial.println("Retreat!"); // Go forward for the given amount of time digitalWrite(fwdPin, LOW); digitalWrite(bwdPin, HIGH); digitalWrite(rightPin, HIGH); digitalWrite(leftPin, LOW); delay(1000); digitalWrite(bwdPin, LOW); digitalWrite(rightPin, LOW); } void goForward(int runTime) { Serial.print("Forward: "); Serial.println(runTime); // Go forward for the given amount of time digitalWrite(fwdPin, HIGH); digitalWrite(bwdPin, LOW); digitalWrite(rightPin, LOW); digitalWrite(leftPin, LOW); delay(runTime); digitalWrite(fwdPin, LOW); } int getMeasurement() { Serial.print("Measurement: "); int measurement; // wait for some time, make it look like we are thinking ;) digitalWrite(ledPin,HIGH); delay(500); // send trigger pulse digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); measurement = pulseIn(echoPin, HIGH); Serial.println(measurement); digitalWrite(ledPin, LOW); return measurement; }