Sets the Kp, Ki and Kd values of the PID controller for the encoder motors

Dependencies:   FastPWM HIDScope_motor_ff MODSERIAL QEI mbed

Fork of Encoder by Biorobotics_group_2

main.cpp

Committer:
sjoerdbarts
Date:
2016-10-07
Revision:
7:e7aa4f10d1fb
Parent:
6:ed11342ab079
Child:
8:fe907b9a0bab

File content as of revision 7:e7aa4f10d1fb:

#include "mbed.h"
#include "HIDScope.h"
#include "QEI.h"
#define SERIAL_BAUD 115200  // baud rate for serial communication
 
Serial pc(USBTX,USBRX);

// Variables counter
int countsCW = 0;
int countsCCW = 0;
int net_counts = 0;

// Variables degrees, and speed
float degrees = 0.0;
volatile float curr_degrees = 0.0;
volatile float prev_degrees = 0.0;
volatile float speed = 0.0;          // speed in degrees/s
volatile const float T_CalculateSpeed = 0.1; // 100 Hz

// Set counts per revolution
const float counts_per_rev = 4200.0;

// Set encoder
QEI EncoderCW(D12,D13,NC,32);
QEI EncoderCCW(D13,D12,NC,32);

// Print the output
void PrintDegrees(){
    pc.printf("\r\n Nett Pulses %i \r\n", net_counts);
    pc.printf("\r\n Output degrees  %f \r\n", degrees);
    pc.printf("\r\n Speed %f \r\n",speed);
    }

// Calculate the speed
void CalculateSpeed() {
    curr_degrees = degrees;
    speed = (curr_degrees-prev_degrees)/T_CalculateSpeed; 
    prev_degrees = curr_degrees;
}

int main()
{
    pc.baud(SERIAL_BAUD);
    pc.printf("\r\n ***THERMONUCLEAR WARFARE COMMENCES*** \r\n");
    
    // Set ticker for serial communication of counts and degrees
    Ticker PrintDegreesTicker;
    PrintDegreesTicker.attach(&PrintDegrees,0.1);
    
    // Set ticker for speed calculation
    Ticker CalculateSpeedTicker;
    CalculateSpeedTicker.attach(CalculateSpeed,T_CalculateSpeed);
    
    // count the CW and CCW counts and calculate the output degrees
    while(true){
    countsCW = EncoderCW.getPulses();
    countsCCW= EncoderCCW.getPulses();
    net_counts=countsCW-countsCCW;
    degrees=(net_counts*360.0)/counts_per_rev;
    
    }
}