Version 3 is with update to the test rig with a linear actuator

Dependencies:   SPTE_10Bar_5V mbed AS5048 SDFileSystem MODSERIAL PinDetect LCM101 LinearActuator

main.cpp

Committer:
cnckiwi31
Date:
2020-02-17
Revision:
10:77fcbad99a31
Parent:
5:63063a9fa51c
Child:
11:fc82dd22a527
Child:
12:b0a6faaa5e9f

File content as of revision 10:77fcbad99a31:

/**
* Author: Allan Veale
* Date: 27/11/19
* Purpose: Datalog from the active wearable test rig fitted with the first
* realistic (foam tissue) leg
*/

//Both the general mbed header and the test rig bench header are needed 
#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;

/**
 * Main loop
 */
int main()
{   
    leg.setLoggingFrequency(100); //Set datalogging frequency
    
    /* Two extra columns of data will be recorded in this experiment.
    One for the target pressure, and the other for the number of sit and 
    stand cycles currently completed in the experiment */ 
    string colNames[] = {"Target pressure (kPa)","Cycle"}; //add data headings
    leg.setExtraColumns(colNames,2);
    
    float targetP = 200; //Target pressure in kPa
    int expCycles = 2; //Number of sit to stand to sit cycles 
    float vals[] = {targetP,0}; //set initial values of data that will be logged
    leg.setExtraData(vals);
    
    /* Setup all peripherals on rig, display info about SD card and the 
    user interface menu */ 
    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 
    stop it if the button is pressed again to stop datalogging 
    (or when experiment stops - then datalogging stops by itself) */
    while (true) {
        if (leg.isLogging()) {
            runDemoExperiment0(expCycles,targetP);            
        }
    }
}

/**
 * Shows how a demo experiment works. This experiment pressurises the leg to 
 * pressure targetkPa, depressurises it, and then repeats the process cycles 
 * number of times
 * @param cycles: the number of cycles the leg goes up and down
 * @param targetkPa: the pressure at which the valve is opened to let the leg go down
 */
void runDemoExperiment0(int cycles, float targetkPa) 
{
    //The experiment starts when logging does
    bool experimentRunning = leg.isLogging(); 
    
    //Stop the Bench class from printing, so this method can print
    leg.pausePrint();
    
    if (experimentRunning) {
        // Pressurise and depressurise the leg cycles number of times
        for (int i=0; i<cycles; i++) {
            leg.pc.printf("\r\nCycle: \t%i out of \t%i",i+1,cycles);
            
            //Update cycles logged
            float data[] = {targetkPa,i+1};
            leg.setExtraData(data);

            //Pressurise
            leg.setValve(true);

            //Wait until measured pressure reaches target pressure
            float measureP = 0;
            while(measureP < targetkPa) {
                //Keep checking logging is going
                experimentRunning = leg.isLogging(); 
                
                if (experimentRunning) {
                    measureP = leg.getPressure0()*100;//Conversion of bar to kPa
                    wait(0.05);//Wait a bit
                } else { //Logging stopped
                    leg.stopLogging(); //Stop logging data
                    leg.setValve(false); //Depressurise
                    leg.resumePrint(); //Let the Bench class print
                    return;
                }
            }

            //Depressurise
            leg.setValve(false);

            /*Wait until depressurised (completely depressurised is 
            around 10-12 kPa due to current sensor calibration)*/
            while(measureP > 15) {
                //Keep checking logging is going
                experimentRunning = leg.isLogging();
                
                if (experimentRunning) {
                    measureP = leg.getPressure0()*100;//Conversion of bar to kpa
                    wait(0.05);//Wait a bit
                } else { //Logging stopped
                    leg.stopLogging(); //Stop logging data
                    leg.resumePrint(); //Let the Bench class print
                    return;
                }
            }
        }
        // Logging stopped as experiment is fully completed
        leg.stopLogging(); //Stop logging data
        leg.resumePrint(); //Let the Bench class print
    }
}

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;
    }
}