Numero Uno / Mbed 2 deprecated TheProgram

Dependencies:   EMG HIDScope PID QEI mbed TextLCD

Committer:
ewoud
Date:
Mon Nov 02 11:12:46 2015 +0000
Revision:
11:c5042e19a096
Parent:
10:819fb5288aa0
The final thing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ewoud 3:70f78cfc0f25 1
ewoud 2:8f9b6c1e89cf 2 #define RATE 0.005f
ewoud 0:ca94aa9bf823 3 #define Kc 0.30
ewoud 0:ca94aa9bf823 4 #define Ti 0.00
ewoud 0:ca94aa9bf823 5 #define Td 0.0
ewoud 0:ca94aa9bf823 6
ewoud 3:70f78cfc0f25 7 ///////////////////////
ewoud 3:70f78cfc0f25 8 /// pin layout ////////
ewoud 3:70f78cfc0f25 9 ///////////////////////
ewoud 3:70f78cfc0f25 10
ewoud 0:ca94aa9bf823 11 Serial pc(USBTX, USBRX);
ewoud 8:b0971033dc41 12 TextLCD lcd(D9, D14, D15, PTB19, PTB18, A4); // rs, e, d4 to d7
ewoud 3:70f78cfc0f25 13
ewoud 8:b0971033dc41 14 // Left motor. (motor connection 1)
ewoud 8:b0971033dc41 15 PwmOut leftMotor(D5);
ewoud 0:ca94aa9bf823 16 DigitalOut leftDirection(D4);
ewoud 0:ca94aa9bf823 17 QEI leftQei(D12, D13, NC, 624);
ewoud 0:ca94aa9bf823 18 PID leftController(Kc, Ti, Td, RATE);
ewoud 0:ca94aa9bf823 19
ewoud 8:b0971033dc41 20 // Right motor (motor connection 2)
ewoud 0:ca94aa9bf823 21 PwmOut rightMotor(D6);
ewoud 0:ca94aa9bf823 22 DigitalOut rightDirection(D7);
ewoud 0:ca94aa9bf823 23 QEI rightQei(D11, D10, NC, 624);
ewoud 0:ca94aa9bf823 24 PID rightController(Kc, Ti, Td, RATE);
ewoud 0:ca94aa9bf823 25
ewoud 0:ca94aa9bf823 26 // edge leads
ewoud 3:70f78cfc0f25 27 DigitalIn edgeleft(PTC5);
ewoud 3:70f78cfc0f25 28 DigitalIn edgeright(PTC7);
ewoud 3:70f78cfc0f25 29 DigitalIn edgetop(PTC0);
ewoud 3:70f78cfc0f25 30 DigitalIn edgebottom(PTC9);
ewoud 10:819fb5288aa0 31 DigitalIn edgeStart(PTC8);
ewoud 10:819fb5288aa0 32 DigitalIn edgeFinish(PTC1);
ewoud 0:ca94aa9bf823 33
ewoud 3:70f78cfc0f25 34 // user interface
ewoud 10:819fb5288aa0 35 DigitalOut statusled(LED_GREEN);
ewoud 3:70f78cfc0f25 36 InterruptIn startButton(D3);
ewoud 10:819fb5288aa0 37 bool calibrated=false; // bool to determine if button press has to start calibration or the game
ewoud 3:70f78cfc0f25 38 InterruptIn stopButton(D2);
ewoud 6:ae2ce89dd695 39 InterruptIn initpositionButton(PTC6);
ewoud 11:c5042e19a096 40 HIDScope scope(6);
ewoud 10:819fb5288aa0 41 bool controlbypc=false;
ewoud 11:c5042e19a096 42 DigitalOut musicHit(A5);
ewoud 11:c5042e19a096 43
ewoud 3:70f78cfc0f25 44
ewoud 3:70f78cfc0f25 45 ////////////////////////////
ewoud 3:70f78cfc0f25 46 //////// Timers ////////////
ewoud 3:70f78cfc0f25 47 ////////////////////////////
ewoud 9:3e19a344c025 48 Timer playTimer;
ewoud 11:c5042e19a096 49 int playTime;
ewoud 0:ca94aa9bf823 50 Ticker motorControlTicker;
ewoud 9:3e19a344c025 51 Ticker lcdTicker;
ewoud 9:3e19a344c025 52 bool lcdGoFlag=false;
ewoud 0:ca94aa9bf823 53 bool goFlag=false;
ewoud 0:ca94aa9bf823 54 bool systemOn=false;
ewoud 6:ae2ce89dd695 55 bool initposition_go=false;
ewoud 3:70f78cfc0f25 56
ewoud 3:70f78cfc0f25 57
ewoud 3:70f78cfc0f25 58 ////////////////////////////
ewoud 3:70f78cfc0f25 59 ///////// Variables ////////
ewoud 3:70f78cfc0f25 60 ////////////////////////////
ewoud 3:70f78cfc0f25 61
ewoud 3:70f78cfc0f25 62
ewoud 3:70f78cfc0f25 63 // Working variables for motors
ewoud 10:819fb5288aa0 64 volatile float leftPulses = 0;
ewoud 0:ca94aa9bf823 65 volatile float leftAngle=0;
ewoud 10:819fb5288aa0 66 volatile float leftPrevPulses = 0;
ewoud 0:ca94aa9bf823 67 volatile float leftPwmDuty = 0.0;
ewoud 0:ca94aa9bf823 68 volatile float leftVelocity = 0.0;
ewoud 0:ca94aa9bf823 69 float leftRequest=0;
ewoud 0:ca94aa9bf823 70
ewoud 10:819fb5288aa0 71 volatile float rightPulses = 0;
ewoud 10:819fb5288aa0 72 volatile float rightPrevPulses = 0;
ewoud 0:ca94aa9bf823 73 volatile float rightAngle =0;
ewoud 0:ca94aa9bf823 74 volatile float rightPwmDuty = 0.0;
ewoud 0:ca94aa9bf823 75 volatile float rightVelocity = 0.0;
ewoud 0:ca94aa9bf823 76 float rightRequest=0;
ewoud 0:ca94aa9bf823 77
ewoud 3:70f78cfc0f25 78 // working vars for calculation
ewoud 3:70f78cfc0f25 79 float horrequest;
ewoud 3:70f78cfc0f25 80 float verrequest;
ewoud 3:70f78cfc0f25 81 float grenslaag=0.3;
ewoud 3:70f78cfc0f25 82 float grenshoog=0.7;
ewoud 10:819fb5288aa0 83 float round=4200.0*6.6*6.6;
ewoud 9:3e19a344c025 84 float maxspeed=1; //cm/sec
ewoud 0:ca94aa9bf823 85 float currentX;
ewoud 0:ca94aa9bf823 86 float currentY;
ewoud 11:c5042e19a096 87 float l=50;
ewoud 0:ca94aa9bf823 88 float toX;
ewoud 0:ca94aa9bf823 89 float toY;
ewoud 0:ca94aa9bf823 90 float toLeftAngle;
ewoud 0:ca94aa9bf823 91 float toRightAngle;
ewoud 0:ca94aa9bf823 92 float M_PI=3.14159265359;
ewoud 9:3e19a344c025 93 string text;
ewoud 11:c5042e19a096 94 int hitCount=0;
ewoud 11:c5042e19a096 95 float hitTime=0;
ewoud 11:c5042e19a096 96 bool started=false;
ewoud 0:ca94aa9bf823 97
ewoud 3:70f78cfc0f25 98 //////////////////////////////////
ewoud 3:70f78cfc0f25 99 ////// initialize functions //////
ewoud 3:70f78cfc0f25 100 //////////////////////////////////
ewoud 0:ca94aa9bf823 101
ewoud 0:ca94aa9bf823 102 void initMotors(){
ewoud 0:ca94aa9bf823 103 //Initialization of motor
ewoud 0:ca94aa9bf823 104 leftMotor.period_us(50);
ewoud 0:ca94aa9bf823 105 leftMotor = 0.0;
ewoud 0:ca94aa9bf823 106 leftDirection = 1;
ewoud 0:ca94aa9bf823 107
ewoud 0:ca94aa9bf823 108 rightMotor.period_us(50);
ewoud 0:ca94aa9bf823 109 rightMotor = 0.0;
ewoud 0:ca94aa9bf823 110 rightDirection = 1;
ewoud 0:ca94aa9bf823 111
ewoud 0:ca94aa9bf823 112 }
ewoud 0:ca94aa9bf823 113
ewoud 0:ca94aa9bf823 114 void initPIDs(){
ewoud 0:ca94aa9bf823 115 //Initialization of PID controller
ewoud 3:70f78cfc0f25 116 leftController.setInputLimits(-90.0, 90.0);
ewoud 0:ca94aa9bf823 117 leftController.setOutputLimits(-1.0, 1.0);
ewoud 0:ca94aa9bf823 118 leftController.setBias(0);
ewoud 0:ca94aa9bf823 119 leftController.setDeadzone(-0.4, 0.4);
ewoud 0:ca94aa9bf823 120 leftController.setMode(AUTO_MODE);
ewoud 0:ca94aa9bf823 121
ewoud 3:70f78cfc0f25 122 rightController.setInputLimits(-90.0, 90.0);
ewoud 0:ca94aa9bf823 123 rightController.setOutputLimits(-1.0, 1.0);
ewoud 0:ca94aa9bf823 124 rightController.setBias(0);
ewoud 0:ca94aa9bf823 125 rightController.setDeadzone(-0.4, 0.4);
ewoud 0:ca94aa9bf823 126 rightController.setMode(AUTO_MODE);
ewoud 0:ca94aa9bf823 127 }