This is a starting gate for a line following competition in which two cars are held up by a stepper motor and then released at the same time. A transmission signal is sent through an antenna to alert the ending line that the race timer should start and will only stop when the tripwire is broken.

Dependencies:   mMotor mbed

About

This program is intended to start two cars at the same time from a starting gate rig that uses a stepper motor to control the release of the cars. Two buttons control the release mechanism and once the cars are released, a signal is sent through an antenna to the ending gate rig which tells it to start its timers. This is specifically made for this application only and uses the custom mMotor library to drive a 5718m-05e-05 stepper motor.

Hardware

The circuit diagram is given below. Vs should be external to the mbed since the mbed can not provide enough current to drive the stepper motor.

/media/uploads/mdu7078/starting-gate-1.png

Circuit diagram constructed using CircuitLab.

Usage

The starting gate is controlled by two separate buttons which cause the gate to release or reset based on which button is pressed. The buttons themselves can only be pressed once before pressing the opposite button to prevent the gate from going too far in either direction.

Committer:
mdu7078
Date:
Wed Apr 24 18:36:21 2013 +0000
Revision:
2:0e23dd14e129
Parent:
1:244a23186799
Child:
3:6be11b32f35f
Tested working conditions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mdu7078 0:372aa18dd17d 1 /* * * * * * * * * * * * * * * * * * * * * * * * * * *
mdu7078 0:372aa18dd17d 2 * This times a race with up to two separate timers *
mdu7078 0:372aa18dd17d 3 * which start whenever a button press interrupt is *
mdu7078 0:372aa18dd17d 4 * detected. The timers are stopped only by *
mdu7078 0:372aa18dd17d 5 * recording a tripwire interrupt which will save *
mdu7078 0:372aa18dd17d 6 * the time and output it to the serial port. *
mdu7078 0:372aa18dd17d 7 * *
mdu7078 0:372aa18dd17d 8 * Created by: Michael Dushkoff (mad1841@rit.edu) *
mdu7078 0:372aa18dd17d 9 * * * * * * * * * * * * * * * * * * * * * * * * * * */
mdu7078 0:372aa18dd17d 10
mdu7078 0:372aa18dd17d 11 #include "mbed.h"
mdu7078 1:244a23186799 12 #include "mMotor.h"
mdu7078 0:372aa18dd17d 13
mdu7078 0:372aa18dd17d 14 /* Stepper motor constants, (change based on motor) */
mdu7078 2:0e23dd14e129 15 int step_speed = 3000; // set default motor speed
mdu7078 2:0e23dd14e129 16 int numstep = 200; // defines full turn of 360 degrees
mdu7078 0:372aa18dd17d 17
mdu7078 0:372aa18dd17d 18 /* Create two stepper motor controllers */
mdu7078 1:244a23186799 19 mMotor mt1(p33, p34, p35, p36);
mdu7078 1:244a23186799 20 mMotor mt2(p11, p12, p13, p14);
mdu7078 0:372aa18dd17d 21
mdu7078 0:372aa18dd17d 22 /* Create two individual timers */
mdu7078 0:372aa18dd17d 23 Timer t1;
mdu7078 0:372aa18dd17d 24 Timer t2;
mdu7078 0:372aa18dd17d 25
mdu7078 0:372aa18dd17d 26 /* Interrupt sensors */
mdu7078 0:372aa18dd17d 27 InterruptIn sEnd1(p20);
mdu7078 0:372aa18dd17d 28 InterruptIn sEnd2(p21);
mdu7078 0:372aa18dd17d 29 InterruptIn bInt1(p23);
mdu7078 0:372aa18dd17d 30 InterruptIn bInt2(p24);
mdu7078 0:372aa18dd17d 31
mdu7078 0:372aa18dd17d 32 /* Indicator LEDs */
mdu7078 0:372aa18dd17d 33 DigitalOut iled1(LED1);
mdu7078 0:372aa18dd17d 34 DigitalOut iled2(LED4);
mdu7078 0:372aa18dd17d 35 DigitalOut tled1(LED2); //Timer LED1
mdu7078 0:372aa18dd17d 36 DigitalOut tled2(LED3); //Timer LED2
mdu7078 0:372aa18dd17d 37
mdu7078 0:372aa18dd17d 38 /* Tells when to stop the timer */
mdu7078 0:372aa18dd17d 39 int end1 = 0;
mdu7078 0:372aa18dd17d 40 int end2 = 0;
mdu7078 0:372aa18dd17d 41
mdu7078 0:372aa18dd17d 42 /* This is a print button that allows the time to be outputted */
mdu7078 0:372aa18dd17d 43 DigitalIn button(p8);
mdu7078 0:372aa18dd17d 44
mdu7078 0:372aa18dd17d 45 /* Rise detection */
mdu7078 0:372aa18dd17d 46 int rise = 0;
mdu7078 0:372aa18dd17d 47
mdu7078 0:372aa18dd17d 48 /* This detects tripwire interrupt 1 */
mdu7078 0:372aa18dd17d 49 void l1(){
mdu7078 0:372aa18dd17d 50 if (end1 == -1){
mdu7078 0:372aa18dd17d 51 t1.stop();
mdu7078 0:372aa18dd17d 52 tled1 = 0;
mdu7078 0:372aa18dd17d 53 iled1 = 1;
mdu7078 0:372aa18dd17d 54 end1 = 1;
mdu7078 0:372aa18dd17d 55 }
mdu7078 0:372aa18dd17d 56 }
mdu7078 0:372aa18dd17d 57
mdu7078 0:372aa18dd17d 58 /* This detects tripwire interrupt 2 */
mdu7078 0:372aa18dd17d 59 void l2(){
mdu7078 0:372aa18dd17d 60 if (end2 == -1){
mdu7078 0:372aa18dd17d 61 t2.stop();
mdu7078 0:372aa18dd17d 62 tled2 = 0;
mdu7078 0:372aa18dd17d 63 iled2 = 1;
mdu7078 0:372aa18dd17d 64 end2 = 1;
mdu7078 0:372aa18dd17d 65 }
mdu7078 0:372aa18dd17d 66 }
mdu7078 0:372aa18dd17d 67
mdu7078 0:372aa18dd17d 68 /* This detects button interrupt 1 */
mdu7078 0:372aa18dd17d 69 void b1(){
mdu7078 0:372aa18dd17d 70 if (end1 == 0){
mdu7078 0:372aa18dd17d 71 end1 = -1;
mdu7078 0:372aa18dd17d 72 tled1 = 1;
mdu7078 0:372aa18dd17d 73 mt1.step(numstep,0,step_speed);
mdu7078 0:372aa18dd17d 74 t1.start();
mdu7078 0:372aa18dd17d 75 }
mdu7078 0:372aa18dd17d 76 }
mdu7078 0:372aa18dd17d 77
mdu7078 0:372aa18dd17d 78 /* This detects button interrupt 2 */
mdu7078 0:372aa18dd17d 79 void b2(){
mdu7078 0:372aa18dd17d 80 if (end2 == 0){
mdu7078 0:372aa18dd17d 81 end2 = -1;
mdu7078 0:372aa18dd17d 82 tled2 = 1;
mdu7078 0:372aa18dd17d 83 mt2.step(numstep,0,step_speed);
mdu7078 0:372aa18dd17d 84 t2.start();
mdu7078 0:372aa18dd17d 85 }
mdu7078 0:372aa18dd17d 86 }
mdu7078 0:372aa18dd17d 87
mdu7078 0:372aa18dd17d 88 int main() {
mdu7078 1:244a23186799 89 printf("Starting up...\n\r");
mdu7078 1:244a23186799 90
mdu7078 0:372aa18dd17d 91 /* Set the LEDs off */
mdu7078 0:372aa18dd17d 92 iled1 = 0;
mdu7078 0:372aa18dd17d 93 iled2 = 0;
mdu7078 0:372aa18dd17d 94 tled1 = 0;
mdu7078 0:372aa18dd17d 95 tled2 = 0;
mdu7078 0:372aa18dd17d 96
mdu7078 0:372aa18dd17d 97 /* Attach interrupt service routine to pins */
mdu7078 0:372aa18dd17d 98 sEnd1.fall(&l1);
mdu7078 0:372aa18dd17d 99 sEnd2.fall(&l2);
mdu7078 1:244a23186799 100 bInt1.rise(&b1);
mdu7078 1:244a23186799 101 bInt2.rise(&b2);
mdu7078 0:372aa18dd17d 102
mdu7078 0:372aa18dd17d 103 /* Set the stepper motors to default position */
mdu7078 0:372aa18dd17d 104 mt1.step(numstep,1,step_speed);
mdu7078 0:372aa18dd17d 105 mt2.step(numstep,1,step_speed);
mdu7078 0:372aa18dd17d 106
mdu7078 1:244a23186799 107 printf("Ready!\n\r");
mdu7078 0:372aa18dd17d 108 while(1) {
mdu7078 1:244a23186799 109 //printf("%d", button.read());
mdu7078 1:244a23186799 110 if (button.read() == 1 && rise == 0){
mdu7078 0:372aa18dd17d 111 /* Set rise detector to high */
mdu7078 0:372aa18dd17d 112 rise = 1;
mdu7078 0:372aa18dd17d 113
mdu7078 0:372aa18dd17d 114 /* Check Track 1 */
mdu7078 0:372aa18dd17d 115 if (end1 == 1){
mdu7078 1:244a23186799 116 printf("========== Track 1 Time ==========\n\r");
mdu7078 1:244a23186799 117 printf(" Finished in: %lf seconds\n\r", t1.read());
mdu7078 1:244a23186799 118 printf("==================================\n\r\n\r");
mdu7078 0:372aa18dd17d 119 }
mdu7078 0:372aa18dd17d 120 else {
mdu7078 1:244a23186799 121 printf("Track 1 has not finished\n\r\n\r");
mdu7078 0:372aa18dd17d 122 }
mdu7078 0:372aa18dd17d 123
mdu7078 0:372aa18dd17d 124 /* Check Track 2 */
mdu7078 0:372aa18dd17d 125 if (end2 == 1){
mdu7078 1:244a23186799 126 printf("========== Track 2 Time ==========\n\r");
mdu7078 1:244a23186799 127 printf(" Finished in: %lf seconds\n\r", t2.read());
mdu7078 1:244a23186799 128 printf("==================================\n\r\n\r");
mdu7078 0:372aa18dd17d 129 }
mdu7078 0:372aa18dd17d 130 else {
mdu7078 1:244a23186799 131 printf("Track 2 has not finished\n\r\n\r");
mdu7078 0:372aa18dd17d 132 }
mdu7078 0:372aa18dd17d 133 }
mdu7078 1:244a23186799 134 else if (button.read() == 0 && rise == 1){
mdu7078 0:372aa18dd17d 135 /* Reset rise detection */
mdu7078 1:244a23186799 136 rise = 2;
mdu7078 1:244a23186799 137 wait (0.001); //Debounce
mdu7078 1:244a23186799 138 }
mdu7078 1:244a23186799 139 else if (button.read() == 0 && rise == 2){
mdu7078 0:372aa18dd17d 140 rise = 0;
mdu7078 0:372aa18dd17d 141 }
mdu7078 0:372aa18dd17d 142 }
mdu7078 0:372aa18dd17d 143 }