test with PID control to screen on your computer

Dependencies:   Motor_with_encoder MODSERIAL QEI mbed

Fork of Project_motor by Biorobotics Project

main.cpp

Committer:
vera1
Date:
2017-10-03
Revision:
0:bc949f735b8d
Child:
1:e0f6cdefcd6e

File content as of revision 0:bc949f735b8d:

#include "mbed.h"
#include "MODSERIAL.h"
#include "encoder.h"
// this program can turn the motor clockwise or counterclockwise depending on the value p or n typed into the terminal. with 's' you can stop the motor

MODSERIAL pc(USBTX,USBRX);
PwmOut motorspeed(D5);
DigitalOut motorposition(D4);
DigitalOut led1(D8);
DigitalOut led2(D7);
AnalogIn pot(A1);
DigitalIn button1(D9);
DigitalIn button2(D10);
Ticker potmeterreadout;
//Ticker encoderreadout;
//Encoder motor1(PTD0,PTC4); // motor 1 is from encoder, otherwise it would be a pain in the ass to change all that code to motorencoder


float PwmPeriod = 1.0/5000.0;
/*
void readoutencoder()
{
    pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed());
}
*/
volatile float potvalue = 0.0;
void readpot(){
    potvalue = pot.read();
    }
int main()
{
    
    //encoderreadout.attach(readoutencoder,0.2f);
    pc.baud(9600);
    potmeterreadout.attach(readpot,0.2f);
    motorspeed.period(0.0001f);
    //float motorspeed = 0.0f;
    while (true) {
        
            led1 = potvalue;
            motorspeed = potvalue;
            pc.printf("reference velocity = %.2f\r\n", potvalue);     
        

            if ((button2 == 1)&&(button1 == 0)) {

                motorposition = 0;         // motor turns anticlockwise
                led2 = 0;
                
            }
            if ((button2 ==0)&&(button1 ==1)){
            
                motorposition = 1;         // motor turns anticlockwise
                led2 = 1;
                }
        //}

    }
}