Control for RenBuggy
Dependencies: PID PinDetect mbed
RenBuggy_PID.h@1:8a2a7adb3c5d, 2014-03-05 (annotated)
- Committer:
- salatron
- Date:
- Wed Mar 05 13:37:10 2014 +0000
- Revision:
- 1:8a2a7adb3c5d
- Parent:
- 0:f414c64e674f
Version two of RenBuggy with PID control
; Can use with two different configurations of buggys
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
salatron | 0:f414c64e674f | 1 | /******************************************************************************* |
salatron | 0:f414c64e674f | 2 | * RenBED PID Motor Control for RenBuggy * |
salatron | 1:8a2a7adb3c5d | 3 | * Copyright (c) 2014 Sally Brown & Liz Lloyd * |
salatron | 0:f414c64e674f | 4 | * * |
salatron | 0:f414c64e674f | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy * |
salatron | 0:f414c64e674f | 6 | * of this software and associated documentation files (the "Software"), to deal* |
salatron | 0:f414c64e674f | 7 | * in the Software without restriction, including without limitation the rights * |
salatron | 0:f414c64e674f | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * |
salatron | 0:f414c64e674f | 9 | * copies of the Software, and to permit persons to whom the Software is * |
salatron | 0:f414c64e674f | 10 | * furnished to do so, subject to the following conditions: * |
salatron | 0:f414c64e674f | 11 | * * |
salatron | 0:f414c64e674f | 12 | * The above copyright notice and this permission notice shall be included in * |
salatron | 0:f414c64e674f | 13 | * all copies or substantial portions of the Software. * |
salatron | 0:f414c64e674f | 14 | * * |
salatron | 0:f414c64e674f | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * |
salatron | 0:f414c64e674f | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * |
salatron | 0:f414c64e674f | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * |
salatron | 0:f414c64e674f | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * |
salatron | 0:f414c64e674f | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* |
salatron | 0:f414c64e674f | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * |
salatron | 0:f414c64e674f | 21 | * THE SOFTWARE. * |
salatron | 0:f414c64e674f | 22 | * * |
salatron | 0:f414c64e674f | 23 | * PID_Controller.h * |
salatron | 0:f414c64e674f | 24 | * * |
salatron | 0:f414c64e674f | 25 | *******************************************************************************/ |
salatron | 0:f414c64e674f | 26 | |
salatron | 0:f414c64e674f | 27 | #ifndef _PIDCONTROLLER_H |
salatron | 0:f414c64e674f | 28 | #define _PIDCONTROLLER_H |
salatron | 0:f414c64e674f | 29 | |
salatron | 0:f414c64e674f | 30 | #include "mbed.h" |
salatron | 0:f414c64e674f | 31 | #include "PID.h" |
salatron | 0:f414c64e674f | 32 | #include "PinDetect.h" |
salatron | 0:f414c64e674f | 33 | |
salatron | 0:f414c64e674f | 34 | #define pi 3.1416 |
salatron | 0:f414c64e674f | 35 | #define RATE 0.001 |
salatron | 0:f414c64e674f | 36 | |
salatron | 0:f414c64e674f | 37 | class PID_Controller |
salatron | 0:f414c64e674f | 38 | { |
salatron | 1:8a2a7adb3c5d | 39 | public: |
salatron | 1:8a2a7adb3c5d | 40 | void SetUpConstants(int countsPerRev, float wheelCircumference, float axleLength); |
salatron | 0:f414c64e674f | 41 | |
salatron | 1:8a2a7adb3c5d | 42 | void Forwards(int distanceForward); |
salatron | 0:f414c64e674f | 43 | void Left(int angleLeft, int radiusLeft); |
salatron | 0:f414c64e674f | 44 | void Right(int angleRight, int radiusRight); |
salatron | 0:f414c64e674f | 45 | |
salatron | 1:8a2a7adb3c5d | 46 | void Stop(); |
salatron | 1:8a2a7adb3c5d | 47 | |
salatron | 1:8a2a7adb3c5d | 48 | void ConfigurePID(float p, float i, float d); |
salatron | 1:8a2a7adb3c5d | 49 | |
salatron | 1:8a2a7adb3c5d | 50 | void countL(); |
salatron | 1:8a2a7adb3c5d | 51 | void countR(); |
salatron | 1:8a2a7adb3c5d | 52 | |
salatron | 1:8a2a7adb3c5d | 53 | protected: |
salatron | 1:8a2a7adb3c5d | 54 | |
salatron | 1:8a2a7adb3c5d | 55 | PID_Controller(PinName motorL, PinName motorR, PinName brakeL, PinName brakeR); |
salatron | 1:8a2a7adb3c5d | 56 | virtual void setUpControllers(); |
salatron | 1:8a2a7adb3c5d | 57 | PID getLeftController() {return m_controllerL;} |
salatron | 1:8a2a7adb3c5d | 58 | PID getRightController() {return m_controllerR;} |
salatron | 1:8a2a7adb3c5d | 59 | |
salatron | 0:f414c64e674f | 60 | private: |
salatron | 0:f414c64e674f | 61 | |
salatron | 0:f414c64e674f | 62 | void doSomePID(); |
salatron | 0:f414c64e674f | 63 | void PIDLeft(); |
salatron | 0:f414c64e674f | 64 | void PIDRight(); |
salatron | 0:f414c64e674f | 65 | |
salatron | 0:f414c64e674f | 66 | PID m_controllerL; //Kc, Ti, Td, interval |
salatron | 0:f414c64e674f | 67 | PID m_controllerR; |
salatron | 1:8a2a7adb3c5d | 68 | |
salatron | 0:f414c64e674f | 69 | PwmOut m_motorL; |
salatron | 0:f414c64e674f | 70 | PwmOut m_motorR; |
salatron | 0:f414c64e674f | 71 | |
salatron | 0:f414c64e674f | 72 | DigitalOut m_brakeL; |
salatron | 0:f414c64e674f | 73 | DigitalOut m_brakeR; |
salatron | 0:f414c64e674f | 74 | |
salatron | 0:f414c64e674f | 75 | Ticker m_rate; |
salatron | 0:f414c64e674f | 76 | |
salatron | 1:8a2a7adb3c5d | 77 | int m_countsPerRev; |
salatron | 0:f414c64e674f | 78 | float m_wheelCircumference; |
salatron | 1:8a2a7adb3c5d | 79 | float m_axleLength; |
salatron | 0:f414c64e674f | 80 | |
salatron | 0:f414c64e674f | 81 | int m_stripesL; |
salatron | 0:f414c64e674f | 82 | int m_stripesR; |
salatron | 0:f414c64e674f | 83 | |
salatron | 0:f414c64e674f | 84 | bool m_turnLeft; |
salatron | 0:f414c64e674f | 85 | bool m_turnRight; |
salatron | 0:f414c64e674f | 86 | |
salatron | 0:f414c64e674f | 87 | float m_fProportionLeft; |
salatron | 0:f414c64e674f | 88 | float m_fProportionRight; |
salatron | 1:8a2a7adb3c5d | 89 | }; |
salatron | 1:8a2a7adb3c5d | 90 | |
salatron | 1:8a2a7adb3c5d | 91 | class PID_Stripes : public PID_Controller |
salatron | 1:8a2a7adb3c5d | 92 | { |
salatron | 1:8a2a7adb3c5d | 93 | public: |
salatron | 1:8a2a7adb3c5d | 94 | PID_Stripes(PinName motorL, PinName motorR, PinName brakeL, PinName brakeR, PinName sensorL, PinName sensorR); |
salatron | 1:8a2a7adb3c5d | 95 | |
salatron | 1:8a2a7adb3c5d | 96 | private: |
salatron | 1:8a2a7adb3c5d | 97 | PinDetect m_senseL; //Left encoder. Pin detect type is the debouncing. |
salatron | 1:8a2a7adb3c5d | 98 | PinDetect m_senseR; |
salatron | 1:8a2a7adb3c5d | 99 | }; |
salatron | 1:8a2a7adb3c5d | 100 | |
salatron | 1:8a2a7adb3c5d | 101 | class PID_Magnet : public PID_Controller |
salatron | 1:8a2a7adb3c5d | 102 | { |
salatron | 1:8a2a7adb3c5d | 103 | public: |
salatron | 1:8a2a7adb3c5d | 104 | PID_Magnet(PinName motorL, PinName motorR, PinName brakeL, PinName brakeR, PinName sensorL, PinName sensorR); |
salatron | 1:8a2a7adb3c5d | 105 | |
salatron | 1:8a2a7adb3c5d | 106 | private: |
salatron | 1:8a2a7adb3c5d | 107 | InterruptIn m_senseL; //Left encoder. |
salatron | 1:8a2a7adb3c5d | 108 | InterruptIn m_senseR; |
salatron | 0:f414c64e674f | 109 | }; |
salatron | 0:f414c64e674f | 110 | |
salatron | 0:f414c64e674f | 111 | #endif // _PIDCONTROLLER_H |