This library is used for student's experiment. Chiba Institute of Technology
Dependencies: QEI SoftwarePWM
Fork of adrobo by
Motor.cpp@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 | #include "mbed.h" |
yasuohayashibara | 0:4508c5b68135 | 2 | #include "Motor.h" |
yasuohayashibara | 0:4508c5b68135 | 3 | |
yasuohayashibara | 0:4508c5b68135 | 4 | #define max(a,b) (a > b ? a : b) |
yasuohayashibara | 0:4508c5b68135 | 5 | #define min(a,b) (a > b ? b : a) |
yasuohayashibara | 0:4508c5b68135 | 6 | |
yasuohayashibara | 0:4508c5b68135 | 7 | Motor::Motor(PinName pin0, PinName pin1) |
yasuohayashibara | 0:4508c5b68135 | 8 | : pwm0(pin0), pwm1(pin1), max_ratio_(0.5f) |
yasuohayashibara | 0:4508c5b68135 | 9 | { |
yasuohayashibara | 0:4508c5b68135 | 10 | period(0.01f); |
yasuohayashibara | 0:4508c5b68135 | 11 | write(0.0f); |
yasuohayashibara | 0:4508c5b68135 | 12 | } |
yasuohayashibara | 0:4508c5b68135 | 13 | |
yasuohayashibara | 0:4508c5b68135 | 14 | void Motor::setMaxRatio(float max_ratio) |
yasuohayashibara | 0:4508c5b68135 | 15 | { |
yasuohayashibara | 0:4508c5b68135 | 16 | max_ratio_ = max_ratio; |
yasuohayashibara | 0:4508c5b68135 | 17 | } |
yasuohayashibara | 0:4508c5b68135 | 18 | |
yasuohayashibara | 0:4508c5b68135 | 19 | void Motor::period(float period) |
yasuohayashibara | 0:4508c5b68135 | 20 | { |
yasuohayashibara | 0:4508c5b68135 | 21 | period_ = period; |
yasuohayashibara | 0:4508c5b68135 | 22 | pwm0.period(period_); |
yasuohayashibara | 0:4508c5b68135 | 23 | pwm1.period(period_); |
yasuohayashibara | 0:4508c5b68135 | 24 | } |
yasuohayashibara | 0:4508c5b68135 | 25 | |
yasuohayashibara | 0:4508c5b68135 | 26 | void Motor::write(float value) |
yasuohayashibara | 0:4508c5b68135 | 27 | { |
yasuohayashibara | 0:4508c5b68135 | 28 | value = min(max(value, -1.0f), 1.0f); |
yasuohayashibara | 0:4508c5b68135 | 29 | value_ = value; |
yasuohayashibara | 0:4508c5b68135 | 30 | pwm0 = value_ > 0.0f ? value_ : 0.0f; |
yasuohayashibara | 0:4508c5b68135 | 31 | pwm1 = value_ < 0.0f ? -value_ : 0.0f; |
yasuohayashibara | 0:4508c5b68135 | 32 | } |
yasuohayashibara | 0:4508c5b68135 | 33 | |
yasuohayashibara | 0:4508c5b68135 | 34 | float Motor::read() |
yasuohayashibara | 0:4508c5b68135 | 35 | { |
yasuohayashibara | 0:4508c5b68135 | 36 | return value_; |
yasuohayashibara | 0:4508c5b68135 | 37 | } |