Skip to main content

First Draft

Major VersionM

by Duoyang Huang

Introduction

Materials: Arduino Mega, cart chassis (motors, wheels, caster), Arduino Motor Shield, breadboards, battery packs, photoresistors, potentiometers, LEDs, tape.

Software: Arduino IDE

  1. Set up potentiometers on a bread board and follow the wiring diagram shown on the right. This challenge is not setting up your PID controller, it is only setting up a way to interface/change your Speed, P, I, and D coefficient values without the need to re-upload new code to your Arduino each time. The code is provided as potentiometer.ino.
    • Set up potentiometers on a bread board and follow the wiring diagram shown on the right. This challenge is not setting up your PID controller, it is only setting up a way to interface/change your Speed, P, I, and D coefficient values without the need to re-upload new code to your Arduino each time. The code is provided as potentiometer.ino.

    • Test this to ensure it prints the potentiometer output to the serial monitor. Remember that a potentiometer acts as a variable resistor. Do counter clockwise or clockwise twists of the potentiometers increase the value printed to the serial monitor? What are different ways to switch it from counter-clockwise to clockwise or vice versa?

  2. Develop multiple voltage dividers with, instead of variable resistors being made of knobs, they are now sensors that change resistance dependent on light. The voltage dividers use 10kΩ resistors.
    • Develop multiple voltage dividers with, instead of variable resistors being made of knobs, they are now sensors that change resistance dependent on light. The voltage dividers use 10kΩ resistors.

    • Use the code photoresistor.ino where you can read the values from each of your photo resistors and print them to the serial monitor. Test this to be sure each photo resistor is working properly.

    • Try placing it close to different colored surfaces and then try it out on a line. What is the best distance for your sensors to be away from the surface to properly differentiate when the sensors are hovering over black or white? How does surrounding light or shadows affect the readings?

  3. Now we would like to put together the cart. Use the instruction manual found with your cart kit. For our design we will only need 2 motors and 2 wheels. The front two wheels and motors will be replaced with a caster. The caster fits very close to the center front in our design. For Now we would like to put together the cart. Use the instruction manual found with your cart kit. For our design we will only need 2 motors and 2 wheels. The front two wheels and motors will be replaced with a caster. The caster fits very close to the center front in our design. For Now we would like to put together the cart. Use the instruction manual found with your cart kit. For our design we will only need 2 motors and 2 wheels. The front two wheels and motors will be replaced with a caster. The caster fits very close to the center front in our design. For
    • Now we would like to put together the cart. Use the instruction manual found with your cart kit. For our design we will only need 2 motors and 2 wheels. The front two wheels and motors will be replaced with a caster. The caster fits very close to the center front in our design. For

  4. First solder the header pins onto the motor shield. It helps to solder your first one or two pins of each strip while it is attached to an Arduino or a breadboard for alignment purposes. Check out the motor shield manual for some helpful hints. Note that the header pins on the outer edge of motor shield seen in the figures should have the longer ends capable of connecting to the Arduino. The header pins just on the inside of those should have the longer ends on the other side for easy connectivity to other devices using female jumper wires.
    • First solder the header pins onto the motor shield. It helps to solder your first one or two pins of each strip while it is attached to an Arduino or a breadboard for alignment purposes. Check out the motor shield manual for some helpful hints.

    • Note that the header pins on the outer edge of motor shield seen in the figures should have the longer ends capable of connecting to the Arduino. The header pins just on the inside of those should have the longer ends on the other side for easy connectivity to other devices using female jumper wires.

    • Use the motor shield manual to download and install the Adafruit motor shield library for Arduino. Without the motors connected, but with the motor shield attached to the Arduino, ensure the code from Appendix 10.3 compiles and uploads without error.

  5. Looking at the diagram on the left, connect your motors to M1 and M2 on your motor shield. Ensure both power sources are available to connect, one for the Arduino Mega, and one for the motor shield. Do not connect the power sources and have your computer plugged into Arduino through USB at the same time.
    • Looking at the diagram on the left, connect your motors to M1 and M2 on your motor shield. Ensure both power sources are available to connect, one for the Arduino Mega, and one for the motor shield. Do not connect the power sources and have your computer plugged into Arduino through USB at the same time.

    • Pay attention to the orientation of the Arduino board on the cart before taping or fixing it. The USB connected port should face the rear side of the cart so that the cable will not be blocked by the front breadboard.

    • You will need two battery packs to power both the Arduino and the motor shield. The 5× AA battery holder for the motor shield is placed inside the cart and the 4× AA battery holder for the Arduino is placed on top of the cart.

    • Code it! You can use the test code system.ino to move forward for 3 seconds and backward for 3 seconds. After reading through the code, can you try another maneuver like going in a circle.

    • Note: if you notice that one wheel moves forward and one wheel moves backward, or both wheels move backwards when it should be moving forward, you could simply change the polarity of wires coming from your motorshield. You can also us use M3 and M4 slots instead of M1 and M2 with a small code alteration declaring M3 and M4 as the motors.

  6. First, there are many ways to put your robot together from here. Remember to consider the height of those photoresistors from the ground for good sensitivity measuring white vs. black surfaces. It may also be good to use a thin piece of balsa wood or thick paper to keep photoresistors aligned and kept from being easily bent. Tape helps with nearly everything including putting your bread boards in place holding the Arduino Mega to a specific spot on your cart, keeping the wires harnessed/organized together, etc. Tape does not help so much with coding.
    • First, there are many ways to put your robot together from here. Remember to consider the height of those photoresistors from the ground for good sensitivity measuring white vs. black surfaces. It may also be good to use a thin piece of balsa wood or thick paper to keep photoresistors aligned and kept from being easily bent.

    • Tape helps with nearly everything including putting your bread boards in place holding the Arduino Mega to a specific spot on your cart, keeping the wires harnessed/organized together, etc. Tape does not help so much with coding.

    • You could solder header connectors for pin A0-A3 and GND and 5V on the motor driver shield for more convenient plug in of the wires.

  7. Light Shield and Light Source Additions: One problem that arises is the robot’s sensitivity to ambient light changes and shadows. Add a light shield and light source on your robot to help mitigate this problem.
    • Light Shield and Light Source Additions: One problem that arises is the robot’s sensitivity to ambient light changes and shadows. Add a light shield and light source on your robot to help mitigate this problem.

    • You can be creative with the 3D printer or simply cover with paper or balsa wood. Add LEDs that are always on by taking advantage of the 5V rails on the breadboard of your photoresistors and don’t forget to add a resistor in series with each of your LEDs.

    • The 3D print files for the light shield are lightshield-3.prt.1, lightshield-3.stl, lightshield-3.gcode. If you are unfamiliar with 3D printing, refer to the 3D print tutorial. The LEDs could be prewired with resistors and taped onto the shield, pointing down.

    • Don't forget to add the pin modes and high and low outputs control statement for the 4 LEDs in the setup when you are using them in later steps.

  8. Calibrating your sensors is very important since sensor readings can easily alter due to changes in sensor position/alignment, changes in surrounding lighting, or other various reasons.
    • Calibrating your sensors is very important since sensor readings can easily alter due to changes in sensor position/alignment, changes in surrounding lighting, or other various reasons.

    • The main goal with your calibration is to take the possible readings from each sensor shown at the top of the two figures below, and then map those readings to a more uniform measure making it more clear where a black line may be located.

    • Place a tape of a contrasting color onto the floor that your cart will run on. The width of the tape should be about the distance between the leftmost and rightmost photoresistors.

    • Before implementing the new code, you may want to add an LED to make the high/low output on pin 15 more visible or choose another more accessible digital pin to control the LED. This LED is used to help you know what step of the calibration you should be performing.

    • Read through the code calibration.ino Upload to the Arduino and follow these steps: 1. While the connected LED is blinking slowly, place the photoresistors attached to the breadboard and cart over a white surface.

    • 2. After a few seconds the LED will blink quickly and at this point it is important not to touch the cart or cast any shadows over it because it is finding the nominal photoresistor reading of each pin for what correlates with pure white.

    • 3. The LED will then blink slowly again giving you time to move the photoresistors over a pure black surface or line. Ensure that each photoresistor is fully over the black surface.

    • 4. Again, after a few seconds the LED will blink quickly and again it is important not to touch the cart or cast any shadows over it because it is finding the nominal photoresistor reading of each pin for what correlates with pure black.

    • 5. Now the sensors should be calibrated and the photoresistors/Arduino should be able to distinguish clearly between what is black and white and the readings should be more uniform moving between 0 and 100. You should see these readings through the serial monitor.

    • 6. Error, or distance from the centerline of the cart, is then calculated through a weighted average.

  9. The first step is to realize what is your error, e, and how to quantify it. This is what your sensors are for and your error may be found from comparing your measured value to your desired value. error = (measured value - desired value)
    • The first step is to realize what is your error, e, and how to quantify it. This is what your sensors are for and your error may be found from comparing your measured value to your desired value. error = (measured value - desired value)

    • You now need to operate on this error and you may break this portion down into 3 main operations: proportional, integral, and derivative control.

    • Proportional control takes your current error value and scales it by your proportional gain, kP. This scaling should transform your error into a desired turn rate to correct for that error. If t you are slightly to the right of the line you will need to increase the speed of your right motor and/or decrease speed to your left motor.

    • Integral control takes into account a sum of your past errors. As your error remains on one side, it will grow into a more and more impactful role affecting your turn rate. This can eliminate small errors and steady state error since as your error is small, the proportional may not push the system enough into your desired state.

    • Derivative control can predict an increase or decrease in your error and prepare your turn rate in advance. You can imagine this being very helpful on a sudden and sharp turn. By taking your current error and subtracting your previous error, you will be able to see how much your error has changed during this one time step.

    • Now we can add all of these terms together to calculate our turn rate to control our tank. Turnrate = (kP*error + kI*sumerrror + kD*deriverror) This turn rate should be transferred to an increase or decrease in motor speed for your left and/or right motor.

  10. Upload linefollow.ino onto the Arduino. Read through the code and comments to see what are the different parts of the code and what does each part do.
    • Upload linefollow.ino onto the Arduino. Read through the code and comments to see what are the different parts of the code and what does each part do.

    • The code will start with the calibration, so each time you will need to repeat the calibration procedure, and then the cart is good to go!

    • In the PID control part of the line follow code, adjust the coefficients of kp, ki, and kd for better accuracy or when necessary (i.e., there is a change in velocity, floor or line color, line width, etc.).

    • Start with lower speed and change the coefficients of kP, kI, and kD based on your observation of the performance of the cart. If the cart is too slow to change, increase kP; if the cart turns back too late after going off the line, increase kI; if the cart changes the direction too much, increase kD.

Finish Line

Duoyang Huang

Member since: 02/16/2018

3 Guides authored

0 Comments

Add Comment

View Statistics:

Past 24 Hours: 1

Past 7 Days: 1

Past 30 Days: 5

All Time: 533