Final Slot Car Report

16
ASU POLYTECHNIC EST 350 December 2, 2014 Authored by: Virgile Valente and Kyle Avery Optimized Slot Car Track

Transcript of Final Slot Car Report

Page 1: Final Slot Car Report

ASU PolytechnicEST 350

December 2, 2014Authored by: Virgile Valente and Kyle Avery

Optimized Slot Car Track

Using PIC Microcontrollers to Race Slot Cars

Page 2: Final Slot Car Report

1

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

Contents

EXECUTIVE SUMMARY............................................................................................2

PROJECT DESIGN..................................................................................................2

DISCUSSION.........................................................................................................5

CONCLUSION........................................................................................................6

REFERENCES........................................................................................................7

SOURCE CODE.....................................................................................................8

Page 3: Final Slot Car Report

2

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

Executive Summary

A slot car race track typically consists of tracks connected and wired together to create a loop. The track or loop circuit is broken by handheld speed controllers. These speed controllers generally consist of a trigger that relies on a potentiometer which increases or decreases resistance values increasing or decreasing the voltage to the slot cars. The slot cars have a small electric motor which is connected to the track through two brushes that act as electrical contacts. As the user presses the trigger, the resistance decreases and more voltage is passed through the electric car motor and vice versa. Slot cars are very popular toys and hobbies, with the user having complete control of the vehicle speed in his hand. The team decided to take the controls and use a microcontroller to optimize and regulate the speed of a slot car electronically. Using a PIC16F917 microcontroller the team set out to automate the slot car, aptly named the PICar, and program it to race against a human opponent. This was meant to be a man versus machine challenge that would create an automated opponent with high level of difficulty.

Project Design

The Track

Page 4: Final Slot Car Report

3

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

Our initial design had the slot car track was divided into three speed sections; a low speed area around tight curves, a medium speed area for wider turns, and a high speed area for a loop-the-loop. Initially the resistance range of the controller was measured using a multimeters and determined to be approximately 0-25 ohms. The speed of the car relative to the resistance values was tested and examined. Then to control the speed of the car, three different circuits were designed, each having different resistances correlating to the different speeds. A high speed circuit used a low resistance value of 1.2 ohms (allowing the most current to motor), the medium speed circuit used a resistance of 2.7 ohms and the low speed circuit used a resistance of 3.9 ohms (allowing least current to motor).

These different speed circuits would be selected by the PIC, an output signal from the PIC would close a BJT switch, thereby applying the resistive switching circuit to control the speed of the PICar. Several different switching methods including BJTs, AND gates and multiplexers were evaluated and unfortunately none seemed to work due to the large current and voltage already flowing through the emitter and collector as well of the BJT as the high current requirement (1.2-1.8A) to drive the cars. So the team decided to go a different route, using pulse width modulation (PWM) to control the speed of the car.The notion is to change the speed of the car by increasing or decreasing the duty cycle of the car motor using PWM implemented by the PIC microcontroller. The speed sections laid out for the track remained the same as the switching circuit, and three different duty cycles for PWM were identified and are described below along with an initial start speed: Speed Start Slow Medium FastPWM 25 10 22 30

Page 5: Final Slot Car Report

4

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

The appropriate duty cycles chosen had to be tested and adjusted to match the corresponding sections of the track. These were then increased or decreased to produce optimum speeds at each track section.Having calibrated our speeds for the PICar, the PIC microcontroller then needed to be programmed to recognize where the car was in order to switch between these PWM speed settings. To recognize when the car reaches which part of the track the team incorporated an infrared reflective optical sensor, the TRCT5000L. The range of the sensor is 1.5 cm, so the PICar needs to pass within that range to trigger. The sensor essentially experiences a voltage drop when an object reflects IR light back into the phototransistor. Altering the resistor values between the 5V supply and the phototransistor using a voltage divider, the team was able to create two output signals from the sensor: a 5V or digital high when not triggered, and a ~0V or digital low when triggered. The sensor circuit with determined resistor values is depicted below.

The sensors were wired according to the above diagram using 100 ohm resistors for the LED to provide as much power and light as possible. The team used a 100k resistor between the +5V supply and the phototransistor as it forms a voltage divider between it and the PIC pin input. When the phototransistor receives IR light the switch activates allowing the

voltage to pass through it to ground, changing the voltage going into the PIC input pin from 5V (digital high) to less than 50mV (digital low).

Page 6: Final Slot Car Report

5

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

As the PICar passes by the sensor, the emitted IR light from the LED emitter reflects off of it and triggers the phototransistor. This supplies the PIC microcontroller with smooth high or low digital signal inputs.The sensors were then wired into the race track from the bottom to reflect and detect when the car passed over them. (Red - 5V supply, Black – GRND, Yellow – PIC Input)A sensor was added to each speed section (low, medium, high) of the track, as well as an extra sensor at the start of the track to count laps, time permitted. The team also added a start switch and traffic light countdown (Red, Red, Yellow, and Green) to start the race. The

optimal controlled PICar would not start until the light goes green. This also notifies and prepares the human racer to the start of the race.

Having all the hardware and sensors now wired and connected it was time to develop the code. This task was by far the most difficult, considering our meager experience with C-code and microcontrollers. A flow chart (depicted left) was drafted to better illustrate the concepts and loop that had to be programmed for the track.

Page 7: Final Slot Car Report

6

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

A switch is used to start the program, an LED light would countdown from red to red then yellow to green, thereby starting the race.The car would initially begin in the medium speed section of the track and be set to PWM of 75%.The car would then hit multiple sensors effectually switching to the corresponding PWM values before looping back around.An if/else loop inside of a while loop was determined appropriate for this code, and the actual code is documented in C code. Time permitting, the team also wanted to add in a lap counter that would require further coding and configuration.To use PWM from the microcontroller, an H-bridge (Pololu MD07A) was utilized.

The PWM signal from the microcontroller was wired directly into the PWM pin of the Pololu MD07A H-bridge. The OUTA and OUTB pins were then connected to each side of the slot car track, which contacts directly with the positive and negative sides of the motor on the PICar. Appropriate supply and ground connections were also made to the device.

Page 8: Final Slot Car Report

7

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

Testing the circuit and code began after the H-bridge was connected to the slot car track. At first the PWM settings had to be adjusted greatly to find the right speeds for each section of track. This optimization process was time consuming, but when finally refined the speeds of the PICar were as fast as the curves and turns allowed without flying off.

Discussion

The team was excited about this project from the beginning. It was an enlightening experience that involved a fair amount of play. We decided not to use different resistive circuits as our speed control due to problems with interference and unforeseen complications in the switching. The team also considered using a digital potentiometer (digipot) but was not able to acquire one for testing. Using PWM to control the speed of the car provided the solution we desired and functionality that would not interfere with the other racer. The sensors we looked at were mainly optical in nature, although we also considered Hall Effect sensors, but decided against them as optical sensors seemed more practical. The TRCT5000L reflective IR optical sensor proved to be ideal for this project, and can be used in a variety of ways. It is also a

Page 9: Final Slot Car Report

8

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

simple and straight forward sensor which can be tinkered with to output a good range of desired signals. The sensors were attached from the bottom of the track to be less visible and out of the way of the vehicle. The most challenging part of this project was definitely the programming of the microcontroller. The team suffered from a general lack of coding experience and microcontroller knowledge, despite the best efforts of our Microcontrollers’ professor…Along the way the team encountered a few obstacles and issues with the project but managed to work them out in intuitive and clever ways. Sadly, a number of components including H bridges and couple of PIC16F917s were lost during the process, casualties of our learning curve.Future improvements could include more speed settings, implementing the lap counter, difficulty setting, and an automated stop at the end of the race after a desired amount of laps. Expanding the track is also possible, with more twists, turns and additional loops adding to the fun, but increasing the amount of sensors and code needed.

Conclusion

Final Circuit:

The team learned of the many aspects and uses of microcontrollers during this project. There were many speed bumps and challenges along the way. The initial design fell apart a week before being due when our switching circuits could not be implemented properly. The PWM also had a number of

Page 10: Final Slot Car Report

9

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

challenges including wiring to the track and conditioning the signal using a capacitor after the H bridge. A lot of coding was created and many hours were spent debugging and detailing the program. The final design worked well with PWM, the optimized car raced quickly and moved fluidly through the track, about tight turns and around the loop. It proved very difficult to beat manually using the handheld controller. After many hours of tormenting coding and frustrating trial and error the team succeeded in conquering the all-mighty PIC microcontroller, obliging it do our bidding so that we may please our ruthless teacher whom we expect will give us an overly agreeable grade from all the agony and anguish he has wreaked upon us this semester.

Page 11: Final Slot Car Report

10

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

References

Pollat, Scott. EST 350 – Microcontroller Applications. Original PWM source code from Lab 5.2 PWM.Avery, Kyle. ASU POLY -- For being a champion and making PWM work on the track in one day.Pololu MD07A H-Bridge – Pololu High-Power Motor Driver 18v15http://www.pololu.com/product/755TRCT5000L – Reflective Optical Sensor with Transistor Output. Vishay Semiconductors.http://www.vishay.com/docs/83760/tcrt5000.pdf

Page 12: Final Slot Car Report

11

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

Source Code

Kyle AveryVirgile Valente

Slot Car PWM code:

#include<pic.h>#include<time.h>

#include<htc.h>    //Header File (Specific)#define _XTAL_FREQ 4000000

//Setup Device Configuration

__CONFIG(INTIO& DEBUGEN & WDTDIS & PWRTDIS & MCLREN & UNPROTECT & BORDIS & IESODIS & FCMDIS);

void Initialize(void){      //Setup Oscillator for 8MHz      OSCCON =  0b01110101;

      //Disable the PWM output pins by setting the direction register      //bit HIGH for input.      TRISC5 = 1; //Makes RC5 or CCP1 input

      //Set the PWM period at 10us by loading PR2 register      PR2 = 0b11111001;

//Set the PWM duty cycle to 2us by loading the CCPRxL register//and the CCPx bits of the CCPxCON register.

      CCP1X = 0;      CCP1Y = 0;

      //Setup CCP1CON     for PWM Mode      CCP1M3 = 1;      CCP1M2 = 1;

      //Configure and start Timer2. Start by clearing T2CON      //This defaults the prescaler to 1:1      T2CON = 0b00000111;

      //Turn on Timer2        TMR2ON = 1;

      //Enable the PWM output (RC5/CCP1)by clearing the direction      //register bit LOW for output      TRISC5 = 0;}

main()  // main loop{

{{

Page 13: Final Slot Car Report

12

Optim

ized

Slot

Car

Tra

ck 1

2/2/

2014

      Initialize();{    ANSEL = 0x00;  //Set input and outputs to digital    TRISB = 0xff;   //set PORTB to inputs    PORTB = 0xff;   //clear PORTB    TRISA = 0x00;   //Set PORTA as outputs    TRISD = 0xff;  //Set PORTD as inputs    CCPR1L = 0b00000000;  //Set PWM to zero            start:    _delay(2000000);    PORTA = 0x00;    {if (RD3 == 0)   // Start button    while(1)  //while loop for race    {

    {    PORTD=0xff; // Set all of PORTD high for IR sensors    PORTA=0b10000000;  // First stop RED LED            _delay(1500000);      PORTA=0b11000000;  // Second stop RED LED            _delay(1500000);    PORTA=0b11100000;  //Third stop YELLOW LED            _delay(1500000);    PORTA=0b11110000;  // GO GREEN LED    CCPR1L = 0b00111110;  //Set initial PWM to 25% duty cycle    while(1)            {                             if (RD3 == 0){     // Reset button                             CCPR1L = 0b00000000;  // Turn off car                              goto start;  //Jump to beginning                                                       }                         else if (RD2 == 0) {      //Second Sensor                              CCPR1L = 0b00011000;  //Set PWM to 15% after track loop                         }                         else if(RD1 == 0) {     // First Sensor                               CCPR1L = 0b01001010;   //Set PWM to 30% before track loop                            }                                                                                else if (RD0 == 0) {   // Third Sensor                              CCPR1L = 0b00110110;  // Set PWM to 22% after curve                          }                          else                          {                          }                          }        }    }    }    }}}