d
Dependencies: Encoder HIDScope MMA8451Q MODSERIAL TextLCD mbed
main.cpp@0:77a3641d54d4, 2014-10-23 (annotated)
- Committer:
- phgbartels
- Date:
- Thu Oct 23 12:29:19 2014 +0000
- Revision:
- 0:77a3641d54d4
K9-robotarm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phgbartels | 0:77a3641d54d4 | 1 | //Script for Robotarm Quartile 9, Group 7 |
phgbartels | 0:77a3641d54d4 | 2 | |
phgbartels | 0:77a3641d54d4 | 3 | // including all libraries |
phgbartels | 0:77a3641d54d4 | 4 | //#include "mbed.h" //Main functionality |
phgbartels | 0:77a3641d54d4 | 5 | //#include "TextLCD.h" //LCD drivers |
phgbartels | 0:77a3641d54d4 | 6 | //#include "encoder.h" //CPR motor counter |
phgbartels | 0:77a3641d54d4 | 7 | //#include "rtos.h" //Real time operating system(onduidelijk wat deze library doet) |
phgbartels | 0:77a3641d54d4 | 8 | //#include "MODSERIAL.h" //non-blocking serial communication (RTOS compatible) (onduidelijk wat deze library doet) |
phgbartels | 0:77a3641d54d4 | 9 | |
phgbartels | 0:77a3641d54d4 | 10 | #include "mbed.h" |
phgbartels | 0:77a3641d54d4 | 11 | #include "MMA8451Q.h" |
phgbartels | 0:77a3641d54d4 | 12 | |
phgbartels | 0:77a3641d54d4 | 13 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
phgbartels | 0:77a3641d54d4 | 14 | #include "MODSERIAL.h" |
phgbartels | 0:77a3641d54d4 | 15 | #include "HIDScope.h" |
phgbartels | 0:77a3641d54d4 | 16 | //using namespace std; |
phgbartels | 0:77a3641d54d4 | 17 | // define MOTOR_POWER_LIMIT //Veiligheid |
phgbartels | 0:77a3641d54d4 | 18 | // define POSITION_ARM_LIMIT //Veiligheid |
phgbartels | 0:77a3641d54d4 | 19 | // define SPEED_ARM_LIMIT //Veiligheid |
phgbartels | 0:77a3641d54d4 | 20 | |
phgbartels | 0:77a3641d54d4 | 21 | void accelerometer() { |
phgbartels | 0:77a3641d54d4 | 22 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
phgbartels | 0:77a3641d54d4 | 23 | PwmOut rled(LED_RED); |
phgbartels | 0:77a3641d54d4 | 24 | PwmOut gled(LED_GREEN); |
phgbartels | 0:77a3641d54d4 | 25 | PwmOut bled(LED_BLUE); |
phgbartels | 0:77a3641d54d4 | 26 | |
phgbartels | 0:77a3641d54d4 | 27 | while (true) { |
phgbartels | 0:77a3641d54d4 | 28 | rled = 1.0 - abs(acc.getAccX()); |
phgbartels | 0:77a3641d54d4 | 29 | gled = 1.0 - abs(acc.getAccY()); |
phgbartels | 0:77a3641d54d4 | 30 | bled = 1.0 - abs(acc.getAccZ()); |
phgbartels | 0:77a3641d54d4 | 31 | wait(0.1); |
phgbartels | 0:77a3641d54d4 | 32 | } |
phgbartels | 0:77a3641d54d4 | 33 | } |
phgbartels | 0:77a3641d54d4 | 34 | |
phgbartels | 0:77a3641d54d4 | 35 | |
phgbartels | 0:77a3641d54d4 | 36 | //Define objects |
phgbartels | 0:77a3641d54d4 | 37 | AnalogIn emg0(PTB1); //Analog input |
phgbartels | 0:77a3641d54d4 | 38 | PwmOut red(LED_RED); //PWM output |
phgbartels | 0:77a3641d54d4 | 39 | Ticker log_timer; |
phgbartels | 0:77a3641d54d4 | 40 | MODSERIAL pc(USBTX,USBRX); |
phgbartels | 0:77a3641d54d4 | 41 | HIDScope scope(2); |
phgbartels | 0:77a3641d54d4 | 42 | |
phgbartels | 0:77a3641d54d4 | 43 | /** Looper function |
phgbartels | 0:77a3641d54d4 | 44 | * functions used for Ticker and Timeout should be of type void <name>(void) |
phgbartels | 0:77a3641d54d4 | 45 | * i.e. no input arguments, no output arguments. |
phgbartels | 0:77a3641d54d4 | 46 | * if you want to change a variable that you use in other places (for example in main) |
phgbartels | 0:77a3641d54d4 | 47 | * you will have to make that variable global in order to be able to reach it both from |
phgbartels | 0:77a3641d54d4 | 48 | * the function called at interrupt time, and in the main function. |
phgbartels | 0:77a3641d54d4 | 49 | * To make a variable global, define it under the includes. |
phgbartels | 0:77a3641d54d4 | 50 | * variables that are changed in the interrupt routine (written to) should be made |
phgbartels | 0:77a3641d54d4 | 51 | * 'volatile' to let the compiler know that those values may change outside the current context. |
phgbartels | 0:77a3641d54d4 | 52 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t emg_value" |
phgbartels | 0:77a3641d54d4 | 53 | * in the example below, the variable is not re-used in the main function, and is thus declared |
phgbartels | 0:77a3641d54d4 | 54 | * local in the looper function only. |
phgbartels | 0:77a3641d54d4 | 55 | **/ |
phgbartels | 0:77a3641d54d4 | 56 | void looper() |
phgbartels | 0:77a3641d54d4 | 57 | { |
phgbartels | 0:77a3641d54d4 | 58 | /*variable to store value in*/ |
phgbartels | 0:77a3641d54d4 | 59 | uint16_t emg_value; |
phgbartels | 0:77a3641d54d4 | 60 | /*put raw emg value both in red and in emg_value*/ |
phgbartels | 0:77a3641d54d4 | 61 | red.write(emg0.read()); // read float value (0..1 = 0..3.3V) |
phgbartels | 0:77a3641d54d4 | 62 | emg_value = emg0.read_u16(); // read direct ADC result (0..4096 = 0..3.3V) |
phgbartels | 0:77a3641d54d4 | 63 | /*send value to PC. Line below is used to prevent buffer overrun */ |
phgbartels | 0:77a3641d54d4 | 64 | if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) |
phgbartels | 0:77a3641d54d4 | 65 | pc.printf("%u\n",emg_value); |
phgbartels | 0:77a3641d54d4 | 66 | scope.set(0,emg_value); |
phgbartels | 0:77a3641d54d4 | 67 | scope.set(1,red.read()); |
phgbartels | 0:77a3641d54d4 | 68 | scope.send(); |
phgbartels | 0:77a3641d54d4 | 69 | /**When not using the LED, the above could also have been done this way: |
phgbartels | 0:77a3641d54d4 | 70 | * pc.printf("%u\n", emg0.read_u16()); |
phgbartels | 0:77a3641d54d4 | 71 | */ |
phgbartels | 0:77a3641d54d4 | 72 | } |
phgbartels | 0:77a3641d54d4 | 73 | |
phgbartels | 0:77a3641d54d4 | 74 | void HIDscope() |
phgbartels | 0:77a3641d54d4 | 75 | { |
phgbartels | 0:77a3641d54d4 | 76 | /*setup baudrate. Choose the same in your program on PC side*/ |
phgbartels | 0:77a3641d54d4 | 77 | pc.baud(115200); |
phgbartels | 0:77a3641d54d4 | 78 | /*set the period for the PWM to the red LED*/ |
phgbartels | 0:77a3641d54d4 | 79 | red.period_ms(2); |
phgbartels | 0:77a3641d54d4 | 80 | /**Here you attach the 'void looper(void)' function to the Ticker object |
phgbartels | 0:77a3641d54d4 | 81 | * The looper() function will be called every 0.01 seconds. |
phgbartels | 0:77a3641d54d4 | 82 | * Please mind that the parentheses after looper are omitted when using attach. |
phgbartels | 0:77a3641d54d4 | 83 | */ |
phgbartels | 0:77a3641d54d4 | 84 | log_timer.attach(looper, 0.005); |
phgbartels | 0:77a3641d54d4 | 85 | while(1) //Loop |
phgbartels | 0:77a3641d54d4 | 86 | { |
phgbartels | 0:77a3641d54d4 | 87 | /*Empty!*/ |
phgbartels | 0:77a3641d54d4 | 88 | /*Everything is handled by the interrupt routine now!*/ |
phgbartels | 0:77a3641d54d4 | 89 | } |
phgbartels | 0:77a3641d54d4 | 90 | } |
phgbartels | 0:77a3641d54d4 | 91 | int main() |
phgbartels | 0:77a3641d54d4 | 92 | { |
phgbartels | 0:77a3641d54d4 | 93 | HIDscope(); |
phgbartels | 0:77a3641d54d4 | 94 | //accelerometer(); |
phgbartels | 0:77a3641d54d4 | 95 | |
phgbartels | 0:77a3641d54d4 | 96 | } |
phgbartels | 0:77a3641d54d4 | 97 | |
phgbartels | 0:77a3641d54d4 | 98 | |
phgbartels | 0:77a3641d54d4 | 99 | //Nulpositie arm, ook na elke slag (terugkoppeling gebruiker ledje (groen/rood)) |
phgbartels | 0:77a3641d54d4 | 100 | //Nulmeting EMG, éénmalig bij opstarten (terugkoppeling gebruiker ledje (groen/rood)) |
phgbartels | 0:77a3641d54d4 | 101 | // Filteren --> werkelijke schaling (referentie) |
phgbartels | 0:77a3641d54d4 | 102 | //Meten EMG 2x(kiezen vlakken hoogte/breedte) |
phgbartels | 0:77a3641d54d4 | 103 | // RT - Filteren en Verwerking |
phgbartels | 0:77a3641d54d4 | 104 | // RT - vergelijking tov van referentie |
phgbartels | 0:77a3641d54d4 | 105 | // RT - terugkoppeling gebruiker via 3 gekleurde LEDjes (LEDjes aan) |
phgbartels | 0:77a3641d54d4 | 106 | // Gebruiker akkoord geven nav LEDjes (LEDjes knipperen) |
phgbartels | 0:77a3641d54d4 | 107 | //Akkoord doorsturen motor 2x |
phgbartels | 0:77a3641d54d4 | 108 | // PWM |
phgbartels | 0:77a3641d54d4 | 109 | // Direction |
phgbartels | 0:77a3641d54d4 | 110 | //RT - Regelaar |
phgbartels | 0:77a3641d54d4 | 111 | // RT - Terugkoppeling PWM (Encoder) |
phgbartels | 0:77a3641d54d4 | 112 | // RT - Terugkoppeling Direction (Encoder) |
phgbartels | 0:77a3641d54d4 | 113 | //Terug naar nulpositie arm |
phgbartels | 0:77a3641d54d4 | 114 | // Vanaf hier mainscript weer doorlopen, behalve nulmeting EMG |