This project will enable remote control of a motorised turntable via a WiFi enabled TCP link using ACKme's (http://ack.me/) Wi-Fi enablement platform
main.cpp@1:7b420a2ea7db, 2014-08-26 (annotated)
- Committer:
- Stathisn
- Date:
- Tue Aug 26 12:33:27 2014 +0000
- Revision:
- 1:7b420a2ea7db
- Parent:
- 0:01fd80c0a524
- Child:
- 2:a73037a7d85d
End of day progress. Requires clean up.; Added interrupt code to handle encoder rising/falling edges and limit switch falling edge (active low signal); Added code to handle initialisation and calibration of the turntable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Stathisn | 0:01fd80c0a524 | 1 | #include "mbed.h" |
Stathisn | 0:01fd80c0a524 | 2 | |
Stathisn | 0:01fd80c0a524 | 3 | enum calib_state_t { |
Stathisn | 0:01fd80c0a524 | 4 | UNCALIBRATED, |
Stathisn | 1:7b420a2ea7db | 5 | INITIALISING, |
Stathisn | 0:01fd80c0a524 | 6 | CALIBRATING, |
Stathisn | 1:7b420a2ea7db | 7 | CALIBRATED}; |
Stathisn | 0:01fd80c0a524 | 8 | |
Stathisn | 0:01fd80c0a524 | 9 | DigitalOut my_led(LED1); // Sanity LED |
Stathisn | 0:01fd80c0a524 | 10 | DigitalOut ttDrive(PA_14); // the drive signal for the turntable |
Stathisn | 0:01fd80c0a524 | 11 | InterruptIn encoder(PA_13); // Signal from encoder |
Stathisn | 0:01fd80c0a524 | 12 | InterruptIn limitSwitch(PA_15); // Signal from limit switch |
Stathisn | 0:01fd80c0a524 | 13 | |
Stathisn | 1:7b420a2ea7db | 14 | calib_state_t calib_state = UNCALIBRATED; // Flag for calibration state |
Stathisn | 0:01fd80c0a524 | 15 | int encoderMax = 0; // Calibrated maximum value for the encoder |
Stathisn | 1:7b420a2ea7db | 16 | int encoderCurrent = 0; // Keeps track of the current position of the turn table |
Stathisn | 1:7b420a2ea7db | 17 | bool limitFlag = 0; |
Stathisn | 0:01fd80c0a524 | 18 | |
Stathisn | 1:7b420a2ea7db | 19 | void calibrate(); // synchronises and calibrates MCU to turntable |
Stathisn | 1:7b420a2ea7db | 20 | void incrementEncoder(); // increments value i, used as an interrupt target |
Stathisn | 1:7b420a2ea7db | 21 | void resetEncoder(); |
Stathisn | 1:7b420a2ea7db | 22 | |
Stathisn | 1:7b420a2ea7db | 23 | |
Stathisn | 1:7b420a2ea7db | 24 | ConsoleSerial consoleSerial(SERIAL_TX, SERIAL_RX); |
Stathisn | 0:01fd80c0a524 | 25 | |
Stathisn | 0:01fd80c0a524 | 26 | |
Stathisn | 0:01fd80c0a524 | 27 | int main() { |
Stathisn | 0:01fd80c0a524 | 28 | |
Stathisn | 0:01fd80c0a524 | 29 | |
Stathisn | 1:7b420a2ea7db | 30 | consoleSerial.setBaud(CONSOLE_BAUD); |
Stathisn | 1:7b420a2ea7db | 31 | |
Stathisn | 1:7b420a2ea7db | 32 | my_led = 0; |
Stathisn | 1:7b420a2ea7db | 33 | |
Stathisn | 0:01fd80c0a524 | 34 | // Step 1: Attach interrupt signals to appropriate functions |
Stathisn | 1:7b420a2ea7db | 35 | |
Stathisn | 1:7b420a2ea7db | 36 | limitSwitch.mode(PullUp); |
Stathisn | 1:7b420a2ea7db | 37 | limitSwitch.fall(&resetEncoder); //****Ask someone about correct handling of interrupts, including; naming convention (resetEncoder also handles calib_state), potential coupling realted to having a function perform two tasks |
Stathisn | 1:7b420a2ea7db | 38 | encoder.mode(PullDown); |
Stathisn | 1:7b420a2ea7db | 39 | encoder.fall(&incrementEncoder); |
Stathisn | 1:7b420a2ea7db | 40 | encoder.rise(&incrementEncoder); |
Stathisn | 1:7b420a2ea7db | 41 | |
Stathisn | 0:01fd80c0a524 | 42 | // Step 2: Calibrate MCU to the turn table |
Stathisn | 1:7b420a2ea7db | 43 | calibrate(); |
Stathisn | 0:01fd80c0a524 | 44 | // Step 3: Control turntable using hard coded commands |
Stathisn | 0:01fd80c0a524 | 45 | // Step 4: Include WiConnect Library |
Stathisn | 0:01fd80c0a524 | 46 | // Step 5: Interpret commands sent over TCP |
Stathisn | 0:01fd80c0a524 | 47 | |
Stathisn | 0:01fd80c0a524 | 48 | |
Stathisn | 0:01fd80c0a524 | 49 | // Configure sanity LED to blink |
Stathisn | 0:01fd80c0a524 | 50 | while(1) { |
Stathisn | 1:7b420a2ea7db | 51 | //my_led = !my_led; |
Stathisn | 0:01fd80c0a524 | 52 | wait(0.5); |
Stathisn | 0:01fd80c0a524 | 53 | } |
Stathisn | 0:01fd80c0a524 | 54 | } |
Stathisn | 0:01fd80c0a524 | 55 | |
Stathisn | 1:7b420a2ea7db | 56 | void calibrate() |
Stathisn | 0:01fd80c0a524 | 57 | { |
Stathisn | 1:7b420a2ea7db | 58 | // Step 1: Set status to uncalibrated |
Stathisn | 1:7b420a2ea7db | 59 | calib_state = UNCALIBRATED; |
Stathisn | 1:7b420a2ea7db | 60 | // Step 2: Get the motor turning |
Stathisn | 1:7b420a2ea7db | 61 | ttDrive = 1; |
Stathisn | 1:7b420a2ea7db | 62 | calib_state = INITIALISING; |
Stathisn | 1:7b420a2ea7db | 63 | encoderMax = 0; // reset the maximum encoder value |
Stathisn | 1:7b420a2ea7db | 64 | |
Stathisn | 0:01fd80c0a524 | 65 | // Step 2: Continue turning until calibration state is "CALIBRATED" |
Stathisn | 0:01fd80c0a524 | 66 | // the actual calibration will be handled by interrupts |
Stathisn | 1:7b420a2ea7db | 67 | |
Stathisn | 1:7b420a2ea7db | 68 | while (calib_state != CALIBRATED) |
Stathisn | 1:7b420a2ea7db | 69 | { |
Stathisn | 1:7b420a2ea7db | 70 | // run time reaches here....my_led = 1; |
Stathisn | 1:7b420a2ea7db | 71 | my_led = limitFlag; |
Stathisn | 1:7b420a2ea7db | 72 | if(limitFlag == 1) |
Stathisn | 1:7b420a2ea7db | 73 | { |
Stathisn | 1:7b420a2ea7db | 74 | my_led = 1; |
Stathisn | 1:7b420a2ea7db | 75 | // run time does not reach here |
Stathisn | 1:7b420a2ea7db | 76 | // Step 2: Process the calibration state |
Stathisn | 1:7b420a2ea7db | 77 | limitFlag = 0; |
Stathisn | 1:7b420a2ea7db | 78 | switch(calib_state) |
Stathisn | 1:7b420a2ea7db | 79 | { |
Stathisn | 1:7b420a2ea7db | 80 | case (UNCALIBRATED): |
Stathisn | 1:7b420a2ea7db | 81 | calib_state = INITIALISING; |
Stathisn | 1:7b420a2ea7db | 82 | break; |
Stathisn | 1:7b420a2ea7db | 83 | case (INITIALISING): |
Stathisn | 1:7b420a2ea7db | 84 | calib_state = CALIBRATING; |
Stathisn | 1:7b420a2ea7db | 85 | break; |
Stathisn | 1:7b420a2ea7db | 86 | case (CALIBRATING): |
Stathisn | 1:7b420a2ea7db | 87 | calib_state = CALIBRATED; |
Stathisn | 1:7b420a2ea7db | 88 | break; |
Stathisn | 1:7b420a2ea7db | 89 | case (CALIBRATED): |
Stathisn | 1:7b420a2ea7db | 90 | break; |
Stathisn | 1:7b420a2ea7db | 91 | default: |
Stathisn | 1:7b420a2ea7db | 92 | calib_state = UNCALIBRATED; |
Stathisn | 1:7b420a2ea7db | 93 | } |
Stathisn | 1:7b420a2ea7db | 94 | } |
Stathisn | 1:7b420a2ea7db | 95 | } |
Stathisn | 0:01fd80c0a524 | 96 | } |
Stathisn | 0:01fd80c0a524 | 97 | |
Stathisn | 1:7b420a2ea7db | 98 | void incrementEncoder() |
Stathisn | 0:01fd80c0a524 | 99 | { |
Stathisn | 0:01fd80c0a524 | 100 | // Step 1: De-refference pointer and increment the value |
Stathisn | 1:7b420a2ea7db | 101 | if (calib_state == CALIBRATING) |
Stathisn | 1:7b420a2ea7db | 102 | ++encoderMax; |
Stathisn | 1:7b420a2ea7db | 103 | else |
Stathisn | 1:7b420a2ea7db | 104 | ++encoderCurrent; |
Stathisn | 1:7b420a2ea7db | 105 | } |
Stathisn | 1:7b420a2ea7db | 106 | |
Stathisn | 1:7b420a2ea7db | 107 | void resetEncoder() |
Stathisn | 1:7b420a2ea7db | 108 | { |
Stathisn | 1:7b420a2ea7db | 109 | // code reaches here...my_led = 1; |
Stathisn | 1:7b420a2ea7db | 110 | // Step 1: Set encoderCurrent value to zero |
Stathisn | 1:7b420a2ea7db | 111 | encoderCurrent = 0; |
Stathisn | 1:7b420a2ea7db | 112 | |
Stathisn | 1:7b420a2ea7db | 113 | // Show that the limit switch has been pressed |
Stathisn | 1:7b420a2ea7db | 114 | limitFlag = 1; |
Stathisn | 1:7b420a2ea7db | 115 | my_led = limitFlag; |
Stathisn | 0:01fd80c0a524 | 116 | } |