Brandon Modon Encoder_Nucleo_16_bits

Committer:
Brand101
Date:
Tue Feb 21 15:33:10 2017 +0000
Revision:
7:cd502aeab71a
Parent:
6:11dea4c204ba
45465446

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkoichy 0:ebd170807e11 1 #ifndef ENCODER_H
kkoichy 0:ebd170807e11 2 #define ENCODER_H
kkoichy 0:ebd170807e11 3 #include "mbed.h"
kkoichy 2:b83f2956cc8e 4 #include "CAN.h"
kkoichy 4:734474ae54b9 5 /*
kkoichy 4:734474ae54b9 6 #include "mbed.h"
kkoichy 4:734474ae54b9 7 #include "Nucleo_Encoder_16_bits.h"
kkoichy 4:734474ae54b9 8 #include "CAN.h"
kkoichy 4:734474ae54b9 9
CoupeCachan 6:11dea4c204ba 10
kkoichy 4:734474ae54b9 11 * HAL_TIM_Encoder_MspInit()
kkoichy 4:734474ae54b9 12 * Overrides the __weak function stub in stm32f4xx_hal_tim.h
kkoichy 4:734474ae54b9 13 *
kkoichy 4:734474ae54b9 14 * Edit the below for your preferred pin wiring & pullup/down
kkoichy 4:734474ae54b9 15 * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
kkoichy 4:734474ae54b9 16 * Encoder A&B outputs connected directly to GPIOs.
kkoichy 4:734474ae54b9 17 *
kkoichy 4:734474ae54b9 18 * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00102166.pdf
kkoichy 4:734474ae54b9 19 * www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00141306.pdf
kkoichy 4:734474ae54b9 20 *
kkoichy 4:734474ae54b9 21 * TIM1_CH1: AF1 @ PA_8, PE_9
kkoichy 4:734474ae54b9 22 * TIM1_CH2: AF1 @ PA_9, PE_11
kkoichy 4:734474ae54b9 23 *
kkoichy 4:734474ae54b9 24 * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PB_8* *F446 only
kkoichy 4:734474ae54b9 25 * TIM2_CH2: AF1 @ PA_1, PB_3, PB_9* *F446 only
kkoichy 4:734474ae54b9 26 *
kkoichy 4:734474ae54b9 27 * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6
kkoichy 4:734474ae54b9 28 * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7
kkoichy 4:734474ae54b9 29 *
kkoichy 4:734474ae54b9 30 * TIM4_CH1: AF2 @ PB_6, PD_12
kkoichy 4:734474ae54b9 31 * TIM4_CH2: AF2 @ PB_7, PD_13
kkoichy 4:734474ae54b9 32 *
kkoichy 4:734474ae54b9 33 * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable
kkoichy 4:734474ae54b9 34 * TIM5_CH2: AF2 @ PA_1*
kkoichy 4:734474ae54b9 35 *
kkoichy 5:018659d4022a 36
kkoichy 4:734474ae54b9 37
kkoichy 4:734474ae54b9 38
kkoichy 4:734474ae54b9 39 //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
kkoichy 4:734474ae54b9 40 #ifdef TARGET_STM32F3
kkoichy 4:734474ae54b9 41 #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
kkoichy 4:734474ae54b9 42 #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
kkoichy 4:734474ae54b9 43 #endif
kkoichy 4:734474ae54b9 44 #define PERIMETRE_ROUE_CODEUSE 160.0
kkoichy 4:734474ae54b9 45 #define LARGEUR_ROBOT 270.0
kkoichy 4:734474ae54b9 46 #define PI 3.14159265359
kkoichy 4:734474ae54b9 47 #define RESOLUTION_ROUE_CODEUSE 1024.0
kkoichy 4:734474ae54b9 48
kkoichy 4:734474ae54b9 49
kkoichy 4:734474ae54b9 50 Nucleo_Encoder_16_bits encoder1(TIM3, 0xffff, TIM_ENCODERMODE_TI12);
kkoichy 4:734474ae54b9 51 Nucleo_Encoder_16_bits encoder2(TIM1, 0xffff, TIM_ENCODERMODE_TI12);
kkoichy 4:734474ae54b9 52
kkoichy 4:734474ae54b9 53 int cpt = 0, dist_d, dist_g;
kkoichy 4:734474ae54b9 54 double D, theta, x, y;
kkoichy 4:734474ae54b9 55
kkoichy 4:734474ae54b9 56 int main()
kkoichy 4:734474ae54b9 57 {
kkoichy 4:734474ae54b9 58 printf("Debut du prog");
kkoichy 4:734474ae54b9 59
kkoichy 4:734474ae54b9 60
kkoichy 4:734474ae54b9 61 while(1)
kkoichy 4:734474ae54b9 62 {
kkoichy 4:734474ae54b9 63 wait_ms(10);
kkoichy 4:734474ae54b9 64 dist_d = encoder1.GetCounter() - dist_d;
kkoichy 4:734474ae54b9 65 dist_g = encoder2.GetCounter() - dist_g;
kkoichy 4:734474ae54b9 66 D = 0.5 * (dist_d + dist_g);
kkoichy 4:734474ae54b9 67 theta += 0.5 * (dist_d - dist_g) * 360.0 * PERIMETRE_ROUE_CODEUSE/(LARGEUR_ROBOT*PI*RESOLUTION_ROUE_CODEUSE);
kkoichy 4:734474ae54b9 68 x = D * cos(theta * PI / 180.0) * PERIMETRE_ROUE_CODEUSE / RESOLUTION_ROUE_CODEUSE;
kkoichy 4:734474ae54b9 69 y = D * sin(theta * PI / 180.0) * PERIMETRE_ROUE_CODEUSE / RESOLUTION_ROUE_CODEUSE;
kkoichy 4:734474ae54b9 70 cpt ++;
kkoichy 4:734474ae54b9 71 if(cpt >= 50)
kkoichy 4:734474ae54b9 72 {
kkoichy 4:734474ae54b9 73 printf("\n\rX : %d\n\rY : %d\n\rTheta : %d\n\r**********\n\r", (int)x, (int)y, (int)theta);
kkoichy 4:734474ae54b9 74 }
kkoichy 4:734474ae54b9 75 }
kkoichy 4:734474ae54b9 76
kkoichy 4:734474ae54b9 77 }
kkoichy 4:734474ae54b9 78 */
kkoichy 0:ebd170807e11 79
kkoichy 0:ebd170807e11 80 void EncoderInit(TIM_Encoder_InitTypeDef * encoder, TIM_HandleTypeDef * timer, TIM_TypeDef * TIMx, uint32_t maxcount, uint32_t encmode);
kkoichy 0:ebd170807e11 81
kkoichy 0:ebd170807e11 82
kkoichy 0:ebd170807e11 83 namespace mbed{
kkoichy 0:ebd170807e11 84
kkoichy 0:ebd170807e11 85 class Nucleo_Encoder_16_bits {
kkoichy 0:ebd170807e11 86
kkoichy 0:ebd170807e11 87 public :
kkoichy 1:e82009479b5c 88 Nucleo_Encoder_16_bits(TIM_TypeDef * _TIM);
kkoichy 0:ebd170807e11 89 Nucleo_Encoder_16_bits(TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode);
kkoichy 0:ebd170807e11 90 Nucleo_Encoder_16_bits(TIM_Encoder_InitTypeDef * _encoder, TIM_HandleTypeDef * _timer, TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode);
kkoichy 0:ebd170807e11 91 int32_t GetCounter();
kkoichy 0:ebd170807e11 92 TIM_HandleTypeDef* GetTimer();
kkoichy 0:ebd170807e11 93
kkoichy 0:ebd170807e11 94 private :
kkoichy 0:ebd170807e11 95
kkoichy 0:ebd170807e11 96
kkoichy 0:ebd170807e11 97 protected :
kkoichy 0:ebd170807e11 98 TIM_Encoder_InitTypeDef encoder;
kkoichy 0:ebd170807e11 99 TIM_HandleTypeDef timer;
kkoichy 0:ebd170807e11 100 TIM_TypeDef * TIM;
kkoichy 0:ebd170807e11 101
kkoichy 0:ebd170807e11 102 };
kkoichy 0:ebd170807e11 103
kkoichy 0:ebd170807e11 104 }// namespace mbed
kkoichy 0:ebd170807e11 105
kkoichy 0:ebd170807e11 106 #endif