Dependencies: mbed nucleo_rotary_encoder
main.cpp@0:f246cceb43f6, 2016-08-21 (annotated)
- Committer:
- inst
- Date:
- Sun Aug 21 05:00:35 2016 +0000
- Revision:
- 0:f246cceb43f6
mbed lib ?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:f246cceb43f6 | 1 | #include "mbed.h" |
inst | 0:f246cceb43f6 | 2 | #include "rotary_encoder_a_phase.hpp" |
inst | 0:f246cceb43f6 | 3 | #include "rotary_encoder_ab_phase.hpp" |
inst | 0:f246cceb43f6 | 4 | |
inst | 0:f246cceb43f6 | 5 | int main() { |
inst | 0:f246cceb43f6 | 6 | const size_t encoder_num = 3; |
inst | 0:f246cceb43f6 | 7 | rotary_encoder* e[] = { |
inst | 0:f246cceb43f6 | 8 | new rotary_encoder_a_phase( /* timer_type = */TIM1, /* pulse_per_revol = */100), |
inst | 0:f246cceb43f6 | 9 | new rotary_encoder_ab_phase(/* timer_type = */TIM2, /* pulse_per_revol = */100), |
inst | 0:f246cceb43f6 | 10 | new rotary_encoder_ab_phase(/* timer_type = */TIM3, /* pulse_per_revol = */100) |
inst | 0:f246cceb43f6 | 11 | }; |
inst | 0:f246cceb43f6 | 12 | |
inst | 0:f246cceb43f6 | 13 | for (size_t i = 0; i < encoder_num; ++i) { |
inst | 0:f246cceb43f6 | 14 | e[i]->start(); |
inst | 0:f246cceb43f6 | 15 | } |
inst | 0:f246cceb43f6 | 16 | |
inst | 0:f246cceb43f6 | 17 | const float t = 2.0f; |
inst | 0:f246cceb43f6 | 18 | |
inst | 0:f246cceb43f6 | 19 | while (true) { |
inst | 0:f246cceb43f6 | 20 | wait(t); |
inst | 0:f246cceb43f6 | 21 | for (size_t i = 0; i < encoder_num; ++i) { |
inst | 0:f246cceb43f6 | 22 | // 回転数/秒 に60を掛けて 回転数/分 にする |
inst | 0:f246cceb43f6 | 23 | printf("%f, ", (e[i]->get_revol_num() / t) * 60.0f); |
inst | 0:f246cceb43f6 | 24 | e[i]->stop(); |
inst | 0:f246cceb43f6 | 25 | } |
inst | 0:f246cceb43f6 | 26 | printf("\n"); |
inst | 0:f246cceb43f6 | 27 | wait(t); |
inst | 0:f246cceb43f6 | 28 | |
inst | 0:f246cceb43f6 | 29 | for (size_t i = 0; i < encoder_num; ++i) { |
inst | 0:f246cceb43f6 | 30 | e[i]->start(); |
inst | 0:f246cceb43f6 | 31 | } |
inst | 0:f246cceb43f6 | 32 | wait(t); |
inst | 0:f246cceb43f6 | 33 | |
inst | 0:f246cceb43f6 | 34 | for (size_t i = 0; i < encoder_num; ++i) { |
inst | 0:f246cceb43f6 | 35 | // 回転数/秒 に60を掛けて 回転数/分 にする |
inst | 0:f246cceb43f6 | 36 | printf("%f, ", (e[i]->get_revol_num() / t) * 60.0f); |
inst | 0:f246cceb43f6 | 37 | e[i]->stop(); |
inst | 0:f246cceb43f6 | 38 | } |
inst | 0:f246cceb43f6 | 39 | printf("\n"); |
inst | 0:f246cceb43f6 | 40 | } |
inst | 0:f246cceb43f6 | 41 | |
inst | 0:f246cceb43f6 | 42 | return 0; |
inst | 0:f246cceb43f6 | 43 | } |