Switches between A en B bij pressing a button

Dependencies:   MODSERIAL Motordriver mbed

Committer:
meikefrok
Date:
Fri Oct 14 13:18:28 2016 +0000
Revision:
0:60a905a48133
Add motor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
meikefrok 0:60a905a48133 1 #include "mbed.h"
meikefrok 0:60a905a48133 2 #define SERIAL_BAUD 115200 // baud rate for serial communication
meikefrok 0:60a905a48133 3 #include "MODSERIAL.h"
meikefrok 0:60a905a48133 4 #include "motordriver.h"
meikefrok 0:60a905a48133 5 MODSERIAL pc(USBTX,USBRX);
meikefrok 0:60a905a48133 6
meikefrok 0:60a905a48133 7 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
meikefrok 0:60a905a48133 8 // Timing
meikefrok 0:60a905a48133 9 const float kTimeLedToggle = 0.25f; // period with which to toggle LED
meikefrok 0:60a905a48133 10 const float kTimePrintSerial = 1.0f;// period with which data is printed
meikefrok 0:60a905a48133 11
meikefrok 0:60a905a48133 12 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
meikefrok 0:60a905a48133 13 // constants
meikefrok 0:60a905a48133 14 const int kNumLeds = 3; // three LEDs
meikefrok 0:60a905a48133 15 const int kLedOn = 0; // LED on if 0
meikefrok 0:60a905a48133 16
meikefrok 0:60a905a48133 17 // LEDs
meikefrok 0:60a905a48133 18 DigitalOut led_0(LED_RED);
meikefrok 0:60a905a48133 19 DigitalOut led_1(LED_GREEN);
meikefrok 0:60a905a48133 20
meikefrok 0:60a905a48133 21 // ID of led that should blink; *volatile* because changed by interrupt
meikefrok 0:60a905a48133 22 volatile int led_blink_id = 0;
meikefrok 0:60a905a48133 23
meikefrok 0:60a905a48133 24 // counters
meikefrok 0:60a905a48133 25 int num_turned_on_0 = 0; // count number of times red LED turned on
meikefrok 0:60a905a48133 26 int num_turned_on_1 = 0; // count number of times green LED turned on
meikefrok 0:60a905a48133 27
meikefrok 0:60a905a48133 28 //Safety for Motor
meikefrok 0:60a905a48133 29 int Brakeable; //cna the motor driver break
meikefrok 0:60a905a48133 30 int sign; //prevents throwing the motor from full foward to full reverse and stuff melting.
meikefrok 0:60a905a48133 31
meikefrok 0:60a905a48133 32
meikefrok 0:60a905a48133 33 //Motor
meikefrok 0:60a905a48133 34 Motor A(D6, D7, D7, Brakeable); // pwm, fwd, rev, brake
meikefrok 0:60a905a48133 35 Motor B(D5, D4, D4, Brakeable); // pwm, fwd, rev, brake
meikefrok 0:60a905a48133 36
meikefrok 0:60a905a48133 37 /**
meikefrok 0:60a905a48133 38 * Toggle / Switch / Blink the state of the LED.
meikefrok 0:60a905a48133 39 * Count the number of times the LED was turned ON.
meikefrok 0:60a905a48133 40 * We have quite some duplicate code here; this is not ideal and could be solved
meikefrok 0:60a905a48133 41 * using more 'advanced' concepts such as arrays and enums.
meikefrok 0:60a905a48133 42 * However, this is outside the scope of this course for now.
meikefrok 0:60a905a48133 43 */
meikefrok 0:60a905a48133 44 void ToggleLed()
meikefrok 0:60a905a48133 45 {
meikefrok 0:60a905a48133 46 switch (led_blink_id) {
meikefrok 0:60a905a48133 47 case 0: {
meikefrok 0:60a905a48133 48 // LED 0 to toggle and count:
meikefrok 0:60a905a48133 49 led_0 = not led_0;
meikefrok 0:60a905a48133 50 A.speed(0.5) == 0.5; //If speed is 0, the motor doensn't rotate.
meikefrok 0:60a905a48133 51 B.speed(0) == 0;
meikefrok 0:60a905a48133 52 if (led_0 == kLedOn) {
meikefrok 0:60a905a48133 53 num_turned_on_0++;
meikefrok 0:60a905a48133 54 }
meikefrok 0:60a905a48133 55
meikefrok 0:60a905a48133 56 // LEDs to turn off:
meikefrok 0:60a905a48133 57 led_1 = not kLedOn;
meikefrok 0:60a905a48133 58
meikefrok 0:60a905a48133 59 break;
meikefrok 0:60a905a48133 60 }
meikefrok 0:60a905a48133 61 case 1: {
meikefrok 0:60a905a48133 62 // LED 1 to toggle and count:
meikefrok 0:60a905a48133 63 led_1 = not led_1;
meikefrok 0:60a905a48133 64 A.speed(0) == 0; //If speed is 0, the motor doensn't rotate.
meikefrok 0:60a905a48133 65 B.speed(0.5) == 0.5;
meikefrok 0:60a905a48133 66 if (led_1 == kLedOn) {
meikefrok 0:60a905a48133 67 num_turned_on_1++;
meikefrok 0:60a905a48133 68 }
meikefrok 0:60a905a48133 69
meikefrok 0:60a905a48133 70 // LEDs to turn off:
meikefrok 0:60a905a48133 71 led_0 = not kLedOn;
meikefrok 0:60a905a48133 72
meikefrok 0:60a905a48133 73 break;
meikefrok 0:60a905a48133 74 }
meikefrok 0:60a905a48133 75 }
meikefrok 0:60a905a48133 76 }
meikefrok 0:60a905a48133 77
meikefrok 0:60a905a48133 78 /**
meikefrok 0:60a905a48133 79 * Print the number of times each LED was turned on through serial communication
meikefrok 0:60a905a48133 80 */
meikefrok 0:60a905a48133 81 void PrintSerial()
meikefrok 0:60a905a48133 82 {
meikefrok 0:60a905a48133 83 pc.printf("*Status*\r\n\tn_r = %d\r\n\tn_g = %d\r\n\tn_b = %d\r\n\r\n",
meikefrok 0:60a905a48133 84 num_turned_on_0, num_turned_on_1);
meikefrok 0:60a905a48133 85 }
meikefrok 0:60a905a48133 86
meikefrok 0:60a905a48133 87 /**
meikefrok 0:60a905a48133 88 * Switch the led id that blinks
meikefrok 0:60a905a48133 89 * led_blink_id goes from 0 -> 1 -> 2 -> 0 -> ...
meikefrok 0:60a905a48133 90 * @ensure led_blink_id = ++led_blink_id % kNumStates
meikefrok 0:60a905a48133 91 */
meikefrok 0:60a905a48133 92 void SwitchLedColor() {
meikefrok 0:60a905a48133 93 led_blink_id++;
meikefrok 0:60a905a48133 94 if (led_blink_id>=kNumLeds) {
meikefrok 0:60a905a48133 95 // could also use modulo ('%') operator
meikefrok 0:60a905a48133 96 led_blink_id = 0;
meikefrok 0:60a905a48133 97 }
meikefrok 0:60a905a48133 98 }
meikefrok 0:60a905a48133 99
meikefrok 0:60a905a48133 100 /**
meikefrok 0:60a905a48133 101 * Main loop.
meikefrok 0:60a905a48133 102 */
meikefrok 0:60a905a48133 103 int main()
meikefrok 0:60a905a48133 104 {
meikefrok 0:60a905a48133 105 // Serial comm baud rate
meikefrok 0:60a905a48133 106 pc.baud(SERIAL_BAUD);
meikefrok 0:60a905a48133 107 pc.printf("\r\n**RESET**\r\n");
meikefrok 0:60a905a48133 108
meikefrok 0:60a905a48133 109 // Turn off all LEDs initially
meikefrok 0:60a905a48133 110 led_0 = not kLedOn;
meikefrok 0:60a905a48133 111 led_1 = not kLedOn;
meikefrok 0:60a905a48133 112
meikefrok 0:60a905a48133 113 // Create ticker and attach LED toggle function
meikefrok 0:60a905a48133 114 Ticker tick_toggle_led;
meikefrok 0:60a905a48133 115 tick_toggle_led.attach(&ToggleLed,kTimeLedToggle);
meikefrok 0:60a905a48133 116
meikefrok 0:60a905a48133 117 // Create ticker and attach Print function
meikefrok 0:60a905a48133 118 Ticker tick_print_serial;
meikefrok 0:60a905a48133 119 tick_print_serial.attach(&PrintSerial,kTimePrintSerial);
meikefrok 0:60a905a48133 120
meikefrok 0:60a905a48133 121 // Create interrupt and attach switch function
meikefrok 0:60a905a48133 122 InterruptIn sw2(SW2);
meikefrok 0:60a905a48133 123 sw2.fall(&SwitchLedColor);
meikefrok 0:60a905a48133 124
meikefrok 0:60a905a48133 125 while (true);
meikefrok 0:60a905a48133 126 }