first

Dependencies:   BEAR_Reciever Motor eeprom iSerial mbed

Fork of BEAR_Motion by BE@R lab

main.cpp

Committer:
ParinyaT
Date:
2016-01-14
Revision:
1:84167ca00307
Parent:
0:451c27e4d55e
Child:
6:98871feebea0

File content as of revision 1:84167ca00307:

//*****************************************************/
//  Include  // 
#include "mbed.h"
#include "pinconfig.h"
#include "PID.h"
#include "Motor.h"
#include "eeprom.h"
#include "Reciever.h" 

//*****************************************************/
//--PID parameter--
//-Upper-//
int U_Kc;
int U_Ti;
int U_Td;
//-lower-//
int L_Kc;
int L_Ti;
int L_Td;

//*****************************************************/
// Global  //
//-- Communication --
Serial PC(D1,D0);
ANDANTE_PROTOCOL_PACKET command;
//-- encoder --  
int Upper_Position;
int Lower_Position;
int data;
SPI ENC(Emosi, Emiso, Esck);
DigitalOut EncA(EncoderA);
DigitalOut EncB(EncoderB);
//-- Motor --
int dir;
Motor Upper(PWM_LU,A_LU,B_LU);
Motor Lower(PWM_LL,A_LL,B_LL);
//-- PID --
int Upper_SetPoint;
int Lower_SetPoint;
PID Up_PID(U_Kc, U_Ti, U_Td, 0.001);//Kp,Ki,Kd,Rate
PID Low_PID(L_Kc, L_Ti, L_Td, 0.001);
//*****************************************************/
void Read_Encoder(PinName Encoder)
{
    Enc.format(8,0);
    Enc.frequency(200000);//due to rising time,have to decrease clock from 1M - 240k
    
    Encoder = 0;
    Enc.write(0x41);
    Enc.write(0x09);
    data = Enc.write(0x00);
    Encoder = 1;
    
}
//*****************************************************/
int Get_EnValue(int Val)
{
    int i = 0;
    static unsigned char codes[] = {
    127, 63, 62, 58, 56, 184, 152, 24, 8, 72, 73, 77, 79, 15, 47, 175,
    191, 159, 31, 29, 28, 92, 76, 12, 4, 36, 164, 166, 167, 135, 151, 215,
    223, 207, 143, 142, 14, 46, 38, 6, 2, 18, 82, 83, 211, 195, 203, 235, 
    239, 231, 199, 71, 7, 23, 19, 3, 1, 9, 41, 169, 233, 225, 229, 245, 
    247, 243, 227, 163, 131, 139, 137, 129, 128, 132, 148, 212, 244, 240, 242, 250,
    251, 249, 241, 209, 193, 197, 196, 192, 64, 66, 74, 106, 122, 120, 121, 125, 
    253, 252, 248, 232, 224, 226, 98, 96, 32, 33, 37, 53, 61, 60, 188, 190, 
    254, 126, 124, 116, 112, 113, 49, 48, 16, 144, 146, 154, 158, 30, 94, 95 };
    
    while(Val != codes[i])
    {
        i++;
    }
    
    return i;
    
}
//*****************************************************/
void SET_UpperPID()
{
    Upper.period(0.001);
    Up_PID.setMode(0);
    Up_PID.setInputLimits(0,127);
    Up_PID.setOutputLimits(0,1);
}
//******************************************************/
void SET_LowerPID()
{
    Lower.period(0.001);
    Low_PID.setMode(0);
    Low_PID.setInputLimits(0,127);
    Low_PID.setOutputLimits(0,1);
}
//******************************************************/   
void Move_Upper()
{
    Read_Encoder(EncoderA);
    Upper_Position = Get_EnValue(data);

    Up_PID.setProcessValue(Upper_Position);
            
    if(Upper_Position - Uppper_SetPoint > 0 ){ 
        dir = 1;
        }
    if(Upper_Position - Uppper_SetPoint < 0 ){ 
        dir = -1;
        }
    Upper.speed(LU_PID.compute() * dir);
}
//******************************************************/
void Move_Lower()
{
    Read_Encoder(EncoderB);
    Lower_Position = Get_EnValue(data);

    Low_PID.setProcessValue(Lower_Position);
            
    if(Lower_Position - Lower_SetPoint > 0 ){ 
        dir = 1;
        }
    if(Lower_Position - Lower_SetPoint < 0 ){ 
        dir = -1;
        }
    Lower.speed(LU_PID.compute() * dir);
}   
//******************************************************/


int main()
{
    SET_UpperPID();
    SET_LowerPID();
    
    while(1) 
    {
        Up_PID.setSetPoint(Upper_SetPoint);
        Low_PID.setSetPoint(Lower_SetPoint);

        Move_Upper();
        Move_Lower();
    }

}