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.
main.cpp
- Committer:
- Stevie
- Date:
- 2019-02-23
- Revision:
- 1:08315c315df0
- Parent:
- 0:d665d69947ab
- Child:
- 2:00535b62c344
File content as of revision 1:08315c315df0:
#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 PwmOut MRF(PA_10); //front right motor PwmOut MLB(PA_9); //back left motor PwmOut MLF(PA_8); //front left motor DigitalOut RAIN1(PC_6); //PC_9 - Left //PC_6 - Right DigitalOut RAIN2(PB_9); // PC_8 - Left //PB_9 - Right DigitalOut RSTANDBY(PC_5); //PB_8 - Left //PC_5 - Right DigitalOut LAIN1(PC_9); DigitalOut LAIN2(PC_8); DigitalOut LSTANDBY(PB_8); //InterruptIn ENCODER1(PC_7); //PC_12 - MLB //PC_11 - MLF //PB_7 - MRF //PB_6 - MRB //DigitalIn ENCODER2(PB_6); //PD_2 - MLB //PC_10 - MLF //PA_13 - MRF //PC_7 - MRB int m_speed_ref [4] = {0,0,0,0}; //setup array for 4 motor speeds 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[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; LAIN2 = 0; LSTANDBY = 1; RAIN1 = 1; RAIN2 = 0; RSTANDBY = 1; // Set PWM MRB.period_us(100); MRB.write(0.0); MRF.period_us(100); MRF.write(0.0); MLB.period_us(100); MLB.write(0.0); MLF.period_us(100); MLF.write(0.0); //setup Spi // rpi.reply(0x00); //set default reply while (1) { //write motor speed for (i=0;i<=3;i++){ m_duty[i] = float(m_speed_ref[i])/100.0; } 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 if(v == char('S')){ rpi.reply(0x01); for (i=0;i<=3;i++){ m_speed_ref[i] = rpi.read() - 128; rpi.reply(m_speed_ref[i]); } v = rpi.read(); //last bit setup a blank reply rpi.reply(0x00); } else if(v == char('D')){ rpi.reply(0x02); for (i=0;i<=3;i++){ m_speed_ref[i] = rpi.read() - 128; rpi.reply(m_speed_ref[i]); } for (i=0;i<=3;i++){ m_distance_ref[i] = rpi.read() - 128; rpi.reply(m_distance_ref[i]); } v = rpi.read(); //last bit setup a blank reply rpi.reply(0x00); } else if(v == char('A')){ rpi.reply(0x03); max_accel = rpi.read(); rpi.reply(max_accel); v = rpi.read(); //last bit setup a blank reply rpi.reply(0x00); } else if(v == char('V')){ rpi.reply(0x04); v = rpi.read(); if(v <= 6){ //check the ADC to be addressed exists rpi.reply(adc_values[v]); } v = rpi.read(); //last bit setup a blank reply rpi.reply(0x00); } } } }