
Lol smth
Dependencies: LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed
Fork of TDP_main_BartFork by
main.cpp@9:718987b106a8, 2015-02-23 (annotated)
- Committer:
- Reckstyle
- Date:
- Mon Feb 23 16:49:59 2015 +0000
- Revision:
- 9:718987b106a8
- Parent:
- 7:14af656b721f
- Child:
- 10:c9212bbfeae6
23/02 sensors working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Joseph_Penikis | 3:0274e082582e | 1 | // TESTING REPO COMMIT |
Joseph_Penikis | 3:0274e082582e | 2 | |
Reckstyle | 2:e2adb7ab2947 | 3 | /* |
Reckstyle | 2:e2adb7ab2947 | 4 | ****** MAIN PROGRAM ****** |
Reckstyle | 2:e2adb7ab2947 | 5 | |
Reckstyle | 2:e2adb7ab2947 | 6 | TODO : organisational routine still to be decided. |
Reckstyle | 2:e2adb7ab2947 | 7 | 3 main approaches: |
Reckstyle | 2:e2adb7ab2947 | 8 | -sequential prirority based tasks |
Reckstyle | 2:e2adb7ab2947 | 9 | -Timer/Ticker driven with sequential prirority based tasks |
Reckstyle | 2:e2adb7ab2947 | 10 | - interrupt driven from sensor inputs |
Reckstyle | 2:e2adb7ab2947 | 11 | |
Reckstyle | 2:e2adb7ab2947 | 12 | Please consider that although it is an embedded envrionment we are NOT creating a HARD-TIME real time system - delays can be dealt with |
Reckstyle | 2:e2adb7ab2947 | 13 | */ |
Reckstyle | 2:e2adb7ab2947 | 14 | |
Reckstyle | 2:e2adb7ab2947 | 15 | |
Reckstyle | 2:e2adb7ab2947 | 16 | |
Reckstyle | 0:5ca0450111f3 | 17 | #include "mbed.h" |
Reckstyle | 0:5ca0450111f3 | 18 | #include "sensor_measure.h" |
Joseph_Penikis | 5:2fc7bf0135d4 | 19 | #include "Motor_Driver.h" |
Reckstyle | 9:718987b106a8 | 20 | //#include "Sensors.h" |
Reckstyle | 0:5ca0450111f3 | 21 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 22 | #define PWM_PERIOD_US 10000 |
Reckstyle | 0:5ca0450111f3 | 23 | |
Joseph_Penikis | 7:14af656b721f | 24 | |
Joseph_Penikis | 6:bea13722aae7 | 25 | |
Reckstyle | 9:718987b106a8 | 26 | Serial pc(USBTX, USBRX); |
Reckstyle | 9:718987b106a8 | 27 | |
Reckstyle | 9:718987b106a8 | 28 | Timer MeasureTimer; //Timer used for measurement |
Reckstyle | 9:718987b106a8 | 29 | |
Reckstyle | 9:718987b106a8 | 30 | /* For deleting |
Reckstyle | 9:718987b106a8 | 31 | double measure1 (){ |
Reckstyle | 9:718987b106a8 | 32 | |
Reckstyle | 9:718987b106a8 | 33 | |
Reckstyle | 9:718987b106a8 | 34 | |
Reckstyle | 9:718987b106a8 | 35 | sensorTimer.reset(); |
Reckstyle | 9:718987b106a8 | 36 | double freq,period = 0.0; |
Reckstyle | 9:718987b106a8 | 37 | int n =0; //number of samples |
Reckstyle | 9:718987b106a8 | 38 | int sensor_old = 0; |
Reckstyle | 9:718987b106a8 | 39 | int sensor_new = 0;//variable to remember old sensor state |
Reckstyle | 9:718987b106a8 | 40 | double time_us = sensorTimer.read(); |
Reckstyle | 9:718987b106a8 | 41 | while (n < NUMBER_SAMPLES){ |
Reckstyle | 9:718987b106a8 | 42 | sensor_new = pin_right_right.read(); |
Reckstyle | 9:718987b106a8 | 43 | if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge ,sensor_old |
Reckstyle | 9:718987b106a8 | 44 | n++; |
Reckstyle | 9:718987b106a8 | 45 | } |
Reckstyle | 9:718987b106a8 | 46 | sensor_old = sensor_new; |
Reckstyle | 9:718987b106a8 | 47 | |
Reckstyle | 9:718987b106a8 | 48 | } |
Reckstyle | 9:718987b106a8 | 49 | double time_us2 = sensorTimer.read(); |
Reckstyle | 9:718987b106a8 | 50 | // pc.printf(" delta time is %f , time 2 is %f " , (time_us2 - time_us), time_us2); |
Reckstyle | 9:718987b106a8 | 51 | period = time_us2/((double)NUMBER_SAMPLES); // Get time |
Reckstyle | 9:718987b106a8 | 52 | freq = (1/period); // Convert period (in us) to frequency (Hz). Works up to 100kHz. |
Reckstyle | 9:718987b106a8 | 53 | // pc.printf(" period is %f ", period); |
Reckstyle | 9:718987b106a8 | 54 | |
Reckstyle | 9:718987b106a8 | 55 | return freq;//(freq < sensor.black_value*2)? 1 : 0; |
Reckstyle | 9:718987b106a8 | 56 | } |
Reckstyle | 9:718987b106a8 | 57 | */ |
Reckstyle | 9:718987b106a8 | 58 | |
Reckstyle | 0:5ca0450111f3 | 59 | int main() { |
Joseph_Penikis | 5:2fc7bf0135d4 | 60 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 61 | /* |
Joseph_Penikis | 5:2fc7bf0135d4 | 62 | Motor_Driver motors(PTD4, PTA12, PTA4, PTA5, PTC9, PTC8, PWM_PERIOD_US); |
Joseph_Penikis | 5:2fc7bf0135d4 | 63 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 64 | motors.setSpeed(1.0f); |
Joseph_Penikis | 5:2fc7bf0135d4 | 65 | motors.forward(); |
Joseph_Penikis | 5:2fc7bf0135d4 | 66 | motors.start(); |
Joseph_Penikis | 5:2fc7bf0135d4 | 67 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 68 | wait(3); |
Joseph_Penikis | 5:2fc7bf0135d4 | 69 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 70 | motors.stop() |
Joseph_Penikis | 5:2fc7bf0135d4 | 71 | motors.reverse(); |
Joseph_Penikis | 5:2fc7bf0135d4 | 72 | wait(200ms); |
Joseph_Penikis | 5:2fc7bf0135d4 | 73 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 74 | motors.start(); |
Joseph_Penikis | 5:2fc7bf0135d4 | 75 | */ |
Reckstyle | 9:718987b106a8 | 76 | // setup_counter(1000, 0); |
Reckstyle | 9:718987b106a8 | 77 | // float frequency = measure_frequency(CHANNEL_1); |
Joseph_Penikis | 5:2fc7bf0135d4 | 78 | |
Joseph_Penikis | 5:2fc7bf0135d4 | 79 | sensor_initialise(); // initialise sensor values |
Joseph_Penikis | 5:2fc7bf0135d4 | 80 | wait(1); //give time to set up the system |
Reckstyle | 9:718987b106a8 | 81 | |
Reckstyle | 9:718987b106a8 | 82 | sensorTimer.start(); // start timer for sensors |
Reckstyle | 9:718987b106a8 | 83 | pc.printf("Start..."); |
Reckstyle | 9:718987b106a8 | 84 | MeasureTimer.start(); |
Reckstyle | 9:718987b106a8 | 85 | |
Reckstyle | 9:718987b106a8 | 86 | while (1) { |
Reckstyle | 9:718987b106a8 | 87 | MeasureTimer.reset(); |
Reckstyle | 9:718987b106a8 | 88 | measure(right_right); |
Reckstyle | 9:718987b106a8 | 89 | pc.printf("Time taken for the whole thing is %f",MeasureTimer.read()); |
Reckstyle | 9:718987b106a8 | 90 | } |
Reckstyle | 0:5ca0450111f3 | 91 | |
Reckstyle | 0:5ca0450111f3 | 92 | } |