Vanmorgen aan gewerkt

Dependencies:   FastPWM HIDScope MODSERIAL QEI biquadFilter mbed

Fork of Project_BioRobotics by Jordi Luong

main.cpp

Committer:
jordiluong
Date:
2017-10-23
Revision:
6:246b05228f52
Parent:
4:ea7689bf97e1
Child:
9:c19f6f0f5080

File content as of revision 6:246b05228f52:

#include "BiQuad.h"
#include "FastPWM.h"
#include "HIDScope.h"
#include "MODSERIAL.h"
#include "mbed.h"
#include "QEI.h"

// SERIAL COMMUNICATION WITH PC
MODSERIAL pc(USBTX, USBRX);

// STATES
enum states{MOTORS_OFF, MOVING, HITTING};

states currentState = MOTORS_OFF;                                               // Start with motors off
bool stateChanged = true;                                                       // Make sure the initialization of first state is executed

/*
// ENCODER
QEI Encoder(D12,D13,NC,32);                                                     // CONNECT ENC2A TO D13, ENC2B TO D12
float vorig_omwentelingen = 0;
float degrees;
float vorig_degrees = 0;
float omwentelingen;
float counts;
float snelheid_tpm;
float snelheid_degps;
float snelheid_tps;
*/

// PINS
DigitalOut motor1DirectionPin(D4);                                              // Value 0: CCW; 1: CW
PwmOut motor1MagnitudePin(D5);
DigitalOut motor2DirectionPin(D7);                                              // Value 0: CW or CCW?; 1: CW or CCW?
PwmOut motor2MagnitudePin(D6);
InterruptIn button1(D2);                                                        // CONNECT BUT1 TO D2
InterruptIn button2(D3);                                                        // CONNECT BUT2 TO D3
AnalogIn potPin(A1);

// DEFINITIONS
const float motorVelocity = 4.2;                                                  // unit: rad/s
const float motorGain = 8.4;                                                    // unit: (rad/s) / PWM
const int motorRatio = 131;                                                     // Ratio of the gearbox in the motors
int motor1State = 0;                                                            // 0: Off, 1: CW, 2: CCW
float motorValue = 0;

//TICKERS
Ticker encoder;
Ticker checkMotorState;

// FUNCTIONS
void RotateMotor1CW()
{
    motor1DirectionPin = 1;
    //motor1MagnitudePin = fabs(motorVelocity / motorGain);
    motorValue = potPin;
    motor1MagnitudePin = motorValue;
    //pc.printf("Rotating motor 1 CW \r\n");
    pc.printf("%f \r\n", motorValue);   
}

void RotateMotor1CCW()
{
    motor1DirectionPin = 0;
    //motor1MagnitudePin = fabs(motorVelocity / motorGain);
    //pc.printf("Rotating motor 1 CounterCW \r\n");
    motorValue = potPin;
    motor1MagnitudePin = motorValue;
    //pc.printf("Rotating motor 1 CW \r\n");
    pc.printf("%f \r\n", motorValue);   
}

void TurnMotor1Off()
{
    motor1MagnitudePin = 0;
    //pc.printf("Motors off \r\n");
}

void CheckMotor1()                                                               // Checks the state of motor1 and rotates motor1 depending on the state
{
    switch(motor1State)
    {
        case 0:                                                                 // Turn motors off
        {TurnMotor1Off(); break;}
        
        case 1:                                                                 // Rotate motor 1 CW
        {RotateMotor1CW(); break;}
        
        case 2:                                                                 // Rotate motor 2 CCW
        {RotateMotor1CCW(); break;}
        
        default:
        break;
    }
}
/*
void EncoderCalc()
{
    counts = Encoder.getPulses()/motorRatio;
    degrees = counts/32*360;
    omwentelingen = counts/32;
    snelheid_tpm = (omwentelingen-vorig_omwentelingen)/0.5*60;
    snelheid_tps = (omwentelingen-vorig_omwentelingen)/0.5;
    snelheid_degps = (degrees-vorig_degrees)/0.5;
    vorig_omwentelingen = omwentelingen;
    vorig_degrees = degrees;
    //pc.printf("%.1f pulsen, %.2f graden, %.2f omwentelingen, %.2f tpm, %.2f tps, %.2f deg/s\r\n",counts, degrees, omwentelingen, snelheid_tpm, snelheid_tps, snelheid_degps);
}
*/

// States function
void ProcessStateMachine()
{
    switch(currentState)
    {
        case MOTORS_OFF:
        {
            // State initialization
            if(stateChanged)
            {
                pc.printf("Entering MOTORS_OFF \r\n");
                TurnMotor1Off();                                                // Turn motors off
                stateChanged = false;
            }
            
            // Home command
            if(!button1)
            {
                currentState = MOVING;
                stateChanged = true;
                break;
            }
            break;
        }
        
        case MOVING:
        {
            // State initialization
            if(stateChanged)
            {
                pc.printf("Entering MOVING \r\n");
                stateChanged = false;
            }

            // Move commands
            
            if(!button1){motor1State = 1;}
            else if(!button2){motor1State = 2;}
            else{motor1State = 0;}
            
            /*
            // Hit command    
            if(// HIT COMMAND)
            {
                currentState = HITTING;
                stateChanged = true;
                break;
            }
            */
            break;
        }
        
        case HITTING:
        {
            // State initialization
            if(stateChanged)
            {
                pc.printf("Entering HITTING \r\n");
                //HitBall();                                                    // Hit the ball
                stateChanged = false;
                currentState = MOVING;
                stateChanged = true;
                break;
            }
            break;
        }
        
        default:
        {
            TurnMotor1Off();                                                    // Turn motors off for safety
            break;
        }
    }
}

// Main function
int main()
{
    // Serial communication
    pc.baud(115200);
    
    pc.printf("START \r\n");
    
    //encoder.attach(EncoderCalc, 0.5);
    checkMotorState.attach(CheckMotor1, 0.0002);
    
    while(true)
    {
        ProcessStateMachine();                                                  // Execute states function
    }
}