Script voor aansturen motoren

Dependencies:   Encoder HIDScope MODSERIAL mbed-dsp mbed

Committer:
jessekaiser
Date:
Wed Oct 22 14:08:43 2014 +0000
Revision:
5:e4b9cc904928
Parent:
4:db3dad7e53e3
Child:
6:f260176f704b
motor hidscope;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jessekaiser 0:d5d3b731340c 1 #include "mbed.h"
jessekaiser 0:d5d3b731340c 2 #include "encoder.h"
jessekaiser 0:d5d3b731340c 3 #include "HIDScope.h"
jessekaiser 0:d5d3b731340c 4
jessekaiser 0:d5d3b731340c 5 #define TSAMP 0.01
jessekaiser 0:d5d3b731340c 6 #define K_P (0.1)
jessekaiser 0:d5d3b731340c 7 #define K_I (0.03 *TSAMP)
jessekaiser 0:d5d3b731340c 8 #define K_D (0.001 /TSAMP)
jessekaiser 0:d5d3b731340c 9 #define I_LIMIT 1.
jessekaiser 0:d5d3b731340c 10
jessekaiser 0:d5d3b731340c 11 #define M1_PWM PTC8 //blauw
jessekaiser 0:d5d3b731340c 12 #define M1_DIR PTC9 //groen
jessekaiser 2:11076f69e0a7 13 #define M2_PWM PTA5 //blauw
jessekaiser 2:11076f69e0a7 14 #define M2_DIR PTA4 //groen
jessekaiser 0:d5d3b731340c 15
jessekaiser 3:dcc4cebba0d7 16 Serial pc(USBTX, USBRX);
jessekaiser 4:db3dad7e53e3 17 DigitalOut led1(LED_RED);
jessekaiser 3:dcc4cebba0d7 18
jessekaiser 3:dcc4cebba0d7 19
jessekaiser 0:d5d3b731340c 20 void clamp(float * in, float min, float max);
jessekaiser 0:d5d3b731340c 21 float pid(float setpoint, float measurement);
jessekaiser 0:d5d3b731340c 22 volatile bool looptimerflag;
jessekaiser 5:e4b9cc904928 23 HIDScope scope(2);
jessekaiser 0:d5d3b731340c 24
jessekaiser 3:dcc4cebba0d7 25
jessekaiser 0:d5d3b731340c 26 void setlooptimerflag(void)
jessekaiser 0:d5d3b731340c 27 {
jessekaiser 0:d5d3b731340c 28 looptimerflag = true;
jessekaiser 0:d5d3b731340c 29 }
jessekaiser 0:d5d3b731340c 30
jessekaiser 3:dcc4cebba0d7 31
jessekaiser 3:dcc4cebba0d7 32
jessekaiser 0:d5d3b731340c 33 int main()
jessekaiser 3:dcc4cebba0d7 34 {
jessekaiser 3:dcc4cebba0d7 35 //Let op dat de jumpers goed staan als het motortje niet wilt draaien. De E1 jumper moet onder de nummer 7 pin. De locatie van de M1 pin
jessekaiser 3:dcc4cebba0d7 36 //bepaalt of de motor CW of CCW draait.
jessekaiser 0:d5d3b731340c 37 //start Encoder-> first pin should be PTDx or PTAx, second pin doesn't matter
jessekaiser 3:dcc4cebba0d7 38
jessekaiser 3:dcc4cebba0d7 39 //motor 25D
jessekaiser 0:d5d3b731340c 40 Encoder motor1(PTD3,PTD5); //wit, geel
jessekaiser 3:dcc4cebba0d7 41 PwmOut pwm_motor1(M2_PWM); // PTC8, blauw, /*PwmOut to motor driver*/
jessekaiser 2:11076f69e0a7 42 pwm_motor1.period_us(75); //10kHz PWM frequency
jessekaiser 3:dcc4cebba0d7 43 DigitalOut motordir1(M2_DIR); //PTC9, groen
jessekaiser 3:dcc4cebba0d7 44
jessekaiser 3:dcc4cebba0d7 45 //motor2 37D
jessekaiser 2:11076f69e0a7 46 Encoder motor2(PTD2, PTD0); //wit, geel
jessekaiser 3:dcc4cebba0d7 47 PwmOut pwm_motor2(M1_PWM); //PTA5, blauw
jessekaiser 2:11076f69e0a7 48 pwm_motor2.period_us(75);
jessekaiser 3:dcc4cebba0d7 49 DigitalOut motordir2(M1_DIR); //PTA4, groen
jessekaiser 3:dcc4cebba0d7 50
jessekaiser 4:db3dad7e53e3 51 char c ='0';
jessekaiser 0:d5d3b731340c 52 while(1) {
jessekaiser 4:db3dad7e53e3 53 pwm_motor1.write(1);
jessekaiser 4:db3dad7e53e3 54 motordir1.write(1);
jessekaiser 5:e4b9cc904928 55
jessekaiser 5:e4b9cc904928 56 wait(0.01);
jessekaiser 5:e4b9cc904928 57 scope.set(0, motor1.getposition());
jessekaiser 5:e4b9cc904928 58 scope.set(1, motor2.getPosition());
jessekaiser 5:e4b9cc904928 59 scope.send();
jessekaiser 5:e4b9cc904928 60 /*do {
jessekaiser 4:db3dad7e53e3 61
jessekaiser 4:db3dad7e53e3 62 if(pc.readable()) {
jessekaiser 4:db3dad7e53e3 63 c = pc.getc();
jessekaiser 4:db3dad7e53e3 64 }
jessekaiser 5:e4b9cc904928 65 } while((c !='1'));
jessekaiser 0:d5d3b731340c 66
jessekaiser 5:e4b9cc904928 67 c = '0';
jessekaiser 5:e4b9cc904928 68 pwm_motor1.write(0.5);
jessekaiser 5:e4b9cc904928 69 motordir1.write(0);
jessekaiser 5:e4b9cc904928 70 wait(3);
jessekaiser 5:e4b9cc904928 71
jessekaiser 5:e4b9cc904928 72 }*/
jessekaiser 0:d5d3b731340c 73 }
jessekaiser 0:d5d3b731340c 74
jessekaiser 0:d5d3b731340c 75