Tuesday, April 14, 2015

Final Project 1.20



My final project plans have been refined this week. This includes a more coherent block diagram for the program, and the start of a mechanical platform. I have also checked out a couple of motors and taken measurements so I can plan how everything will connect.



I am considering this project in three components: 

1. Mechanical
2. Hardware (electronic)
3. Software. 

1. Mechanical  The mechanical component of my autonomous vehicle will be a tricycle chassis. I figure this will make it possible to control the vehicle with two motors — one for steering and one for driving. My distance sensors will be attached to the steering actuator so that they can be panned back and forth. 


I'm trying to come up with a configuration that has one wheel to both steer and drive, and places all the electrical components on that wheel. So really, it's more like a unicycle that's pulling a chariot. If this works, I won't have to worry as much about connections transitioning the pivot (wires that could fatigue and fail). 


By using a do-everything wheel, then the other two wheels of the tricycle can simply trail behind. They will essentially provide stability for the main wheel, holding it up, and providing a lever to rotate against when the vehicle stops and the sensors are panning.






2. Hardware

The electronic Hardware of this project includes:


  1. geared motor (for driving),
  2. 180° servo motor for steering and sensor panning (servo motor reports position, so the system will know what intervals to take readings).
  3. Four IR distance sensors (Sharp GP2Y0A21YK0F for 10-80cm)
  4. H-Bridge chip for controlling the two motors independently, along with LEDs to indicate their movements.
  5. some momentary switches for start/stop and steering limit detection.
  6. a potentiometer
Here's how the PCB is looking right now.








3. Software

Here's a block diagram:


The code, so far...

/// this sketch is for a custom PCB: i/o must be matched

//============================================================
//=======================VARIABLES============================
//============================================================
// constant variables (digital i/o addresses)
const int light1 = 10;  // PWM-equipped (brightness control)
const int light2 = 3;  // PWM-equipped (brightness control)
const int button1 = 7; // on-board, button closest to digital pins
const int button2 = 4; // on-board closest to analog pins
const int motorEnable1 = 11; // PWM-equipped (speed control)
const int motorClockwise1 = 12;
const int motorCounterClockwise1 = 13;
const int motorEnable2 = 9; // PWM-equipped (speed control)
const int motorClockwise2 = 6;
const int motorCounterClockwise2 = 8;
// unused digital pins: 0, 1, 2, 5.
// constant variables (analog i/o addresses)
const int pot1 = A0; // on-board pot
const int pot2 = A5; // on-board pot
const int distanceSensor1 = A1; // Sharp "2Y0A21 F 92"
const int distanceSensor2 = A2; // Sharp "2Y0A21 F 92"
const int distanceSensor3 = A3; // Sharp "2Y0A21 F 92"
const int distanceSensor4 = A4; // Sharp "2Y0A21 F 92"
// dynamic variables
bool lightOn1 = false;
bool lightOn2 = false;
int buttonRead1 = 111;
int buttonRead2 = 111;
int potRead1 = 111;
int potRead2 = 111;
int distanceRead1 = 111;
int distanceRead2 = 111;
int distanceRead3 = 111;
int distanceRead4 = 111;
int motorSpeed1 = 100;  // percentage speed for tuning
int motorDirection1 = 111;
int motorSpeed2 = 100;  // percentage speed for tuning
int motorDirection2 = 111;
bool printCycle = false;  // if things will serial print
int printDelay = 1000;  // amount of delay for serial print
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;


//============================================================
//=========================SETUP==============================
//============================================================
void setup() {
  Serial.begin(9600);
  // Input pins:
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(pot1, INPUT);
  pinMode(pot2, INPUT);
  pinMode(distanceSensor1, INPUT);
  pinMode(distanceSensor2, INPUT);
  pinMode(distanceSensor3, INPUT);
  pinMode(distanceSensor4, INPUT);
  // Output pins:
  pinMode(lightOn1, OUTPUT);
  pinMode(lightOn2, OUTPUT);
  // H-Bridge motor1 pins:
  pinMode(motorEnable1, OUTPUT);
  digitalWrite(motorEnable1, LOW);  // turn off just in case
  pinMode(motorClockwise1, OUTPUT);
  digitalWrite(motorClockwise1, LOW);  // turn off just in case
  pinMode(motorCounterClockwise1, OUTPUT);
  digitalWrite(motorCounterClockwise1, LOW);  // turn off just in case
  // H-Bridge motor2 pins:
  pinMode(motorEnable2, OUTPUT);
  digitalWrite(motorEnable2, LOW);  // turn off just in case
  pinMode(motorClockwise2, OUTPUT);
  digitalWrite(motorClockwise2, LOW);  // turn off just in case
  pinMode(motorCounterClockwise2, OUTPUT);
  digitalWrite(motorCounterClockwise2, LOW);  // turn off just in case
  // conversions
  motorSpeed1 = map (motorSpeed1, 0, 100, 0, 255);
  motorSpeed2 = map (motorSpeed2, 0, 100, 0, 255);
}  // close setup.


//============================================================
//=========================LOOP===============================
//============================================================
void loop() {
  readInputs();  // reads button and sensor inputs and writes to serial.
  safety();  // checks limit switches; if reached, changes direction.
  buttons();  // acts on the button pushes.
  movement();  // tells motor which what to do.
  digitalWrite(light1, HIGH);
  digitalWrite(light2, LOW);
  delay(potRead1/4);
  digitalWrite(light1, LOW);
  digitalWrite(light2, HIGH);
  delay(potRead1/4);

}  // close loop.


//============================================================
//=======================FUNCTIONS============================
//============================================================
//............................................................
//................readInputs function.........................
//............................................................
void readInputs() {
  printCycle = false;
// read inputs
  buttonRead1 = digitalRead(button1);
  buttonRead2 = digitalRead(button2);
  potRead1 = analogRead(pot1);
  delay(2);  // delay for analog read stability
  potRead2 = analogRead(pot2);
  delay(2);  // delay for analog read stability
  distanceRead1 = analogRead(distanceSensor1);
  delay(2);  // delay for analog read stability
  distanceRead2 = analogRead(distanceSensor2);
  delay(2);  // delay for analog read stability
  distanceRead3 = analogRead(distanceSensor3);
  delay(2);  // delay for analog read stability
  distanceRead4 = analogRead(distanceSensor4);
  delay(2);  // delay for analog read stability
// write results
  currentMillis = millis();
  if (currentMillis > (previousMillis + printDelay)) {
    printCycle = false;
    Serial.println("new cycle: ");
    Serial.print("( buttonRead1 = ");
    Serial.print(buttonRead1);
    Serial.print(" ) buttonRead2 = ");
    Serial.print(buttonRead2);
    Serial.print(" ) potRead1 = ");
    Serial.print(potRead1);
    Serial.print(" ) potRead2 = ");
    Serial.print(potRead2);
    Serial.println(" )");
    Serial.print("( distanceRead1 = ");
    Serial.print(distanceRead1);
    Serial.print(" ) distanceRead2 = ");
    Serial.print(distanceRead2);
    Serial.print(" ) distanceRead3 = ");
    Serial.print(distanceRead3);
    Serial.print(" ) distanceRead4 = ");
    Serial.print(distanceRead4);
    Serial.println(" )");
    previousMillis = currentMillis;
  }  // close "if(currentMillis...)"
  else {
    printCycle = false;
  }  // close "else"
}  // close function "readInputs"


//............................................................
//....................safety function.........................
//............................................................
void safety() {
//  if (potRead2 <= 0) {
//    motorDirection2 = 1;
//  }  // close "if(stopSwitch1...)"
//  if (potRead2 >= 1023) {
//    motorDirection2 = 2;
//  }  // close "if(stopSwitch2...)"
}  // close function "safety()"


//............................................................
//....................buttons function.......................
//............................................................
void buttons() {
  if (buttonRead1 == 0 && buttonRead2 == 0) {
    motorDirection1 = 0;
    motorDirection2 = 0;
  ;
  } // close first "if"
  else {
    if (buttonRead1 == 0 && buttonRead2 == 1) {
    motorDirection1 = 2;
    motorDirection2 = 2;
    ;
    } // close second "if"
    else {
      if (buttonRead1 == 1 && buttonRead2 == 0) {
      motorDirection1 = 1;
      motorDirection2 = 1;
      ;
      } // close "if (stopButtonState)..."
    }  //close second "else"
  }  //close first "else"
} // close function "buttons"


//............................................................
//....................Movement function.......................
//............................................................
void movement() {
// motor1  
  if (motorDirection1 == 1) {
    if (printCycle = true) {
      Serial.println("motorDirection1 = 1");
    }  // close "if"
    analogWrite(motorEnable1, motorSpeed1);
    digitalWrite(motorClockwise1, HIGH);
    digitalWrite(motorCounterClockwise1, LOW);
  } // close first "if"
  else {
    if (motorDirection1 == 2) {
      if (printCycle = true) {
        Serial.println("motorDirection1 = 2");
      }  // close "if"
      analogWrite(motorEnable1, motorSpeed1);
      digitalWrite(motorClockwise1, LOW);
      digitalWrite(motorCounterClockwise1, HIGH);
    } // close second "if"
    else {
      if (motorDirection1 == 0) {
        if (printCycle = true) {
          Serial.println("motor1 STOPPING");
        }  // close "if"
        analogWrite(motorEnable1, LOW);
        digitalWrite(motorClockwise1, LOW);
        digitalWrite(motorCounterClockwise1, LOW);
      } // close "if (stopButtonState)..."
    }  //close second "else"
  }  //close first "else"
// motor2  
  if (motorDirection2 == 1) {
    if (printCycle = true) {
      Serial.println("motorDirection2 = 1");
    }  // close "if"
    analogWrite(motorEnable2, motorSpeed2);
    digitalWrite(motorClockwise2, HIGH);
    digitalWrite(motorCounterClockwise2, LOW);
  } // close first "if"
  else {
    if (motorDirection2 == 2) {
      if (printCycle = true) {
        Serial.println("motorDirection2 = 2");
      }  // close "if"
      analogWrite(motorEnable2, motorSpeed2);
      digitalWrite(motorClockwise2, LOW);
      digitalWrite(motorCounterClockwise2, HIGH);
    } // close second "if"
    else {
      if (motorDirection2 == 0) {
        if (printCycle = true) {
          Serial.println("motor2 STOPPING");
        }  // close "if"
        analogWrite(motorEnable2, LOW);
        digitalWrite(motorClockwise2, LOW);
        digitalWrite(motorCounterClockwise2, LOW);
      } // close "if (stopButtonState)..."
    }  //close second "else"
  }  //close first "else"
} // close function "Movement"



...
Finally, areas of concern:
  1. Scanning to matrix.
  2. Deciding which direction to go (cliff vs. cave vs. open).
  3. Mechanical: possibly making everything on 3D printer?

No comments:

Post a Comment