Stevie Wray / Mbed 2 deprecated SkyFawkes

Dependencies:   mbed

Revision:
0:d665d69947ab
Child:
1:08315c315df0
diff -r 000000000000 -r d665d69947ab main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Feb 23 14:16:20 2019 +0000
@@ -0,0 +1,107 @@
+#include "mbed.h"
+
+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[10] = {0,0,0,0,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_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]);
+        
+        //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 <= 9){ //check the ADC to be addressed exists
+                    rpi.reply(adc_values[v]);
+                }
+                v = rpi.read(); //last bit setup a blank reply
+                rpi.reply(0x00);
+            }
+        }
+    }
+}