code of robot bike

Dependencies:   SDFileSystem mbed

Fork of Robot_Bicycle by Chris LU

Committer:
YCTung
Date:
Tue Jun 21 18:11:46 2016 +0000
Revision:
1:709be64ca63c
Parent:
0:830ddddc129f
Child:
2:ec30613f2b2b
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YCTung 0:830ddddc129f 1 #include "mbed.h"
YCTung 0:830ddddc129f 2
YCTung 0:830ddddc129f 3 #include "RobotServo.h"
YCTung 0:830ddddc129f 4 #include "SensorFusion.h"
YCTung 0:830ddddc129f 5 #include "SPI_9dSensor.h"
YCTung 0:830ddddc129f 6 #include "RobotBicycleConst.h"
YCTung 1:709be64ca63c 7 /****************************
YCTung 1:709be64ca63c 8 Wiring Map
YCTung 0:830ddddc129f 9
YCTung 1:709be64ca63c 10 CK3 PC_10 | PC_11 MI3||re PC_9 | PC_8 rw
YCTung 1:709be64ca63c 11 MO3 PC_12 | PD_2 || |-----------------|
YCTung 1:709be64ca63c 12 3.3V | E5V ||rs |PB_8 D15 | PC_6 |
YCTung 1:709be64ca63c 13 BOOT0 | GND ||ra |PB_9 D14 | PC_5 |
YCTung 1:709be64ca63c 14 |---------------| || |AVDD | U5V |
YCTung 1:709be64ca63c 15 |NC | NC | || |GND | NC |
YCTung 1:709be64ca63c 16 |NC | IOREF | ||rk |PA_5 D13 | PA_12|
YCTung 1:709be64ca63c 17 |PA_13 | NRST | || |PA_6 D12 | PA_11| rl
YCTung 1:709be64ca63c 18 |PA_14 | 3.3V | || |PA_7 D11 | PB_12|
YCTung 1:709be64ca63c 19 |PA_15 | 5.0V | ||rb |PB_6 D10 | NC |
YCTung 1:709be64ca63c 20 |GND | GND | || |PC_7 D9 | GND |
YCTung 1:709be64ca63c 21 |PB_7 | GND | ||lb |PA_9 D8 | PB_2 |CSG
YCTung 1:709be64ca63c 22 |PC_13 | VIN | || |-----------------|
YCTung 1:709be64ca63c 23 |---------------| || |-----------------|
YCTung 1:709be64ca63c 24 PC_14 | NC ||ll |PA_8 D7 | PB_1 |CSX
YCTung 1:709be64ca63c 25 |---------------| ||lk |PB_10 D6 | PB_15|MO2
YCTung 1:709be64ca63c 26 |PC_15 | PA_0 A0| ||la |PB_4 D5 | PB_14|MI2
YCTung 1:709be64ca63c 27 |PH_0 | PA_1 A1| ||ls |PB_5 D4 | PB_13|CK2
YCTung 1:709be64ca63c 28 |PH_1 | PA_4 A2| ||le |PB_3 D3 | AGND |
YCTung 1:709be64ca63c 29 |VBAT | PB_0 A3| ||lw |PA_10 D2 | PC_4 |
YCTung 1:709be64ca63c 30 |PC_2 | PC_1 A4| || |PA_2 D1 | NC |
YCTung 1:709be64ca63c 31 |PC_3 | PC_0 A5| || |PA_3 D0 | NC |
YCTung 1:709be64ca63c 32 |---------------| || |-----------------|
YCTung 1:709be64ca63c 33 ****************************/
YCTung 0:830ddddc129f 34 AnalogIn analog_value(A5);//When using the ADC module, those pins connected to AnalogIn can't be used to output.
YCTung 0:830ddddc129f 35
YCTung 0:830ddddc129f 36 Ticker timer1;
YCTung 0:830ddddc129f 37 Ticker timer2;
YCTung 0:830ddddc129f 38
YCTung 0:830ddddc129f 39 //Serial pc(D1, D0) declared in SPI_9dSensor.h
YCTung 0:830ddddc129f 40
YCTung 0:830ddddc129f 41 float meas, L_inver;
YCTung 0:830ddddc129f 42
YCTung 0:830ddddc129f 43 void timer1_interrupt(void);
YCTung 0:830ddddc129f 44 void timer2_interrupt(void);
YCTung 0:830ddddc129f 45
YCTung 0:830ddddc129f 46 int main() {
YCTung 0:830ddddc129f 47 setupServo();
YCTung 1:709be64ca63c 48 setup_spi();
YCTung 0:830ddddc129f 49
YCTung 0:830ddddc129f 50 timer1.attach_us(&timer1_interrupt, 4000);//4.0ms interrupt period (250 Hz)
YCTung 0:830ddddc129f 51 timer2.attach_us(&timer2_interrupt, 4098);//4.098ms interrupt period (244 Hz)
YCTung 0:830ddddc129f 52
YCTung 0:830ddddc129f 53 pc.printf("\nAnalogIn example\r\n");
YCTung 0:830ddddc129f 54
YCTung 0:830ddddc129f 55 while(1) {
YCTung 0:830ddddc129f 56 pc.printf("measure: %f V,\t%f cm\r\n", meas, 1/L_inver);
YCTung 0:830ddddc129f 57 wait(0.1); // 200 ms
YCTung 0:830ddddc129f 58 }
YCTung 0:830ddddc129f 59 }
YCTung 0:830ddddc129f 60
YCTung 0:830ddddc129f 61 void timer1_interrupt(void)
YCTung 0:830ddddc129f 62 {
YCTung 0:830ddddc129f 63 meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
YCTung 0:830ddddc129f 64 meas = meas * 3.3f; // Change the value to be in the 0 to 3300 range
YCTung 0:830ddddc129f 65 L_inver = 0.0063f * meas - 0.005769f;
YCTung 0:830ddddc129f 66 }
YCTung 0:830ddddc129f 67
YCTung 0:830ddddc129f 68 void timer2_interrupt(void)
YCTung 0:830ddddc129f 69 {
YCTung 0:830ddddc129f 70 ;
YCTung 0:830ddddc129f 71 }