Robot

Dependencies:   HIDScope MODSERIAL Motordriver QEI Servo mbed

Fork of The_Claw_with_EMG_Control_PID by Meike Froklage

Committer:
megrootens
Date:
Fri Sep 16 15:27:36 2016 +0000
Revision:
0:048fbd80203e
Child:
1:1d2208dce484
Example solution Minor Biorobotics 2016; University of Twente,; Dept. of Biomechanical Engineering; Homework set 2, Exercise 2.

Who changed what in which revision?

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