Dependents: serial_connected_mcu_nucleo rotary_encoder_mbed serial_connected_mcu_nucleo omuni_speed_pid ... more
Fork of rotary_encoder by
このライブラリは以下のプログラムに基いています https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
rotary_encoder_base/rotary_encoder_base.cpp@0:caf1d0bc4b90, 2016-02-06 (annotated)
- Committer:
- inst
- Date:
- Sat Feb 06 14:24:56 2016 +0000
- Revision:
- 0:caf1d0bc4b90
- Child:
- 2:4580c3869b7b
many classes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:caf1d0bc4b90 | 1 | #include "rotary_encoder_base.hpp" |
inst | 0:caf1d0bc4b90 | 2 | #include "rotary_encoder.hpp" |
inst | 0:caf1d0bc4b90 | 3 | |
inst | 0:caf1d0bc4b90 | 4 | rotary_encoder_base::rotary_encoder_base(TIM_TypeDef* timer_type, |
inst | 0:caf1d0bc4b90 | 5 | uint32_t encoder_mode, |
inst | 0:caf1d0bc4b90 | 6 | size_t resolution) : rotary_encoder(resolution) { |
inst | 0:caf1d0bc4b90 | 7 | TIM_Encoder_InitTypeDef encoder; |
inst | 0:caf1d0bc4b90 | 8 | timer_handler_ = new TIM_HandleTypeDef; |
inst | 0:caf1d0bc4b90 | 9 | |
inst | 0:caf1d0bc4b90 | 10 | timer_handler_->Instance = timer_type; |
inst | 0:caf1d0bc4b90 | 11 | timer_handler_->Init.Period = max_counts_; |
inst | 0:caf1d0bc4b90 | 12 | timer_handler_->Init.CounterMode = TIM_COUNTERMODE_UP; |
inst | 0:caf1d0bc4b90 | 13 | timer_handler_->Init.Prescaler = 0; |
inst | 0:caf1d0bc4b90 | 14 | timer_handler_->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
inst | 0:caf1d0bc4b90 | 15 | |
inst | 0:caf1d0bc4b90 | 16 | encoder.EncoderMode = encoder_mode; |
inst | 0:caf1d0bc4b90 | 17 | |
inst | 0:caf1d0bc4b90 | 18 | encoder.IC1Filter = 0x0F; |
inst | 0:caf1d0bc4b90 | 19 | encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; |
inst | 0:caf1d0bc4b90 | 20 | encoder.IC1Prescaler = TIM_ICPSC_DIV4; |
inst | 0:caf1d0bc4b90 | 21 | encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
inst | 0:caf1d0bc4b90 | 22 | |
inst | 0:caf1d0bc4b90 | 23 | encoder.IC2Filter = 0x0F; |
inst | 0:caf1d0bc4b90 | 24 | encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING; |
inst | 0:caf1d0bc4b90 | 25 | encoder.IC2Prescaler = TIM_ICPSC_DIV4; |
inst | 0:caf1d0bc4b90 | 26 | encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
inst | 0:caf1d0bc4b90 | 27 | |
inst | 0:caf1d0bc4b90 | 28 | if (HAL_TIM_Encoder_Init(timer_handler_, &encoder) != HAL_OK) { |
inst | 0:caf1d0bc4b90 | 29 | error("couldn't init encoder\r\n"); |
inst | 0:caf1d0bc4b90 | 30 | } |
inst | 0:caf1d0bc4b90 | 31 | } |
inst | 0:caf1d0bc4b90 | 32 | |
inst | 0:caf1d0bc4b90 | 33 | int32_t rotary_encoder_base::get_counts() const { |
inst | 0:caf1d0bc4b90 | 34 | int32_t counts = timer_handler_->Instance->CNT; |
inst | 0:caf1d0bc4b90 | 35 | |
inst | 0:caf1d0bc4b90 | 36 | if (counts > (max_counts_ >> 1)) { |
inst | 0:caf1d0bc4b90 | 37 | return counts - max_counts_; |
inst | 0:caf1d0bc4b90 | 38 | } |
inst | 0:caf1d0bc4b90 | 39 | |
inst | 0:caf1d0bc4b90 | 40 | return counts; |
inst | 0:caf1d0bc4b90 | 41 | } |
inst | 0:caf1d0bc4b90 | 42 | |
inst | 0:caf1d0bc4b90 | 43 | void rotary_encoder_base::reset() { |
inst | 0:caf1d0bc4b90 | 44 | timer_handler_->Instance->CNT = 0; |
inst | 0:caf1d0bc4b90 | 45 | } |
inst | 0:caf1d0bc4b90 | 46 | |
inst | 0:caf1d0bc4b90 | 47 | void rotary_encoder_base::start() { |
inst | 0:caf1d0bc4b90 | 48 | if(HAL_TIM_Encoder_Start(timer_handler_, TIM_CHANNEL_1) != HAL_OK) { |
inst | 0:caf1d0bc4b90 | 49 | error("couldn't start encoder\r\n"); |
inst | 0:caf1d0bc4b90 | 50 | } |
inst | 0:caf1d0bc4b90 | 51 | } |
inst | 0:caf1d0bc4b90 | 52 | |
inst | 0:caf1d0bc4b90 | 53 | void rotary_encoder_base::stop() { |
inst | 0:caf1d0bc4b90 | 54 | if(HAL_TIM_Encoder_Stop(timer_handler_, TIM_CHANNEL_1) != HAL_OK) { |
inst | 0:caf1d0bc4b90 | 55 | error("couldn't start encoder\r\n"); |
inst | 0:caf1d0bc4b90 | 56 | } |
inst | 0:caf1d0bc4b90 | 57 | } |