Capstone

From UCLA IEEE OPS
Jump to: navigation, search

Resources

Links

Datasheet: http://www.st.com/web/en/resource/technical/document/datasheet/CD00000059.pdf

Project Overview

Project 7

The Capstone is the last project standing between you and your free T-Shirt.

This project will be much more challenging than the previous ones as breadboard space will be very limited, and design constraints are more open-ended allowing you to make some design decisions.


You will be making a fake Micromouse or Natcar in groups of 2-3 people. This project is intended to help prepare you for one of IEEE's competitions next year, and as a way for you to apply the skills you've learned in a creative application.


Requirements

In order to get checked off for this project (aka COMPLETE OPS AND GET YOUR MONEY BACK), you must meet the following criteria:

  • Avoid crashing into walls or straying completely from the line using a control loop
  • Design your controller so your path is close to straight
  • Turn left or right at a corner and continue straight
  • Successfully navigate a path

You can demonstrate your project using either the mini Micromouse maze or the Natcar test line.


You can implement a P, PI, PD, or full PID controller as long as it behaves according to the requirement. As for the status LED, it is good practice to have some way of observing where your MCU is in its program. For example, you would like to know if it is calibrating sensors or simply stuck in place.

Capstone Competition

At the end of the year, we will have a mini-social/competition! This is your chance to show off your accomplishments. It is also where you will be getting your deposits and free T-Shirts, if you qualify. The goal is to navigate the path in the lowest time. Winners get bragging rights and a small prize!

There will be prizes for 1st, 2nd, and 3rd place teams (Subject to change) including Quadcopters, Raspberry Pi, and TI Launchpads.

Also, winning this competition is a good resume builder and something you can brag about to your EE peers.

Calibrating the Sensors and Measuring Error

Establishing a base value against which your errors can be calculated is important. Because our sensors are not shielded to ambient interference, we must assume that they are not identical. This leads to the need for two measured error values: left error and right error.

We will use a simple averaging method to find our ideal sensor reading. This consists of initializing a sum variable to zero, adding a user defined number of analogRead samples from the desired sensor, and dividing by this number of samples. This will yield the target value for a sensor measurement. The averaging method should be called twice in the setup function to establish both the left and right sensor targets.

In our actual control loop, we will want to check our position relative to where we should be: the averaged target value. Because our motors will vary in speed due to manufacturing error, drift will occur. In each iteration of the control loop, we need to sample measurements from both sensors (again, take an average of a few values to reduce noise) and find the difference between the target. This will give us error values for both the left and right sides. Because our motors and the errors are coupled, that is, changing one motor affects both errors, it would be unwise to output two separate correction values for the motors. Instead, you can find a total error by taking the difference between the left and right errors. This gives us a fairly good position about which we can correct to zero.

Note: Error propagation is negligible for this project.

Motor Driver: L293D H-Bridge

Project 7

An H-Bridge is a class of motor driver circuits, and we have an IC version that makes it easy to breadboard.
IN1, IN2, IN3, and IN4 signals control the direction of rotation. To control the speed of the motors, send PWM signals to the EN (enable) pins (in other words, disregard what the image says about PWM).


Using a 9V Battery

Relying on the 3.7V Li-Po battery will make your robot move, but it will be extremely sluggish. To give the rodent a boost, you'll need to use the 9V battery to power the motors. Do not connect the 9V battery to the Teensy. It will die and you will be sad. Connect the 9V only to pin 8 of the H-Bridge, and use your Teensy's 3.3V Output for everything else.

As for speeding up the motors, connect 9V to Vs pin (pin 8) on the H-Bridge motor driver, and connect 3.3V to the Vss pin (pin 16). This will allow the driver to power the motors with 9V, and communicate with the Teensy with a standard 3.3V logic level.

Example Photos

Project 7
Project 7

Male headers can be soldered to the PCB allowing you to plug the sensor into the breadboard.

Project 7
Project 7

Finished rodent with IR sensors plugged in.

Example natBus

Example of a Natbus.


FAQ

What orientation do the LED and Phototransistor go in?
For the LED, on the board, the footprint has a flat side to the circle outline. On the LED, there is also a flat side that matches with the footprint. For the phototransistor, you will have to refer to the datasheet and the schematic for your board. On the datasheet, there is a diagram which shows you what the longer and shorter pins are.

What value resistors should I use for R1 and R2 on the IR sensor board?
The resistor connected to the IR LED should be 180 Ohms, and the one connected to the IR Phototransistor should be 10 kOhms. Refer to your Eagle schematic and board design to determine which resistor is which.

How do I connect my sensor module to the breadboard?
I would recommend using a 3-pin male header soldered onto the 3 header pins on the side of your PCB, and mating that with a 3-pin female header that is plugged into your breadboard.

How do I attach the motors and ball caster to the breadboard?
The back of the breadboard has an adhesive that the motors and ball caster can be stuck to. You have to remove the paper that is covering the adhesive.


Rodent Pseudocode

The pseudocode for this project can be downloaded as a pdf here

  define hbridge pins
define indicator LED
define sensor pins
// Declare parameters of the robot to measure and control int leftSpeed int rightSpeed
int rightDistance int leftDistance
int rightBaseline int leftBaseline
// Parameters for the controller int rightError int leftError int prevRightError int prevLeftError int totalError int tolerance
int time // Time between control loop iterations int integral double kp // Proportional gain double ki // Integral gain
// Functions to move and decide how to move int acquireSensor(pin) void moveStraight() void turnRight() void stopMoving()
void setup(){
// Configure the pins set the hbridge pins to OUTPUT set the hbridge enable pins to HIGH set the LED pin to OUTPUT set the sensor pins to INPUT // Set the starting parameters initialze motor speeds sample and find the average value for the right sensor sample and find the average value for the left sensor // Set the control parameters set the proportional and integral gain define the time between control loop iterations initialize proportional and integral error begin serial for debugging }
void loop(){
// Like a game, the bulk of programming this robot will be done in // other functions. The main loop just calls these fuctions.
while walls are present: move straight
stop moving // This lets everything settle to improve turn accuracy
decide which direction in which to turn and execute }

ACQUIRE SENSOR:

  {
  	int sum = 0
  	for 0 to number of samples:
  		sum += read sensor value
  	return sum/(number of samples)
  }

MOVE STRAIGHT:

  {
  	// This is where our controller will be implemented since we only
  	// need it when going forward.
  
  	// Find your position relative to the left and right walls
  	rightDistance = read right sensor
  	leftDistance = read left distance
  
  	// Compute the error in these measurements
  	rightError = baselineRight - rightDistance
  	leftError = baselineLeft - leftDistance
  
  	// Compute the total error and integral error
  	totalError = rightError - leftError
  
  	// Decide how to correct based on the error
  	if totalError < -tolerance
  		rightSpeed -= absolute value(kp*totalError)	//shift right
  		leftSpeed += absolute value(kp*totalError)
  
  	if totalError > tolerance
  		rightSpeed += absolute value(kp*totalError) //shift left
  		leftSpeed -= absolute value(kp*totalError)
  
  	otherwise
  		move forward at full speed
  
  	// Normalize speeds if they exceed the system limit
  	if speed < 0
  		speed = 0
  	if speed > 255
  		speed = 255
  
  	
  	// Set motor control signals
  	set motors to forward direction
       set right PWM (rightSpeed)
       set left PWM (leftSpeed)
  	
  }   

TURN RIGHT:

  {
  	right motor backward
  	left motor forward
  	wait some time
  
  	go back to straight
  }

TURN LEFT:

  {
  	right motor forward
  	left motor backward
  	wait some time
  
  	go back to straight
  }


Natbus Pseudocode

The pseudocode for this project can be downloaded as a pdf here //TODO

  define hbridge pins
define indicator LED
define sensor pins
// Declare parameters of the robot to measure and control int leftSpeed int rightSpeed
int rightRead int leftRead
int rightBaseline int leftBaseline
// Parameters for the controller int rightError int leftError int tolerance
int time // Time between control loop iterations double kp // Proportional gain
// Functions to move and decide how to move int acquireSensor(pin) void move() void turnRight() void stopMoving()
void setup(){
// Configure the pins set the hbridge pins to OUTPUT set the hbridge enable pins to HIGH set the LED pin to OUTPUT set the sensor pins to INPUT // Set the starting parameters initialze motor speeds sample and find the average value for the right sensor sample and find the average value for the left sensor // Set the control parameters set the proportional and integral gain define the time between control loop iterations initialize proportional and integral error begin serial for debugging }
void loop(){
// Like a game, the bulk of programming this robot will be done in // other functions. The main loop just calls these fuctions.
while walls are present: move straight
stop moving // This lets everything settle to improve turn accuracy
decide which direction in which to turn and execute }

ACQUIRE SENSOR:

  {
  	int sum = 0
  	for 0 to number of samples:
  		sum += read sensor value
  	return sum/(number of samples)
  }

MOVE:

  {
  	// This is where our controller will be implemented since we only
  	// need it when going forward.
  
  	// Find your position relative to the left and right walls
  	rightRead = read right sensor
  	leftRead = read left distance
  
  	// Compute the error in these measurements
  	rightError = rightRead - rightBaseline
  	leftError = leftRead - leftBaseline
       
       // Normalize error values to be between 0-255 since they currently are between 0-1023 potentially
       rightError *= (255/1023)
       leftError *= (255/1023)
       
  	// Decide how to correct based on the error
  	if rightError > tolerance
  		rightSpeed -= absolute value(Kp*rightError)  //shift right
  		leftSpeed += absolute value(Kp*rightError)
  
  	if leftError > tolerance
  		rightSpeed += absolute value(Kp*leftError) //shift left
  		leftSpeed -= absolute value(Kp*leftError)
  
  	otherwise
  		move forward at full speed
  
  	// Normalize speeds if they exceed the system limit
  	if speed < 0
  		speed = 0
  	if speed > 255
  		speed = 255
  
  	
  	// Set motor control signals
  	set motors to forward direction
       set right PWM (rightSpeed)
       set left PWM (leftSpeed)
  	
}