PID en biquad filter implemented and outcommented

Dependencies:   Motor_with_encoder MODSERIAL mbed QEI

Fork of Tutorial_lecture5_pin_pot_motor by Biorobotics Project

Committer:
vera1
Date:
Fri Oct 06 08:26:55 2017 +0000
Revision:
2:80177e90c1e6
Parent:
1:e0f6cdefcd6e
Child:
3:4520d0ca4e56
motor velocity proportional to potmeter working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vera1 0:bc949f735b8d 1 #include "mbed.h"
vera1 0:bc949f735b8d 2 #include "MODSERIAL.h"
vera1 0:bc949f735b8d 3 #include "encoder.h"
vera1 0:bc949f735b8d 4 // 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
vera1 0:bc949f735b8d 5
vera1 0:bc949f735b8d 6 MODSERIAL pc(USBTX,USBRX);
vera1 0:bc949f735b8d 7 PwmOut motorspeed(D5);
vera1 0:bc949f735b8d 8 DigitalOut motorposition(D4);
vera1 0:bc949f735b8d 9 DigitalOut led1(D8);
vera1 1:e0f6cdefcd6e 10 DigitalOut led2(D11);
vera1 0:bc949f735b8d 11 AnalogIn pot(A1);
vera1 2:80177e90c1e6 12 DigitalIn button1(D13);
vera1 2:80177e90c1e6 13 DigitalIn button2(D12);
vera1 0:bc949f735b8d 14 Ticker potmeterreadout;
vera1 1:e0f6cdefcd6e 15 Ticker encoderreadout;
vera1 1:e0f6cdefcd6e 16 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
vera1 0:bc949f735b8d 17
vera1 0:bc949f735b8d 18
vera1 0:bc949f735b8d 19 float PwmPeriod = 1.0/5000.0;
vera1 0:bc949f735b8d 20 /*
vera1 0:bc949f735b8d 21 void readoutencoder()
vera1 0:bc949f735b8d 22 {
vera1 0:bc949f735b8d 23 pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed());
vera1 0:bc949f735b8d 24 }
vera1 0:bc949f735b8d 25 */
vera1 0:bc949f735b8d 26 volatile float potvalue = 0.0;
vera1 1:e0f6cdefcd6e 27 volatile float position = 0.0;
vera1 0:bc949f735b8d 28 void readpot(){
vera1 0:bc949f735b8d 29 potvalue = pot.read();
vera1 1:e0f6cdefcd6e 30 //position = motor1.getPosition();
vera1 2:80177e90c1e6 31 pc.printf("pos: %d, speed %f reference velocity = %.2f\r\n",motor1.getPosition(), motor1.getSpeed(), potvalue);
vera1 2:80177e90c1e6 32 led1 = potvalue;
vera1 2:80177e90c1e6 33 motorspeed = potvalue;
vera1 0:bc949f735b8d 34 }
vera1 2:80177e90c1e6 35 void writevalues(){
vera1 2:80177e90c1e6 36
vera1 2:80177e90c1e6 37 }
vera1 0:bc949f735b8d 38 int main()
vera1 1:e0f6cdefcd6e 39 {
vera1 0:bc949f735b8d 40
vera1 0:bc949f735b8d 41 pc.baud(9600);
vera1 0:bc949f735b8d 42 potmeterreadout.attach(readpot,0.2f);
vera1 0:bc949f735b8d 43 motorspeed.period(0.0001f);
vera1 0:bc949f735b8d 44 //float motorspeed = 0.0f;
vera1 0:bc949f735b8d 45 while (true) {
vera1 0:bc949f735b8d 46
vera1 2:80177e90c1e6 47
vera1 1:e0f6cdefcd6e 48 //pc.printf("reference velocity = %.2f\r\n", potvalue);
vera1 0:bc949f735b8d 49
vera1 0:bc949f735b8d 50
vera1 0:bc949f735b8d 51 if ((button2 == 1)&&(button1 == 0)) {
vera1 0:bc949f735b8d 52
vera1 0:bc949f735b8d 53 motorposition = 0; // motor turns anticlockwise
vera1 0:bc949f735b8d 54 led2 = 0;
vera1 0:bc949f735b8d 55
vera1 0:bc949f735b8d 56 }
vera1 0:bc949f735b8d 57 if ((button2 ==0)&&(button1 ==1)){
vera1 0:bc949f735b8d 58
vera1 0:bc949f735b8d 59 motorposition = 1; // motor turns anticlockwise
vera1 0:bc949f735b8d 60 led2 = 1;
vera1 0:bc949f735b8d 61 }
vera1 0:bc949f735b8d 62 //}
vera1 0:bc949f735b8d 63
vera1 0:bc949f735b8d 64 }
vera1 0:bc949f735b8d 65 }
vera1 0:bc949f735b8d 66