(code, updates) |
(updated code) |
||
Line 19: | Line 19: | ||
* 01/03/2012: Uploaded code (see below), works, but _only_ when USB cable is attached, otherwise Frank _always_ retreats! | * 01/03/2012: Uploaded code (see below), works, but _only_ when USB cable is attached, otherwise Frank _always_ retreats! | ||
<nowiki><pre>/* | |||
Semi-intelligent steering for RC car | Semi-intelligent steering for RC car | ||
bjornl | bjornl | ||
29-02-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); | Serial.begin(9600); | ||
// set up the outputs | // set up the outputs | ||
Line 57: | Line 59: | ||
pinMode( fwdPin, OUTPUT); | pinMode( fwdPin, OUTPUT); | ||
pinMode( bwdPin, OUTPUT); | pinMode( bwdPin, OUTPUT); | ||
pinMode( ledPin, OUTPUT); | pinMode( ledPin, OUTPUT); | ||
Line 63: | Line 65: | ||
pinMode(triggerPin, OUTPUT); | pinMode(triggerPin, OUTPUT); | ||
pinMode(echoPin, INPUT); | pinMode(echoPin, INPUT); | ||
} | |||
void loop() { | |||
int measurement; | int measurement; | ||
Line 90: | Line 92: | ||
goForward(runTime); | goForward(runTime); | ||
} | } | ||
} | |||
void panic() { | |||
Serial.println("Panic!"); | Serial.println("Panic!"); | ||
// flash LED | // flash LED | ||
Line 101: | Line 103: | ||
delay(100); | delay(100); | ||
} | } | ||
} | |||
void retreat() { | |||
Serial.println("Retreat!"); | Serial.println("Retreat!"); | ||
// Go | // Go backward left for the given amount of time | ||
digitalWrite(rightPin, HIGH); | |||
digitalWrite(leftPin, LOW); | |||
delay(200); | |||
digitalWrite(fwdPin, LOW); | digitalWrite(fwdPin, LOW); | ||
digitalWrite(bwdPin, HIGH); | digitalWrite(bwdPin, HIGH); | ||
delay(500); | |||
delay( | |||
digitalWrite(bwdPin, LOW); | digitalWrite(bwdPin, LOW); | ||
delay(200); | |||
digitalWrite(rightPin, LOW); | digitalWrite(rightPin, LOW); | ||
} | |||
void goForward(int runTime) { | |||
Serial.print("Forward: "); | Serial.print("Forward: "); | ||
Serial.println(runTime); | Serial.println(runTime); | ||
Line 125: | Line 129: | ||
delay(runTime); | delay(runTime); | ||
digitalWrite(fwdPin, LOW); | digitalWrite(fwdPin, LOW); | ||
} | |||
int getMeasurement() { | |||
Serial.print("Measurement: "); | Serial.print("Measurement: "); | ||
int measurement; | int measurement; | ||
// wait for some time, make it look like we are thinking ;) | // wait for some time, make it look like we are thinking ;) | ||
digitalWrite(ledPin,HIGH); | digitalWrite(ledPin,HIGH); | ||
delay( | delay(1000); | ||
// send trigger pulse | // send trigger pulse | ||
digitalWrite(triggerPin, HIGH); | digitalWrite(triggerPin, HIGH); | ||
Line 142: | Line 146: | ||
Serial.println(measurement); | Serial.println(measurement); | ||
digitalWrite(ledPin, LOW); | digitalWrite(ledPin, LOW); | ||
return measurement; | return measurement; | ||
} | |||
/* end */ | |||
</pre></nowiki> |
Revision as of 21:19, 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!
<pre>/* Semi-intelligent steering for RC car bjornl 29-02-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 backward left for the given amount of time digitalWrite(rightPin, HIGH); digitalWrite(leftPin, LOW); delay(200); digitalWrite(fwdPin, LOW); digitalWrite(bwdPin, HIGH); delay(500); digitalWrite(bwdPin, LOW); delay(200); 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(1000); // send trigger pulse digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); measurement = pulseIn(echoPin, HIGH); Serial.println(measurement); digitalWrite(ledPin, LOW); return measurement; } /* end */ </pre>