Robot

Dependencies:   HIDScope MODSERIAL Motordriver QEI Servo mbed

Fork of The_Claw_with_EMG_Control_PID by Meike Froklage

Committer:
meikefrok
Date:
Wed Oct 19 11:03:34 2016 +0000
Revision:
2:ad4b181a6422
Parent:
1:1d2208dce484
Child:
3:3a671d01bcb8
Button for Base Cart Arm, with LED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
megrootens 0:048fbd80203e 1 #include "mbed.h"
megrootens 0:048fbd80203e 2
megrootens 0:048fbd80203e 3 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
megrootens 0:048fbd80203e 4 // Serial communication using MODSERIAL
megrootens 0:048fbd80203e 5 #define SERIAL_BAUD 115200 // baud rate for serial communication
megrootens 0:048fbd80203e 6 #include "MODSERIAL.h"
megrootens 0:048fbd80203e 7 MODSERIAL pc(USBTX,USBRX);
megrootens 0:048fbd80203e 8
megrootens 0:048fbd80203e 9 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
megrootens 0:048fbd80203e 10 // Timing
megrootens 0:048fbd80203e 11 const float kTimeLedToggle = 0.25f; // period with which to toggle LED
megrootens 0:048fbd80203e 12 const float kTimePrintSerial = 1.0f;// period with which data is printed
megrootens 0:048fbd80203e 13
megrootens 0:048fbd80203e 14 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >
megrootens 0:048fbd80203e 15 // constants
megrootens 0:048fbd80203e 16 const int kLedOn = 0; // LED on if 0
megrootens 0:048fbd80203e 17
megrootens 0:048fbd80203e 18 // LEDs
meikefrok 2:ad4b181a6422 19 DigitalOut led_r(LED_RED);
meikefrok 2:ad4b181a6422 20 DigitalOut led_g(LED_GREEN);
meikefrok 2:ad4b181a6422 21 DigitalOut led_b(LED_BLUE);
megrootens 0:048fbd80203e 22
megrootens 0:048fbd80203e 23 // ID of led that should blink; *volatile* because changed by interrupt
meikefrok 2:ad4b181a6422 24 volatile int part_id = 0;
megrootens 0:048fbd80203e 25
megrootens 0:048fbd80203e 26 // counters
megrootens 0:048fbd80203e 27 int num_turned_on_0 = 0; // count number of times red LED turned on
megrootens 0:048fbd80203e 28 int num_turned_on_1 = 0; // count number of times green LED turned on
megrootens 0:048fbd80203e 29 int num_turned_on_2 = 0; // count number of times blue LED turned on
megrootens 0:048fbd80203e 30
megrootens 0:048fbd80203e 31
megrootens 0:048fbd80203e 32
megrootens 0:048fbd80203e 33 /**
megrootens 0:048fbd80203e 34 * Toggle / Switch / Blink the state of the LED.
megrootens 0:048fbd80203e 35 * Count the number of times the LED was turned ON.
megrootens 0:048fbd80203e 36 * We have quite some duplicate code here; this is not ideal and could be solved
megrootens 0:048fbd80203e 37 * using more 'advanced' concepts such as arrays and enums.
megrootens 0:048fbd80203e 38 * However, this is outside the scope of this course for now.
megrootens 0:048fbd80203e 39 */
megrootens 0:048fbd80203e 40 void ToggleLed()
megrootens 0:048fbd80203e 41 {
meikefrok 2:ad4b181a6422 42 switch (part_id) {
megrootens 0:048fbd80203e 43 case 0: {
megrootens 0:048fbd80203e 44 // LED 0 to toggle and count:
meikefrok 2:ad4b181a6422 45 led_r = kLedOn;
meikefrok 2:ad4b181a6422 46 if (led_r == kLedOn) {
megrootens 0:048fbd80203e 47 num_turned_on_0++;
megrootens 0:048fbd80203e 48 }
megrootens 0:048fbd80203e 49
megrootens 0:048fbd80203e 50 // LEDs to turn off:
meikefrok 2:ad4b181a6422 51 led_g = not kLedOn;
meikefrok 2:ad4b181a6422 52 led_b = not kLedOn;
megrootens 0:048fbd80203e 53
megrootens 0:048fbd80203e 54 break;
megrootens 0:048fbd80203e 55 }
megrootens 0:048fbd80203e 56 case 1: {
megrootens 0:048fbd80203e 57 // LED 1 to toggle and count:
meikefrok 2:ad4b181a6422 58 led_g = kLedOn;
meikefrok 2:ad4b181a6422 59 if (led_g == kLedOn) {
megrootens 0:048fbd80203e 60 num_turned_on_1++;
megrootens 0:048fbd80203e 61 }
megrootens 0:048fbd80203e 62
megrootens 0:048fbd80203e 63 // LEDs to turn off:
meikefrok 2:ad4b181a6422 64 led_r = not kLedOn;
meikefrok 2:ad4b181a6422 65 led_b = not kLedOn;
megrootens 0:048fbd80203e 66
megrootens 0:048fbd80203e 67 break;
megrootens 0:048fbd80203e 68 }
megrootens 0:048fbd80203e 69 case 2: {
megrootens 0:048fbd80203e 70 // LED 2 to toggle and count:
meikefrok 2:ad4b181a6422 71 led_b = kLedOn;
meikefrok 2:ad4b181a6422 72 if (led_b == kLedOn) {
megrootens 0:048fbd80203e 73 num_turned_on_2++;
megrootens 0:048fbd80203e 74 }
megrootens 0:048fbd80203e 75
megrootens 0:048fbd80203e 76 // LEDs to turn off:
meikefrok 2:ad4b181a6422 77 led_r = not kLedOn;
meikefrok 2:ad4b181a6422 78 led_g = not kLedOn;
megrootens 0:048fbd80203e 79
megrootens 0:048fbd80203e 80 break;
megrootens 0:048fbd80203e 81 }
megrootens 0:048fbd80203e 82 }
megrootens 0:048fbd80203e 83 }
megrootens 0:048fbd80203e 84
megrootens 0:048fbd80203e 85 /**
megrootens 0:048fbd80203e 86 * Print the number of times each LED was turned on through serial communication
megrootens 0:048fbd80203e 87 */
megrootens 0:048fbd80203e 88 void PrintSerial()
megrootens 0:048fbd80203e 89 {
megrootens 0:048fbd80203e 90 pc.printf("*Status*\r\n\tn_r = %d\r\n\tn_g = %d\r\n\tn_b = %d\r\n\r\n",
megrootens 0:048fbd80203e 91 num_turned_on_0, num_turned_on_1, num_turned_on_2);
megrootens 0:048fbd80203e 92 }
megrootens 0:048fbd80203e 93
megrootens 0:048fbd80203e 94 /**
megrootens 0:048fbd80203e 95 * Switch the led id that blinks
megrootens 0:048fbd80203e 96 * led_blink_id goes from 0 -> 1 -> 2 -> 0 -> ...
megrootens 0:048fbd80203e 97 * @ensure led_blink_id = ++led_blink_id % kNumStates
megrootens 0:048fbd80203e 98 */
meikefrok 2:ad4b181a6422 99 void SetValue2() {
meikefrok 2:ad4b181a6422 100 part_id = 0;
meikefrok 2:ad4b181a6422 101 }
meikefrok 2:ad4b181a6422 102
meikefrok 2:ad4b181a6422 103 void SetValue3() {
meikefrok 2:ad4b181a6422 104 part_id = 1;
meikefrok 2:ad4b181a6422 105 }
meikefrok 2:ad4b181a6422 106
meikefrok 2:ad4b181a6422 107
meikefrok 2:ad4b181a6422 108 void SetValue4() {
meikefrok 2:ad4b181a6422 109 part_id = 2;
megrootens 0:048fbd80203e 110 }
megrootens 0:048fbd80203e 111
meikefrok 2:ad4b181a6422 112
megrootens 0:048fbd80203e 113 /**
megrootens 0:048fbd80203e 114 * Main loop.
megrootens 0:048fbd80203e 115 */
megrootens 0:048fbd80203e 116 int main()
megrootens 0:048fbd80203e 117 {
megrootens 0:048fbd80203e 118 // Serial comm baud rate
megrootens 0:048fbd80203e 119 pc.baud(SERIAL_BAUD);
megrootens 0:048fbd80203e 120 pc.printf("\r\n**RESET**\r\n");
megrootens 0:048fbd80203e 121
megrootens 0:048fbd80203e 122 // Turn off all LEDs initially
meikefrok 2:ad4b181a6422 123 led_r = not kLedOn;
meikefrok 2:ad4b181a6422 124 led_g = not kLedOn;
meikefrok 2:ad4b181a6422 125 led_b = not kLedOn;
megrootens 0:048fbd80203e 126
megrootens 0:048fbd80203e 127 // Create ticker and attach LED toggle function
megrootens 0:048fbd80203e 128 Ticker tick_toggle_led;
megrootens 1:1d2208dce484 129 tick_toggle_led.attach(&ToggleLed,kTimeLedToggle);
megrootens 0:048fbd80203e 130
megrootens 0:048fbd80203e 131 // Create ticker and attach Print function
megrootens 0:048fbd80203e 132 Ticker tick_print_serial;
megrootens 0:048fbd80203e 133 tick_print_serial.attach(&PrintSerial,kTimePrintSerial);
megrootens 0:048fbd80203e 134
megrootens 0:048fbd80203e 135 // Create interrupt and attach switch function
meikefrok 2:ad4b181a6422 136 InterruptIn btn_base(D2);
meikefrok 2:ad4b181a6422 137 btn_base.fall(&SetValue2);
megrootens 0:048fbd80203e 138
meikefrok 2:ad4b181a6422 139 InterruptIn btn_cart(D3);
meikefrok 2:ad4b181a6422 140 btn_cart.fall(&SetValue3);
meikefrok 2:ad4b181a6422 141
meikefrok 2:ad4b181a6422 142 InterruptIn btn_arm(D4);
meikefrok 2:ad4b181a6422 143 btn_arm.fall(&SetValue4);
meikefrok 2:ad4b181a6422 144
megrootens 0:048fbd80203e 145 while (true);
megrootens 0:048fbd80203e 146 }
meikefrok 2:ad4b181a6422 147
meikefrok 2:ad4b181a6422 148
meikefrok 2:ad4b181a6422 149