Dependencies:   ros_lib_kinetic

LLComms.cpp

Committer:
WD40andTape
Date:
2018-12-17
Revision:
25:88e6cccde856
Parent:
24:bc852aa89e7a
Child:
26:7c59002c9cd7

File content as of revision 25:88e6cccde856:

// LLComms.cpp

#include "LLComms.h"

LLComms::LLComms() : 
    queue(32 * EVENTS_EVENT_SIZE), //32 //8 * EVENTS_EVENT_SIZE
    pinGate6(PE_11),
    spi_0(PC_12, PC_11, PC_10),
    spi_1(PF_9, PF_8, PF_7),
    pinCheck(PE_5),
    // These interrupt pins have to be declared AFTER SPI declaration. No Clue Why.
    pinGate0(PF_11),
    pinGate1(PG_14),
    pinGate2(PF_15),
    pinGate3(PF_12),
    pinGate4(PF_3),
    pinGate5(PF_13),
    //pinGate6(PE_11), // See above nonsense
    pinGate7(PE_13),
    pinReset(PD_2)
{ // Constructor

    spi_0.format(16,2);
    spi_0.frequency(LOW_LEVEL_SPI_FREQUENCY);
    spi_1.format(16,2);
    spi_1.frequency(LOW_LEVEL_SPI_FREQUENCY);

    PinName LLPins[8] = {PD_15, PE_10, PD_11, PD_14, PE_7, PD_12, PF_10, PD_13};
    //PinName LLPins[8] = {PD_15, PE_10, PD_14, PD_11, PE_7, PD_12, PF_10, PD_13};
    PinName ADCPins[8] = {PG_12, PG_9, PE_1, PG_0, PD_0, PD_1, PF_0, PF_1};
    for (short int i = 0; i < 8; i++) {
        isDataReady[i] = 0;
        cs_LL[i] = new DigitalOut(LLPins[i]);
        cs_ADC[i] = new DigitalOut(ADCPins[i]);
    }
    
    // Initialise relevant variables
    for(short int i = 0; i<N_CHANNELS; i++) {
        // All chip selects in off state
        *cs_LL[i] = 1;
        *cs_ADC[i] = 1;
    }
    pinReset = 1; // Initialise reset pin to not reset the controllers.
    wait(0.25);
    pinReset=0; // Reset controllers to be safe
    wait(0.25);
    pinReset = 1; // Ready to go
    
    // Set up rise interrupts MIGHT NOT NEED TO BE POINTERS
    pinGate0.rise(callback(this,&LLComms::rise0));
    pinGate1.rise(callback(this,&LLComms::rise1));
    pinGate2.rise(callback(this,&LLComms::rise2));
    pinGate3.rise(callback(this,&LLComms::rise3));
    pinGate4.rise(callback(this,&LLComms::rise4));
    pinGate5.rise(callback(this,&LLComms::rise5));
    pinGate6.rise(callback(this,&LLComms::rise6));
    pinGate7.rise(callback(this,&LLComms::rise7));
    // Set up fall interrupts MIGHT NOT NEED TO BE POINTERS
    pinGate0.fall(callback(this,&LLComms::fall0));
    pinGate1.fall(callback(this,&LLComms::fall1));
    pinGate2.fall(callback(this,&LLComms::fall2));
    pinGate3.fall(callback(this,&LLComms::fall3));
    pinGate4.fall(callback(this,&LLComms::fall4));
    pinGate5.fall(callback(this,&LLComms::fall5));
    pinGate6.fall(callback(this,&LLComms::fall6));
    pinGate7.fall(callback(this,&LLComms::fall7));
}

//LLComms::~LLComms(void) { } // Destructor

void LLComms::formatMessage(short int type, double dblValue, double dblMaxValue) {
    // Convert to a 9-bit number
    int intValue = (int) ((dblValue/dblMaxValue)*511);
    intValue = intValue & 0x1FF; // Ensure number is 9-bit
    // Initialize message with value
    unsigned int intMsg = intValue;
    // Calculate checksum (the decimal sum of the position data)
    int intCheckSum = 0, intTempVar = intValue;
    while( intTempVar>0 ) { //591
        intCheckSum += intTempVar%10;
        intTempVar = floor(intTempVar/10.0);
    }
    // Add checksum to message
    intMsg = intMsg<<5;
    intMsg = intMsg | intCheckSum;
    // Add type bit (0 == position, 1 == velocity)
    intMsg = intMsg<<1;
    // Calculate decimal parity check for the whole message
    unsigned int count = 0, b = 1;  
    for(short int i=0; i<32; i++) {
        if( intMsg & (b << i) ) count++;
    }
    // Add parity bit to message (0 == Odd, 1 == Even)
    bool boolParity = !(bool)(count%2);
    intMsg = intMsg<<1;
    intMsg = intMsg | (int)boolParity;
}

void LLComms::SendReceiveData(int channel) {
    mutChannel[channel].lock(); // Lock mutex for specific Channel
    
    // Construct messages
    unsigned int intPositionMsg = formatMessage(1,demandPosition[channel],MAX_ACTUATOR_LENGTH);
    unsigned int intVelocityMsg = formatMessage(0,demandSpeed[channel],MAX_SPEED_MMPS);
    
    // Get data from controller
    int intPosSPI_Rx[N_CHANNELS]; // 16bit position value received over SPI from the actuator 
    int intPresSPI_Rx[N_CHANNELS]; // 16bit pressure value received over SPI from the actuator 
    *cs_LL[channel] = 0; // Select relevant chip
    if( channel < 4 ) {
        intPosSPI_Rx[channel] = spi_0.write(intPositionMsg); // Transmit & receive
        intPresSPI_Rx[channel] = intPresSPI_Rx[channel] | spi_0.write(intVelocityMsg); // Transmit & receive
    } else {
        intPosSPI_Rx[channel] = spi_1.write(intPositionMsg); // Transmit & receive
        intPresSPI_Rx[channel] = intPresSPI_Rx[channel] | spi_1.write(intVelocityMsg); // Transmit & receive
    }
    *cs_LL[channel] = 1; // Deselect chip
    isDataReady[channel] = 0; // Data no longer ready, i.e. we now require new data
    
    /*if(channel == 0) {
        intGlobalTest = intPosSPI_Rx[channel];
        dblGlobalTest = ((double) (intPosSPI_Rx[channel])/8191.0*52.2);
    }*/
    // Sort out received data
    //STILL TO DO!!!!!!
    intPosSPI_Rx[channel] = intPosSPI_Rx[channel]>>7 & 0x1FF;
    positionSensor_m[channel] = (double)intPosSPI_Rx[channel]/511*(MAX_ACTUATOR_LENGTH/DBL_ACTUATOR_CONVERSION[channel])/1000;
    //pressureSensor_bar[channel] = ...
    
    mutChannel[channel].unlock();//unlock mutex for specific channel
}

// Common rise handler function 
void LLComms::common_rise_handler(int channel) {
    pinCheck = 1;
    if (isDataReady[channel]) { // Check if data is ready for tranmission
        ThreadID[channel] = queue.call(this,&LLComms::SendReceiveData,channel); // Schedule transmission
    }
}

// Common fall handler functions
void LLComms::common_fall_handler(int channel) {
    pinCheck = 0;
    queue.cancel(ThreadID[channel]); // Cancel relevant queued event
}

// Stub rise functions
void LLComms::rise0(void) { common_rise_handler(0); }
void LLComms::rise1(void) { common_rise_handler(1); }
void LLComms::rise2(void) { common_rise_handler(2); }
void LLComms::rise3(void) { common_rise_handler(3); }
void LLComms::rise4(void) { common_rise_handler(4); }
void LLComms::rise5(void) { common_rise_handler(5); }
void LLComms::rise6(void) { common_rise_handler(6); }
void LLComms::rise7(void) { common_rise_handler(7); }
// Stub fall functions
void LLComms::fall0(void) { common_fall_handler(0); }
void LLComms::fall1(void) { common_fall_handler(1); }
void LLComms::fall2(void) { common_fall_handler(2); }
void LLComms::fall3(void) { common_fall_handler(3); }
void LLComms::fall4(void) { common_fall_handler(4); }
void LLComms::fall5(void) { common_fall_handler(5); }
void LLComms::fall6(void) { common_fall_handler(6); }
void LLComms::fall7(void) { common_fall_handler(7); }