PID controll for the robot motors.

Dependencies:   BioroboticsMotorControl MODSERIAL mbed

main.cpp

Committer:
brass_phoenix
Date:
2018-10-19
Revision:
5:fd4eaf3cb09d
Parent:
4:fce8c7458e97
Child:
6:cc3e47c7aac2

File content as of revision 5:fd4eaf3cb09d:

#include "mbed.h"
#include "MODSERIAL.h"

#include "pid.h"
#include "motor.h"

const float pid_period = 0.001; // PID sample period in seconds.

const double Kp = 10.0;
const double Ki = 0.1;
const double Kd = 0.5;

AnalogIn potmeter1(A1); // Analoge input van potmeter 1 -> Motor 1
AnalogIn potmeter2(A2); // Analoge input van potmeter 2 -> Motor 2
Serial pc(USBTX, USBRX);

Motor motor(D6, D7, D13, D12, &pc);


// Normalizes a potmeter value from it's original range of [0, 1] to [-1, 1]
double normalize_pot(double pot_value) {
    // scales value potmeter from 0-1 to -1 - 1.
    return pot_value * 2 - 1;
};


int main()
{
    pc.baud(115200);
    pc.printf("Starting.");
    motor.set_pid_k_values(Kp, Ki, Kd);
    // Start the motor controller at the desired frequency.
    motor.start(pid_period);
    
    while(true){
        // reads out value potmeter 1 between 0-1
        double pot = potmeter2.read();
        double desired_angle = normalize_pot(pot) * PI; // Scale the potmeter to [-PI, PI]
        // Update the motor controller with the new angle.
        motor.set_target_angle(desired_angle);
        wait(0.1);
    } //Lege while loop zodat functie niet afloopt
}