Pseudocode and Base Code


Pseudocode

Pseudocode is an outline of the logic or algorithm for your program. It is written as a structured description of how your program will work.

For Mission Objective #2, a possible pseudocode might be:

  1. Define a “software switch” using a boolean variable to indicate if our rover is to start moving. Use a push button to toggle between start and stop for our rover.
  2. If rover is to start, get the heading and use that as our desired heading that we want to maintain.
  3. While the “software switch” is true, we will always move forward
    • Move forward
    • Check if an object is in the way
    • If an object is in the way, stop rover, otherwise keep going
    • Check our heading.
    • If our heading is off by ?? degrees, then adjust heading by steering the rover

 

Basic Code

#include <Servo.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>   // Adafruit Sensor library
#include <Adafruit_LSM303_U.h> // Magnetometer library
#include <Adafruit_L3GD20_U.h> // Gyroscope library
#include <Adafruit_9DOF.h>     // 9-DOF library

// Define sensors
Adafruit_9DOF dof = Adafruit_9DOF();
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);

// Sensor event vectors
sensors_event_t magEvent;
sensors_vec_t orientation;

// Define the servos
Servo rightServo;
Servo leftServo;

// Digital I/O pin assignments
int pbPin = 2;         // Push button
int rightServoPin = 3; // Servo
int leftServoPin = 4;  // Servo

// Speed values
int fast = 7;
int slow = 4;
int steerRight = 0;  // initial steering factor for the right servo
int steerLeft = 0;   // initial steering factor for the left servo
boolean isStarted = false;
double startHeading;

void setup() {
  pinMode(pbPin, INPUT);

  if (!mag.begin()) {
    while(1); // We cannot do anything without the sensor
  }
}


void loop() {
  // Make sure the rover is stopped
  rightServo.write(90);
  leftServo.write(90);
 
  if (digitalRead(pbPin) == LOW) {
    isStarted = !isStarted; // if the button is pressed, toggle the isStarted boolean

    if (isStarted == true) {
      // We want to start moving
      // Get our startHeading as this will be our desired direction
      startHeading = getHeading();
    }

    delay(500); // Add a delay to give time for us to lift our finger off the push button
  }

  while(isStarted == true) {
    // Check if the push button is pressed (means we want to stop)
    if (digitalRead(2) == LOW) {
      isStarted = false;
    }
    // -------------------------------------
    // Check if anything is in front of us
    double distance = getDistance();

    // put your obstacle detection logic here
    // ... 
    // ...
    // -------------------------------------

    // -------------------------------------
    // Check out current heading.
    // If we are off course, adjust course with the steering
    double currentHeading = getHeading();

    // put your steering logic here
    // adjust the steerRight and steerLeft variables
    // ... 
    // ...
    // -------------------------------------

    // Go forward!
    rightServo.write(90 + fast + steerRight);
    leftServo.write(90 - fast - steerLeft);
  }
}

// =================================================================
// Function definitions

double getDistance() {
  double distance;

  // ... put code here

  return distance; 
}

double getHeading() {
  double heading;
  // ... put code here 

  return heading;
}