Help with converting logic process to code

Hi all,

I'm fairly new at this so please bare with me!

I'm building a robot and have been working on various logic processes or algorithms to get it to navigate a room....some more advanced than others, so I'm starting to try to program the basic one to begin with!

The intention of the robot is to make it's way to the top right corner of a room.

I have included my logic process and the code i have written so far. I'm having a few issues converting the logic into code but the one i particularly need help with is:

at the moment the robot turns left or right dependent on it's surroundings, but i want it to follow the logic process with regards to getting back on track. For example

if front <=15cm and left <=15cm
turn right
move forward until left >15cm
turn left
move forward

or another branch on the logic might call for the vehicle to turn 180 and back track before getting back on track.

Do i need to code every possible eventuality (to an extent) or is there some easy way of writing a sub-routine and calling it in a particular situation?

I hope this makes sense, hopefully the logic and code will help!

Many thanks in advance

Alex

const int numOfReadingsLeft = 10;
int readingsLeft[numOfReadingsLeft];
int arrayIndexLeft = 0;
int totalLeft = 0;
int averageDistanceLeft = 0;

const int numOfReadingsFront = 10;
int readingsFront[numOfReadingsFront];
int arrayIndexFront = 0;
int totalFront = 0;
int averageDistanceFront = 0;

const int numOfReadingsRight = 10;
int readingsRight[numOfReadingsRight];
int arrayIndexRight = 0;
in totalRight = 0;
int averageDistanceRight = 0;

// Setup pins for three SRF-05’s //

int durationLeft;				// Stores the duration of the pulse
int distanceLeft;				// Stores the distance

int durationFront;
int distanceFront;

int durationRight;
int distanceRight;

int pingPinLeft = 3;				// Sets the ping pin to pin 3
int echoPinLeft = 4;				// Sets the echo pin to pin 4

int pingPinFront = 5;
int echoPinFront = 6;

int pingPinRight = 7;
int echoPinRight = 8;

// Setup Pins for two Motors //

int MotorLeftPin1 = 2;
int motorLeftPin2 = 11;
int enableMotorLeft = 9;
int motorRightPin1 = 12;
int motorRightPin2 = 13;
int enableMotorRight = 10;




// Set motor pins to I/O //

void setup ( ) {
	pinMode(motorLeftPin1, OUTPUT);
	pinMode(motorLeftPin2, OUTPUT);
	pinMode(enableMotorLeft, OUTPUT);
	pinMode(motorRightPin1, OUTPUT);
	pinMode(motorRightPin2, OUTPUT);
	pinMode(enableMotorRight, OUTPUT);

	digitalWrite(enableMotorLeft, HIGH);
	digitalWrite(enableMotorRight, HIGH);

// Setup serial port //

void setup() {						
  Serial.begin(9600);
}

// Set SRF-05 pins to I/O //

void setup() {
  pinMode(pingPinLeft, OUTPUT);	  	
  pinMode(pingPinFront, OUTPUT);
  pinMode(pingPinRight, OUTPUT);
  pinMode(echoPinLeft, INPUT);
  pinMode(echoPinFront, INPUT);
  pinMode(echoPinRight, INPUT);


// create array loop to iterate over every item in the array

  for (int thisReadingLeft = 0; thisReadingLeft < numOfReadingsLeft;   
    thisReadingLeft++) { readingsLeft[thisReadingLeft] = 0
  }
  
  for (int thisReadingFront = 0; thisReadingFront < numOfReadingsFront;   
    thisReadingFront++) { readingsFront[thisReadingFront] = 0 
  }
  
  for (int thisReadingRight = 0; thisReadingRight < numOfReadingsRight;  
    thisReadingRight++) { readingsRight[thisReadingRight] = 0
  }
}

void loop() {
  digitalWrite(pingPinLeft, HIGH);
  delayMicroseconds(10); 
  digitalWrite(pingPinLeft, LOW);

  digitalWrite(pingPinFront, HIGH);
  delayMicroseconds(10); 
  digitalWrite(pingPinFront, LOW);

  digitalWrite(pingPinRight, HIGH);
  delayMicroseconds(10); 
  digitalWrite(pingPinRight, LOW);

  pulseTimeLeft = pulseInLeft(echoPinLeft, HIGH);
  distanceLeft = pulseTimeLeft/58;

  pulseTimeFront = pulseInFront(echoPinFront, HIGH);
  distanceFront = pulseTimeLeft/58;

  pulseTimeRight = pulseInRight(echoPinRight, HIGH);
  distanceRight = pulseTimeRight/58;

  totalLeft = totalLeft – readingsLeft[arrayIndexLeft];
  readingsLeft[arrayIndexLeft] = distanceLeft;
  totalLeft = totalLeft + readingsLeft[arrayIndexLeft];
  arrayIndexLeft = arrayIndexLeft + 1;

  totalFront = totalFront – readingsFront[arrayIndexFront];
  readingsFront[arrayIndexFront] = distanceFront;
  totalFront = totalFront + readingsFront[arrayIndexFront
  totalRight = totalRight – readingsRight[arrayIndexRight];
  readingsRight[arrayIndexRight] = distanceRight;
  totalRight = totalRight + readingsRight[arrayIndexRight];
  arrayIndexRight = arrayIndexRight + 1;

// At the end of the array (10 items) then start again

  if (arrayIndexLeft >= numOfReadingsLeft)  {
    arrayIndexLeft = 0;
  }

  if (arrayIndexFront >= numOfReadingsFront)  {
    arrayIndexFront = 0;
  }

  if (arrayIndexRight >= numOfReadingsRight)  {
    arrayIndexRight = 0;
  }

// Calculate the average distance for each heading //

  averageDistanceLeft = totalLeft / numOfReadingsLeft; 
  delay(10);

  averageDistanceFront = totalFront / numOfReadingsFront;
  delay(10);

  averageDistanceRight = totalRight / numOfReadingsRight;
  delay(10);


// LOGIC PROCESS – Assess distance and move accordingly //

// Move Forward

if (averageDistanceFront >15) {
	digitalWrite(motorLeftPin1, LOW);
	digitalWrite(motorLeftPin2, HIGH);
	digitalWrite(motorRightPin1, LOW);
	digitalWrite(motorRightPin2, HIGH);
}

// STOP

if (averageDistanceFront <=15 && averageDistanceLeft <=15 &&   
  averageDistanceRight <=15) {
digitalWrite(motorLeftPin1, LOW);
	digitalWrite(motorLeftPin2, LOW);
	digitalWrite(motorRightPin1, LOW);
	digitalWrite(motorRightPin2, LOW);
}


// Turn Right //

if (averageDistanceFront <= 15 && averageDistanceLeft <=15) { 
digitalWrite(motorLeftPin1, LOW);
	digitalWrite(motorLeftPin2, HIGH);
	digitalWrite(motorRightPin1, HIGH);
	digitalWrite(motorRightPin2, LOW);
}

// Turn Left //

if averageDistanceFront <= 15 && averageDistanceRight <=15) { 
digitalWrite(motorLeftPin1, HIGH);
	digitalWrite(motorLeftPin2, LOW);
	digitalWrite(motorRightPin1, LOW);
	digitalWrite(motorRightPin2, HIGH);
}

LOGIC.pdf (317 KB)

You can only have one setup() function in a program you appear to have two.

is there some easy way of writing a sub-routine and calling it in a particular situation?

Yes in this language they are called functions, and you have written two already setup() and loop().
Break the code into manageable chunks by using functions. Pass those functions variables to get them to act in the way you want.
So you might have a function that takes a variable for the turn 0 for left 1 for right, one variable for the distance to the side and another for the distance forward. I think you can work out how to make that universal.

Did you even try compiling that code?

void setup ( ) {
void setup() {						
void setup() {

You get ONE setup() and one loop() function.

It's nice that you have developed this flow chart. I don't see how, though, "The intention of the robot is to make it's way to the top right corner of a room." will ever happen, unless your robot can climb walls or fly.

Have you "played robot" and followed that flow chart to get from the start point to the end point? I see one Start and 3 Ends. Typically, you have one of each.

Do i need to code every possible eventuality (to an extent) or is there some easy way of writing a sub-routine and calling it in a particular situation?

You mean like setup(), loop(), digitalRead(), etc.? Of course you can (and SHOULD!) have subroutines.

How will you know how far the robot has turned? How will you know how far the robot has traveled after turning? Does the robot travel in a straight line now?

Thanks to both for the replies, much appreciated!

PaulS:

No in all honesty I haven't compiled it yet, it was written in word and I'm trying to iron out all my problems before I pass it through the compiler...and have more issues to deal with!

Sorry, poor description with regards the intentions of the robot; its a plan view, and I should have said the northeast corner!

I'm doing this for my major project at uni, and have written various logic flowcharts to combat this problem, each version raising new issues whilst trying to overcome the issues of it's predecessor. As I have no experience programming I'm just trying to combat the most basic solution at the moment, and there are many 'givens' such as the vehicle starts off facing east, also It wont stop at the intended corner but will instead turn back and amble on...other versions have solved this but are way beyond me when it comes to the code!

....the additional "ends" are where I haven't solved the next step! And I keep spotting errors in my logic and revising it...lets call this a work in progress :wink:

Grumpy_Mike:

As you can probably tell I've hacked this code from various sources and although I can follow what it is doing I had no idea I already had subroutines! (Newb!)

I shall try what you have suggested. As I understand it I can write a routine which will for example get the robot back on track from say an object on the left and one in front, an then give it a name and then put an 'if' in to run it if it encounters that situation? Have I grasped it correctly?.. if i were to call it correctRightTurn would the following work?

if averageDistanceFront <= 15 && averageDistanceLeft <=15) {
correctRightTurn
}

And how would I define what correctRightTurn does? If i wanted it to include the following instructions...

correctRightDoes

// Turn Right

digitalWrite(motorLeftPin1, LOW);
digitalWrite(motorLeftPin2, HIGH);
digitalWrite(motorRightPin1, HIGH);
digitalWrite(motorRightPin2, LOW);

// Drive Forward

digitalWrite(motorLeftPin1, LOW);
digitalWrite(motorLeftPin2, HIGH);
digitalWrite(motorRightPin1, LOW);
digitalWrite(motorRightPin2, HIGH);

I'm sure I look rather stupid as the code I originally included probably includes an example of this...I just cant spot it.

...Thanks again!

I would forget for the moment getting round things and just write a program that follows a set path. Get that going, get the feel of programming and also see the connection between what you write and what your robot actually does.
Write that as a set of functions (or subroutines) so that you can call them up one at a time from an array of instructions.
Only when you have got that under your belt can you begin to address your stated problem.
Try and crawl before you attempt to stand.

Get that going, get the feel of programming and also see the connection between what you write and what your robot actually does.

Absolutely. You'll learn more in 30 minutes with some code really running on the robot than in 2 weeks of flow charts and wondering.