This library is used for student's experiment. Chiba Institute of Technology
Dependencies: QEI SoftwarePWM
Fork of adrobo by
Motor.h@1:45772cb4e01c, 2020-04-20 (annotated)
- Committer:
- yasuohayashibara
- Date:
- Mon Apr 20 04:07:37 2020 +0000
- Revision:
- 1:45772cb4e01c
- Parent:
- 0:4508c5b68135
add required libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasuohayashibara | 0:4508c5b68135 | 1 | // Chiba Institute of Technology |
yasuohayashibara | 0:4508c5b68135 | 2 | |
yasuohayashibara | 0:4508c5b68135 | 3 | #ifndef MBED_MOTOR_H |
yasuohayashibara | 0:4508c5b68135 | 4 | #define MBED_MOTOR_H |
yasuohayashibara | 0:4508c5b68135 | 5 | |
yasuohayashibara | 0:4508c5b68135 | 6 | #include "mbed.h" |
yasuohayashibara | 0:4508c5b68135 | 7 | |
yasuohayashibara | 0:4508c5b68135 | 8 | /** Class to control a L293E motor driver on PWM out pin |
yasuohayashibara | 0:4508c5b68135 | 9 | * |
yasuohayashibara | 0:4508c5b68135 | 10 | * Example: |
yasuohayashibara | 0:4508c5b68135 | 11 | * @code |
yasuohayashibara | 0:4508c5b68135 | 12 | * // Motor Control |
yasuohayashibara | 0:4508c5b68135 | 13 | * #include "mbed.h" |
yasuohayashibara | 0:4508c5b68135 | 14 | * #include "Motor.h" |
yasuohayashibara | 0:4508c5b68135 | 15 | * |
yasuohayashibara | 0:4508c5b68135 | 16 | * Motor motor(A1, A2); |
yasuohayashibara | 0:4508c5b68135 | 17 | * |
yasuohayashibara | 0:4508c5b68135 | 18 | * int main(){ |
yasuohayashibara | 0:4508c5b68135 | 19 | * while(1) { |
yasuohayashibara | 0:4508c5b68135 | 20 | * for (int i = -100; i < 100; i += 1) { |
yasuohayashibara | 0:4508c5b68135 | 21 | * motor = (float)i/100.0f; |
yasuohayashibara | 0:4508c5b68135 | 22 | * wait_ms(20); |
yasuohayashibara | 0:4508c5b68135 | 23 | * } |
yasuohayashibara | 0:4508c5b68135 | 24 | * } |
yasuohayashibara | 0:4508c5b68135 | 25 | * } |
yasuohayashibara | 0:4508c5b68135 | 26 | * @endcode |
yasuohayashibara | 0:4508c5b68135 | 27 | */ |
yasuohayashibara | 0:4508c5b68135 | 28 | |
yasuohayashibara | 0:4508c5b68135 | 29 | class Motor |
yasuohayashibara | 0:4508c5b68135 | 30 | { |
yasuohayashibara | 0:4508c5b68135 | 31 | public: |
yasuohayashibara | 0:4508c5b68135 | 32 | /** Create a new SoftwarePWM object on pwm pin |
yasuohayashibara | 0:4508c5b68135 | 33 | * |
yasuohayashibara | 0:4508c5b68135 | 34 | * @param Pin Pin on mbed to connect PWM device to |
yasuohayashibara | 0:4508c5b68135 | 35 | */ |
yasuohayashibara | 0:4508c5b68135 | 36 | Motor(PinName pin0, PinName pin1); |
yasuohayashibara | 0:4508c5b68135 | 37 | |
yasuohayashibara | 0:4508c5b68135 | 38 | void setMaxRatio(float max_ratio); |
yasuohayashibara | 0:4508c5b68135 | 39 | |
yasuohayashibara | 0:4508c5b68135 | 40 | void period(float period); |
yasuohayashibara | 0:4508c5b68135 | 41 | |
yasuohayashibara | 0:4508c5b68135 | 42 | void write(float value); |
yasuohayashibara | 0:4508c5b68135 | 43 | |
yasuohayashibara | 0:4508c5b68135 | 44 | float read(); |
yasuohayashibara | 0:4508c5b68135 | 45 | |
yasuohayashibara | 0:4508c5b68135 | 46 | //#ifdef MBED_OPERATORS |
yasuohayashibara | 0:4508c5b68135 | 47 | /** A operator shorthand for write() |
yasuohayashibara | 0:4508c5b68135 | 48 | */ |
yasuohayashibara | 0:4508c5b68135 | 49 | Motor& operator= (float value) { |
yasuohayashibara | 0:4508c5b68135 | 50 | write(value); |
yasuohayashibara | 0:4508c5b68135 | 51 | return *this; |
yasuohayashibara | 0:4508c5b68135 | 52 | } |
yasuohayashibara | 0:4508c5b68135 | 53 | |
yasuohayashibara | 0:4508c5b68135 | 54 | Motor& operator= (Motor& rhs) { |
yasuohayashibara | 0:4508c5b68135 | 55 | write(rhs.read()); |
yasuohayashibara | 0:4508c5b68135 | 56 | return *this; |
yasuohayashibara | 0:4508c5b68135 | 57 | } |
yasuohayashibara | 0:4508c5b68135 | 58 | |
yasuohayashibara | 0:4508c5b68135 | 59 | /** An operator shorthand for read() |
yasuohayashibara | 0:4508c5b68135 | 60 | */ |
yasuohayashibara | 0:4508c5b68135 | 61 | operator float() { |
yasuohayashibara | 0:4508c5b68135 | 62 | return read(); |
yasuohayashibara | 0:4508c5b68135 | 63 | } |
yasuohayashibara | 0:4508c5b68135 | 64 | //#endif |
yasuohayashibara | 0:4508c5b68135 | 65 | |
yasuohayashibara | 0:4508c5b68135 | 66 | private: |
yasuohayashibara | 0:4508c5b68135 | 67 | PwmOut pwm0; |
yasuohayashibara | 0:4508c5b68135 | 68 | PwmOut pwm1; |
yasuohayashibara | 0:4508c5b68135 | 69 | float value_; |
yasuohayashibara | 0:4508c5b68135 | 70 | float period_; |
yasuohayashibara | 0:4508c5b68135 | 71 | float max_ratio_; |
yasuohayashibara | 0:4508c5b68135 | 72 | }; |
yasuohayashibara | 0:4508c5b68135 | 73 | |
yasuohayashibara | 0:4508c5b68135 | 74 | #endif |