Biorobotics_group_2 / Mbed 2 deprecated Encoder

Dependencies:   HIDScope_motor_ff QEI mbed FastPWM MODSERIAL

Fork of HID_scope_test by Biorobotics_group_2

main.cpp

Committer:
sjoerdbarts
Date:
2016-10-07
Revision:
6:ed11342ab079
Parent:
5:36788b154e25
Child:
7:e7aa4f10d1fb

File content as of revision 6:ed11342ab079:

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

int countsCW = 0;
int countsCCW = 0;
int net_counts = 0;
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

const float counts_per_rev = 4200.0;
QEI EncoderCW(D12,D13,NC,32);
QEI EncoderCCW(D13,D12,NC,32);

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