Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:08315c315df0
- Parent:
- 0:d665d69947ab
- Child:
- 2:00535b62c344
--- a/main.cpp Sat Feb 23 14:16:20 2019 +0000 +++ b/main.cpp Sat Feb 23 16:45:39 2019 +0000 @@ -1,5 +1,12 @@ #include "mbed.h" +//Analog inputs A0-A5 addressable as such. A6 to A9 do not work. +AnalogIn ANALOG0(A0); +AnalogIn ANALOG1(A1); +AnalogIn ANALOG2(A2); +AnalogIn ANALOG3(A3); +AnalogIn ANALOG4(A4); +AnalogIn ANALOG5(A5); SPISlave rpi(PB_5, PB_4, PB_3, PA_15); //setup SPI pins to talk to the raspberry pi //PA_9 - MLB //PA_8 - MLF //PA_10 - MRF //PA_11 - MRB PwmOut MRB(PA_11); //back right motor @@ -18,13 +25,14 @@ float m_duty [4] = {0.0, 0.0, 0.0, 0.0}; //setup array for motor pwm duty int m_distance_ref [4] = {0,0,0,0}; //setup array for 4 motor distances int max_accel = 0; //for storing the maximum acceleration -int adc_values[10] = {0,0,0,0,0,0,0,0,0,0}; //setup array for ADC values +int adc_values[6] = {0,0,0,0,0,0}; //setup array for ADC values int main() { int i = 0; //temp counter variable int v = 0; //temp SPI variable + //setup driver pins LAIN1 = 1; @@ -52,15 +60,20 @@ for (i=0;i<=3;i++){ m_duty[i] = float(m_speed_ref[i])/100.0; } - /* MRB.write(m_speed_ref[0]/100); - MRF.write(m_speed_ref[1]/100); - MLB.write(m_speed_ref[2]/100); - MLF.write(m_speed_ref[3]/100); */ MRB.write(m_duty[0]); MRF.write(m_duty[1]); MLB.write(m_duty[2]); MLF.write(m_duty[3]); +//read all the ADC values when not doing something else + adc_values[0] = int(ANALOG0.read()*255); + adc_values[1] = int(ANALOG1.read()*255); + adc_values[2] = int(ANALOG2.read()*255); + adc_values[3] = int(ANALOG3.read()*255); + adc_values[4] = int(ANALOG4.read()*255); + adc_values[5] = int(ANALOG5.read()*255); + + //do SPI communication stuff if(rpi.receive()) { v = rpi.read(); // Read byte from master @@ -96,7 +109,7 @@ else if(v == char('V')){ rpi.reply(0x04); v = rpi.read(); - if(v <= 9){ //check the ADC to be addressed exists + if(v <= 6){ //check the ADC to be addressed exists rpi.reply(adc_values[v]); } v = rpi.read(); //last bit setup a blank reply