HeRoS: read out and log joint angles and force sensor data from the leg test bench. Now with additional features to read pressure sensors and set the null values of the pressure and force sensors

Dependencies:   SPTE_10Bar_5V mbed AS5048 SDFileSystem MODSERIAL PinDetect Valve LCM101

Fork of heros_leg_readout_torque_addition by K K

Revision:
10:77fcbad99a31
Parent:
5:63063a9fa51c
Child:
11:b0a6faaa5e9f
--- a/main.cpp	Mon Dec 09 13:10:24 2019 +0000
+++ b/main.cpp	Mon Feb 17 15:15:24 2020 +0000
@@ -9,9 +9,22 @@
 #include "mbed.h"
 #include "bench.h"
 
+ 
 //Example experiment method 
 void runDemoExperiment0(int cycles, float targetkPa);
 
+//Methods for testing DAC chip - leave for now
+void selectDACB();
+void deselectDACB();
+
+void testDAC();
+void testToggleChannel();
+
+DigitalOut myled(LED1);
+DigitalOut CSA(DAC_CSA);
+DigitalOut CSB(DAC_CSB);
+SPI spi(DAC_MOSI,DAC_MISO,DAC_SCLK);
+
 // Create bench object - this is used to control the test rig
 Bench leg;
 
@@ -35,7 +48,8 @@
     
     /* Setup all peripherals on rig, display info about SD card and the 
     user interface menu */ 
-    leg.initialise(); 
+    leg.initialise(); //this gives the 10MHz, normal clock polarity, mode 1 spi we need, pins are same as for encoder
+    
 
       
     /*Run an experiment when the button is pressed to start datalogging and 
@@ -83,7 +97,7 @@
                 
                 if (experimentRunning) {
                     measureP = leg.getPressure0()*100;//Conversion of bar to kPa
-                    wait(0.2);//Wait a bit
+                    wait(0.05);//Wait a bit
                 } else { //Logging stopped
                     leg.stopLogging(); //Stop logging data
                     leg.setValve(false); //Depressurise
@@ -103,7 +117,7 @@
                 
                 if (experimentRunning) {
                     measureP = leg.getPressure0()*100;//Conversion of bar to kpa
-                    wait(0.2);//Wait a bit
+                    wait(0.05);//Wait a bit
                 } else { //Logging stopped
                     leg.stopLogging(); //Stop logging data
                     leg.resumePrint(); //Let the Bench class print
@@ -117,4 +131,176 @@
     }
 }
 
+void testDAC() {
+    //setup SPI to write 8 bit words, mode 1 and turn select lines high
+    spi.format(8,1);
+    spi.frequency(200000);
+    deselectDACB();
+    wait_ms(20);
+    
+    //Power up DAC
+    selectDACB();
+    spi.write(0xE0);
+    spi.write(0x00);
+    deselectDACB();
+    
+    //Write a value to a channel
+    selectDACB();
+    spi.write(0x47);
+    spi.write(0xFF);
+    deselectDACB();
+    
 
+    
+    
+  /*  selectDACB();
+    spi.write(0x2F);
+    spi.write(0xFF);
+    deselectDACB();
+    
+    selectDACB();
+    spi.write(0x60);
+    spi.write(0x00);
+    deselectDACB();*/
+    
+    //wait(3);
+    
+    //Write a value to a channel
+  /*  selectDACB();
+    spi.write(0x40);
+    spi.write(0x00);
+    deselectDACB();*/
+    
+    while (true) {
+        myled = !myled;
+
+
+        wait_ms(500);
+    }
+}    
+
+/** Selects DAC B (enable line goes low)
+ */
+void selectDACB()
+{
+    CSB.write(0);
+    wait_us(1);
+}
+
+/** Deselects DAC B (enable line goes high)
+ */
+void deselectDACB()
+{
+    CSB.write(1);
+    wait_us(1);
+}
+    
+void testToggleChannel()
+{
+        // POWER up dac
+    //select chip
+    CSB.write(0);
+    wait_us(1);
+
+    spi.write(0b11100000);
+    spi.write(0b00000000);
+
+    //deselect chip
+    CSB.write(1);
+    wait_us(1);
+
+
+    //write output on a
+    //select chip
+    CSB.write(0);
+    wait_us(1);
+
+    spi.write(0b01000011);
+    spi.write(0b11111111);
+
+    //deselect chip
+    CSB.write(1);
+    wait_us(1);
+    
+   
+    //write output on b
+    //select chip
+    CSB.write(0);
+    wait_us(1);
+
+    spi.write(0b01011011);
+    spi.write(0b11111111);
+
+    //deselect chip
+    CSB.write(1);
+    wait_us(1);
+    
+    while (true) {
+        //leg.pc.printf("Hi");
+        // POWER up dac
+        //select chip
+        CSB.write(0);
+        wait_us(1);
+
+        spi.write(0b11100000);
+        spi.write(0b00000000);
+
+        //deselect chip
+        CSB.write(1);
+        wait_us(1);
+        //write output on a
+        //select chip
+        CSB.write(0);
+        wait_us(1);
+
+        spi.write(0b01010011);
+        spi.write(0b11111111);
+
+        spi.write(0b01001011);
+        spi.write(0b11111111);
+
+        //deselect chip
+        CSB.write(1);
+        wait_us(1);
+
+        wait_ms(100);
+
+
+    }
+    bool ch = true;
+    while (true) {
+        //data value
+        unsigned int data = 0xFFF;
+
+        //if more than 12 bits (0xfff) then set all bits true)
+        if (data > 0xFFF) {
+            data = 0xFFF;
+        }
+
+        //select chip
+        //bring cs low
+        CSB.write(0);
+        //wait a bit (more than 40ns)
+        wait_us(1);
+
+        //transfer a command (for channel a 0x4<<12 + data masked to 12 bits, for channel b 0x5<<12 + data masked to 12 bits)
+
+        int command = (0x01<<12);//default to channel a
+        /*if (!ch) {
+            command = (0x05<<12);
+            } */
+        data = command + (data&0xFFF);
+        //spi.write(data);
+        spi.write(data>>8);
+        spi.write(data & 0x00FF);
+        //bring cs high
+        CSB.write(1);
+        //wait a bit (more than 10-15ns)
+        wait_us(1);
+        wait_ms(10);
+        //leg.pc.printf("\r\nCommand: \t%i",command);
+
+        //leg.pc.printf("\r\nData: \t%i",data);
+        ch = !ch;
+    }
+}