Simple controller class for Stinger Robot without using Robotics Connection board.
Robot.h@1:b4d202b471ae, 2010-10-13 (annotated)
- Committer:
- gtg795y
- Date:
- Wed Oct 13 03:18:54 2010 +0000
- Revision:
- 1:b4d202b471ae
- Parent:
- 0:fc95840345e8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gtg795y | 0:fc95840345e8 | 1 | #ifndef ROBOT_H |
gtg795y | 0:fc95840345e8 | 2 | #define ROBOT_H |
gtg795y | 0:fc95840345e8 | 3 | |
gtg795y | 0:fc95840345e8 | 4 | // This library was designed simplify operation of Stinger robot only with MBED and two H-Bridges. |
gtg795y | 0:fc95840345e8 | 5 | // Is is still in development. |
gtg795y | 0:fc95840345e8 | 6 | // Class Robot integrated QEI, PID and Motor classes to work together through use of Ticker function to |
gtg795y | 0:fc95840345e8 | 7 | // regularly poll QEI objects, adjust PID and Motor objects. |
gtg795y | 0:fc95840345e8 | 8 | // Right now all pins have been preassigned, later empty constructor will be added with individual pin assignments. |
gtg795y | 0:fc95840345e8 | 9 | // Pinouts are: |
gtg795y | 0:fc95840345e8 | 10 | // rightMotor = new Motor(p23, p6, p5); // pwm, fwd, rev |
gtg795y | 0:fc95840345e8 | 11 | // leftMotor = new Motor(p22, p8, p7); // pwm, fwd, rev |
gtg795y | 0:fc95840345e8 | 12 | // leftQei = new QEI(p29, p30, NC, 624, QEI::X2_ENCODING); //chanA, chanB, index, ppr |
gtg795y | 0:fc95840345e8 | 13 | // rightQei = new QEI(p27, p28, NC, 624, QEI::X2_ENCODING); //chanB, chanA, index, ppr |
gtg795y | 0:fc95840345e8 | 14 | // leftPid = new PID(0.4312, 0.1, 0.0, RATE); //Kc, Ti, Td |
gtg795y | 0:fc95840345e8 | 15 | // rightPid = new PID(0.4312, 0.1, 0.0, RATE); //Kc, Ti, Td |
gtg795y | 0:fc95840345e8 | 16 | |
gtg795y | 0:fc95840345e8 | 17 | #include "mbed.h" |
gtg795y | 0:fc95840345e8 | 18 | #include "Motor.h" |
gtg795y | 0:fc95840345e8 | 19 | #include "QEI.h" |
gtg795y | 0:fc95840345e8 | 20 | #include "PID.h" |
gtg795y | 0:fc95840345e8 | 21 | |
gtg795y | 0:fc95840345e8 | 22 | #define MAX_SPEED 3200 |
gtg795y | 0:fc95840345e8 | 23 | #define RATE 0.05 |
gtg795y | 0:fc95840345e8 | 24 | #define DPP 14.2 //Pulses per degree of motor rotation |
gtg795y | 0:fc95840345e8 | 25 | |
gtg795y | 1:b4d202b471ae | 26 | /** |
gtg795y | 1:b4d202b471ae | 27 | * Robot class. |
gtg795y | 1:b4d202b471ae | 28 | */ |
gtg795y | 0:fc95840345e8 | 29 | |
gtg795y | 0:fc95840345e8 | 30 | class Robot { |
gtg795y | 0:fc95840345e8 | 31 | public: |
gtg795y | 1:b4d202b471ae | 32 | /** |
gtg795y | 1:b4d202b471ae | 33 | * Constructor |
gtg795y | 1:b4d202b471ae | 34 | */ |
gtg795y | 0:fc95840345e8 | 35 | Robot(); |
gtg795y | 1:b4d202b471ae | 36 | /** |
gtg795y | 1:b4d202b471ae | 37 | * Destructor |
gtg795y | 1:b4d202b471ae | 38 | */ |
gtg795y | 0:fc95840345e8 | 39 | virtual ~Robot(); |
gtg795y | 1:b4d202b471ae | 40 | /** |
gtg795y | 1:b4d202b471ae | 41 | * Move Straight |
gtg795y | 1:b4d202b471ae | 42 | * @param s speed between -1.0 and 1.0 |
gtg795y | 1:b4d202b471ae | 43 | * @param clix Number of pulses to move |
gtg795y | 1:b4d202b471ae | 44 | */ |
gtg795y | 0:fc95840345e8 | 45 | void MoveStraightPulses(float s, int clix); |
gtg795y | 1:b4d202b471ae | 46 | /** |
gtg795y | 1:b4d202b471ae | 47 | * Move Straight |
gtg795y | 1:b4d202b471ae | 48 | * @param speed speed between -1.0 and 1.0 |
gtg795y | 1:b4d202b471ae | 49 | * @param in Number of inches to move |
gtg795y | 1:b4d202b471ae | 50 | */ |
gtg795y | 0:fc95840345e8 | 51 | void MoveStraightInches(float speed, float in); |
gtg795y | 1:b4d202b471ae | 52 | /** |
gtg795y | 1:b4d202b471ae | 53 | * Move Straight |
gtg795y | 1:b4d202b471ae | 54 | * @param seed speed between -1.0 and 1.0 |
gtg795y | 1:b4d202b471ae | 55 | * @param rotations Number of wheel rotations to move |
gtg795y | 1:b4d202b471ae | 56 | */ |
gtg795y | 0:fc95840345e8 | 57 | void MoveStraightRotations(float speed, float rotations); |
gtg795y | 1:b4d202b471ae | 58 | /** |
gtg795y | 1:b4d202b471ae | 59 | * Stop Left Wheel |
gtg795y | 1:b4d202b471ae | 60 | */ |
gtg795y | 0:fc95840345e8 | 61 | void StopLeft(); |
gtg795y | 1:b4d202b471ae | 62 | /** |
gtg795y | 1:b4d202b471ae | 63 | * Stop Right Wheel |
gtg795y | 1:b4d202b471ae | 64 | */ |
gtg795y | 0:fc95840345e8 | 65 | void StopRight(); |
gtg795y | 1:b4d202b471ae | 66 | /** |
gtg795y | 1:b4d202b471ae | 67 | * Stop |
gtg795y | 1:b4d202b471ae | 68 | */ |
gtg795y | 0:fc95840345e8 | 69 | void Stop(); |
gtg795y | 1:b4d202b471ae | 70 | /** |
gtg795y | 1:b4d202b471ae | 71 | * Pivet left |
gtg795y | 1:b4d202b471ae | 72 | * @param deg degrees |
gtg795y | 1:b4d202b471ae | 73 | */ |
gtg795y | 0:fc95840345e8 | 74 | void PivetLeft(float deg); |
gtg795y | 1:b4d202b471ae | 75 | /** |
gtg795y | 1:b4d202b471ae | 76 | * Pivet right |
gtg795y | 1:b4d202b471ae | 77 | * @param deg degrees |
gtg795y | 1:b4d202b471ae | 78 | */ |
gtg795y | 0:fc95840345e8 | 79 | void PivetRight(float deg); |
gtg795y | 0:fc95840345e8 | 80 | void RotateLeftWheel(float rpm, float deg); //Rotate Wheel <deg> Degrees |
gtg795y | 0:fc95840345e8 | 81 | void RotateRightWheel(float rpm, float deg); //Rotate Wheel <deg> Degrees |
gtg795y | 1:b4d202b471ae | 82 | /** |
gtg795y | 1:b4d202b471ae | 83 | * Returns 0 if robot is not moving |
gtg795y | 1:b4d202b471ae | 84 | * Returns 1 otherwise |
gtg795y | 1:b4d202b471ae | 85 | * @returns flat |
gtg795y | 1:b4d202b471ae | 86 | */ |
gtg795y | 0:fc95840345e8 | 87 | int IsBusy(); |
gtg795y | 0:fc95840345e8 | 88 | |
gtg795y | 0:fc95840345e8 | 89 | |
gtg795y | 0:fc95840345e8 | 90 | protected: |
gtg795y | 0:fc95840345e8 | 91 | Motor *rightMotor; |
gtg795y | 0:fc95840345e8 | 92 | Motor *leftMotor; |
gtg795y | 0:fc95840345e8 | 93 | QEI *leftQei; |
gtg795y | 0:fc95840345e8 | 94 | QEI *rightQei; |
gtg795y | 0:fc95840345e8 | 95 | PID *leftPid; |
gtg795y | 0:fc95840345e8 | 96 | PID *rightPid; |
gtg795y | 0:fc95840345e8 | 97 | Serial *pc; |
gtg795y | 1:b4d202b471ae | 98 | /** |
gtg795y | 1:b4d202b471ae | 99 | * Ticker function |
gtg795y | 1:b4d202b471ae | 100 | */ |
gtg795y | 0:fc95840345e8 | 101 | void Call(); |
gtg795y | 0:fc95840345e8 | 102 | |
gtg795y | 0:fc95840345e8 | 103 | private: |
gtg795y | 0:fc95840345e8 | 104 | Ticker ticker; |
gtg795y | 0:fc95840345e8 | 105 | volatile int leftPulses; |
gtg795y | 0:fc95840345e8 | 106 | volatile int rightPulses; |
gtg795y | 0:fc95840345e8 | 107 | volatile int leftPrevPulses; |
gtg795y | 0:fc95840345e8 | 108 | volatile int rightPrevPulses; |
gtg795y | 0:fc95840345e8 | 109 | volatile int leftPulsesGoTo; |
gtg795y | 0:fc95840345e8 | 110 | volatile int rightPulsesGoTo; |
gtg795y | 0:fc95840345e8 | 111 | volatile int leftSpeed; |
gtg795y | 0:fc95840345e8 | 112 | volatile int rightSpeed; |
gtg795y | 0:fc95840345e8 | 113 | volatile float rightVelocity; |
gtg795y | 0:fc95840345e8 | 114 | volatile float leftVelocity; |
gtg795y | 0:fc95840345e8 | 115 | float leftDirection; |
gtg795y | 0:fc95840345e8 | 116 | float rightDirection; |
gtg795y | 0:fc95840345e8 | 117 | }; |
gtg795y | 0:fc95840345e8 | 118 | |
gtg795y | 0:fc95840345e8 | 119 | #endif /* ROBOT_H */ |