ECE 4333 / Mbed 2 deprecated Lab3

Dependencies:   mbed

Lab3.cpp

Committer:
A_Sterner
Date:
2016-02-19
Revision:
2:82e4eac97f0a
Parent:
0:b3cd4463d972
Child:
3:30244b9e5351
Child:
4:43aef502bb73

File content as of revision 2:82e4eac97f0a:

// EE4333 Robotics Lab 3

// Library Imports

//#include "InterruptIn.h"
//#include "rtos.h"
#include "mbed.h"
#include "Serial.h"
#include "stdio.h"

// Function Declarations

    void DE0_Init(int);
    void L_MotorInit(void);
    void R_MotorInit(void);
    signed int UserInput(void);
    void ControlThread(void);
    int SaturateAdd(int x, int y);
    float SaturateLimit(float x, float limit);
    signed int SignExtend(signed int x);

// ********************************************************************
//                     GLOBAL VARIABLE DECLARATIONS
// ********************************************************************

    signed int setpoint;                // Desired Angular Speed ( rad/sec )
    float e;                            // Velocity Error
    float u;                            // Control Signal
    int L_integrator;                   // Left Integrator State
    signed int dPositionLeft;           // DE0 Register 0
    int dTimeLeft;                      // DE0 Register 1
    
// *********************************************************************
//                     PROCESSES AND THREADS
// *********************************************************************


// *********************************************************************
//                          PIN DECLARATIONS
// *********************************************************************

    // Digital I/O Pins

    DigitalOut led1(LED1);              // Thread Indicators
    DigitalOut led2(LED2);              //
    DigitalOut led3(LED3);              //
    DigitalOut led4(LED4);              //
    
    DigitalOut DirL(p29);               // Direction of Left Motor
    DigitalOut DirR(p30);               // Direction of Right Motor

    // SPI Related Digital I/O Pins
    
    DigitalOut SpiReset(p11);
    DigitalOut IoReset(p12);

    //PWM

    PwmOut PwmL(p22);
    PwmOut PwmR(p21);

    //Serial

    Serial pc(USBTX, USBRX);            // tx and rx for PC serial channel via USB cable
    Serial Bluetooth(p9,p10);     // Pins tx(p9) , rx(p10) for bluetooth serial channel

    //SPI

    SPI DE0(p5,p6,p7); //Pin 5 is MOSI, Pin 6 MISO, Pin 7 SCLK
  
    //Interrupts
  
    Ticker ControlInterrupt;            // Internal Interrupt to trigger Control Thread


// ***************************************************
//                      DE0 Init
// ***************************************************

void DE0_Init(int SpiControlWord){
    
    int mode = 1;
    int bits = 16;
   
    DE0.format(bits,mode);
    
    // Verify Peripheral ID
    
    // Generates single square pulse to reset DE0 IO
    
    IoReset = 0;
    IoReset = 1;
    IoReset = 0;
    
    // Generates single square pulse to reset DE0 SPI
    
    SpiReset = 0;
    SpiReset = 1;
    SpiReset = 0;
    
    // Writes to DE0 Control Register
    
    int ID = DE0.write(SpiControlWord);             // SPI Control Word specifies SPI settings
    
    if(ID == 23){                                   // DE0 ID 23 (0x0017)
        printf("\n\r >> DE0 Initialized.\n\r");}
    else{
        printf("\n\r >> Failed to initialize DE0 board.\n\r");}
}

// ***************************************************
//              Left Motor Initialization
// ***************************************************

    // Pwm Pin Left Motor : p21
    // Direction Pin Left Motor : p29

void L_MotorInit(void){

   DirL = 1;                                   // Defaults to 0.

    // Direction bit logic output
    // 0 : Backwards ( Reverse ) 
    // 1 : Forwards  ( Advance )

    PwmL.period_us(100);
    PwmL.write(0.7);
    
}
    
// ***************************************************
//             Right Motor Initialization
// ***************************************************

    // Pwm Pin Right Motor : p22
    // Direction Pin Right Motor : p30
    
void R_MotorInit(void){
    
   DirR = 1;                                   // Defaults to 0.
    
    // Direction bit logic output
    // 0 : Forwards  ( Advance ) 
    // 1 : Backwards ( Reverse )
    
    PwmR.period_us(100);
    PwmR.write(0);
    
}
    
/// ***************************************************
//                 User Input
// ***************************************************

signed int UserInput(void){
   
   signed int input;
   
   printf("\n\r Please enter a desired angular speed (rad/sec) >> ");
   scanf("%i",&input);
   printf("\n\r Your number was >> %i\n\r",input);
   
   return input;

}


/// ***************************************************
//                 Control Thread
// ***************************************************

void ControlThread(void){

    // Read Incremental Position from DE0 QEI

    int dummy = 0x0000;                                // Pushes dummy information which DE0 ignores, store return from QEI register
    
    dPositionLeft = SignExtend(DE0.write(dummy));
    dTimeLeft = DE0.write(dummy);
    
    // Computer Angular Speed and Angular Speed Error
    
    signed int AngularSpeedLeft = (123*dPositionLeft)/dTimeLeft;
    
    e = setpoint - AngularSpeedLeft;
    
    float Kp = 2.5;
    float Ki = 0.010;
    
    if(SaturateLimit((Kp*e+Ki*L_integrator)/35,1)<1){
        L_integrator = L_integrator +e;}
    else{
        L_integrator = L_integrator;
    }
    
    
    u = SaturateLimit( (Kp*e+Ki*L_integrator),1);

    PwmL.write(u);
}

/// ***************************************************
//                SaturateAdd
// ***************************************************

signed int SaturateAdd(signed int x, signed int y){
    
    signed int z = x + y;
    
    if( (x>0) && (y>0)&& (z<=0) ){
        z = 0x7FFFFFFF;}
    
    else if( (x<0)&&(y<0)&&(z>=0) ){
        z = 0x80000000;}
        
    return z;
}
    
/// ***************************************************
//              SaturateLimit
// ***************************************************

float SaturateLimit(float x, float limit){
    
    if (x > limit){
        return limit;}
        
    else if(x < -limit){
        return(-limit);}
        
    else{
        return x;}
        
}

/// ***************************************************
//                   Sign Extend
// ***************************************************

signed int SignExtend(int x){
        
        if(x&0x00008000){
            x = x|0xFFFF0000;
        }
        
        return x;
}



// ==============================================================================================================
// ==============================================================================================================


// *********************************************************************
//                        MAIN FUNCTION
// *********************************************************************

int main(){
   
   // Initialization

   DE0_Init(0x8002);                                                            
   L_MotorInit();
   L_integrator = 0;
   ControlInterrupt.attach(&ControlThread, 0.0005);
   
   
   // Specify Setpoint ( rads/sec )

    setpoint = UserInput();
   
   // Display Global Variables to Console
   
   while(1){
       
       float error_t = e;
       float u_t = u;
       
       printf("\n\r US : %i",setpoint);      // User defined setpoint
       printf("\n\r VE : %2.2f",error_t);             // Displays signed Velocity Error
       printf("\n\r IS : %i",L_integrator);  // Displays currently state of the left integrator
       printf("\n\r CS : %2.4f",u_t);             // Displays control signal
       printf("\n\r\n\r");
       wait(0.75);
   }
   
}