encoder modify from arduino
Dependencies: mbed Nucleo_Hello_Encoder
encoder1.cpp@0:88abae0234d5, 2021-11-01 (annotated)
- Committer:
- ea78anana
- Date:
- Mon Nov 01 15:39:01 2021 +0000
- Revision:
- 0:88abae0234d5
encoder modify form arduino
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ea78anana | 0:88abae0234d5 | 1 | #include "mbed.h" |
ea78anana | 0:88abae0234d5 | 2 | |
ea78anana | 0:88abae0234d5 | 3 | #define PinA1 D14 //外部中断0 |
ea78anana | 0:88abae0234d5 | 4 | #define PinZ1 D12 //外部中断1 |
ea78anana | 0:88abae0234d5 | 5 | #define PinB1 D15 //编码器的OUT_B信号连接到数字端口8 |
ea78anana | 0:88abae0234d5 | 6 | |
ea78anana | 0:88abae0234d5 | 7 | #define PinA2 D8 //外部中断0 |
ea78anana | 0:88abae0234d5 | 8 | #define PinZ2 D6 //外部中断1 |
ea78anana | 0:88abae0234d5 | 9 | #define PinB2 D7 //编码器的OUT_B信号连接到数字端口8 |
ea78anana | 0:88abae0234d5 | 10 | |
ea78anana | 0:88abae0234d5 | 11 | #define PinA3 D4 //外部中断0 |
ea78anana | 0:88abae0234d5 | 12 | #define PinZ3 D3 //外部中断1 |
ea78anana | 0:88abae0234d5 | 13 | #define PinB3 D2 //编码器的OUT_B信号连接到数字端口8 |
ea78anana | 0:88abae0234d5 | 14 | #define T 30 //定义采集时间周期单位ms |
ea78anana | 0:88abae0234d5 | 15 | |
ea78anana | 0:88abae0234d5 | 16 | Timer f; |
ea78anana | 0:88abae0234d5 | 17 | unsigned long time1 = 0; // 时间标记 |
ea78anana | 0:88abae0234d5 | 18 | volatile long PulSum_CW = 0; //定义长整型不可修改脉冲数 |
ea78anana | 0:88abae0234d5 | 19 | volatile long PulSum_CCW = 0; |
ea78anana | 0:88abae0234d5 | 20 | |
ea78anana | 0:88abae0234d5 | 21 | |
ea78anana | 0:88abae0234d5 | 22 | long PulSum_CW_t0 = 0; //定义记录顺时针方向 t0 时刻脉冲数变量 |
ea78anana | 0:88abae0234d5 | 23 | long PulSum_CW_t0_T = 0; //定义记录顺时针方向 t0+T 时刻脉冲数变量 |
ea78anana | 0:88abae0234d5 | 24 | |
ea78anana | 0:88abae0234d5 | 25 | |
ea78anana | 0:88abae0234d5 | 26 | float Rad_CW_Speed = 0.000; //定义顺时针角速度 |
ea78anana | 0:88abae0234d5 | 27 | float Rad_CCW_Speed = 0.000; //定义逆时针角速度 |
ea78anana | 0:88abae0234d5 | 28 | |
ea78anana | 0:88abae0234d5 | 29 | DigitalIn ain11(PinA1);//因为编码器信号为欧姆龙E6B2-CWZ6C,为开漏输出,因此需要上拉电阻,此处采用arduino的内部上拉输入模式,置高 |
ea78anana | 0:88abae0234d5 | 30 | DigitalIn ain12(PinB1);//同上 |
ea78anana | 0:88abae0234d5 | 31 | DigitalIn ain13(PinZ1);//脉冲中断函数:捕捉A相信号,并判断A、B相先后顺序 |
ea78anana | 0:88abae0234d5 | 32 | |
ea78anana | 0:88abae0234d5 | 33 | DigitalIn ain21(PinA2);//因为编码器信号为欧姆龙E6B2-CWZ6C,为开漏输出,因此需要上拉电阻,此处采用arduino的内部上拉输入模式,置高 |
ea78anana | 0:88abae0234d5 | 34 | DigitalIn ain22(PinB2);//同上 |
ea78anana | 0:88abae0234d5 | 35 | DigitalIn ain23(PinZ2);//脉冲中断函数:捕捉A相信号,并判断A、B相先后顺序 |
ea78anana | 0:88abae0234d5 | 36 | |
ea78anana | 0:88abae0234d5 | 37 | DigitalIn ain31(PinA3);//因为编码器信号为欧姆龙E6B2-CWZ6C,为开漏输出,因此需要上拉电阻,此处采用arduino的内部上拉输入模式,置高 |
ea78anana | 0:88abae0234d5 | 38 | DigitalIn ain32(PinB3);//同上 |
ea78anana | 0:88abae0234d5 | 39 | DigitalIn ain33(PinZ3);//脉冲中断函数:捕捉A相信号,并判断A、B相先后顺序 |
ea78anana | 0:88abae0234d5 | 40 | |
ea78anana | 0:88abae0234d5 | 41 | |
ea78anana | 0:88abae0234d5 | 42 | |
ea78anana | 0:88abae0234d5 | 43 | long PulSum_CCW_t0 = 0; //定义记录逆时针方向 t0 时刻脉冲数变量 |
ea78anana | 0:88abae0234d5 | 44 | long PulSum_CCW_t0_T = 0; //定义记录逆时针方向 t0+T 时刻脉冲数变量 |
ea78anana | 0:88abae0234d5 | 45 | |
ea78anana | 0:88abae0234d5 | 46 | // 记录角度 |
ea78anana | 0:88abae0234d5 | 47 | int Rad = 180; |
ea78anana | 0:88abae0234d5 | 48 | |
ea78anana | 0:88abae0234d5 | 49 | |
ea78anana | 0:88abae0234d5 | 50 | |
ea78anana | 0:88abae0234d5 | 51 | void loop() |
ea78anana | 0:88abae0234d5 | 52 | { |
ea78anana | 0:88abae0234d5 | 53 | // 角度计算程序 |
ea78anana | 0:88abae0234d5 | 54 | Rad = int((PulSum_CW - PulSum_CCW)/6.944) % 360; |
ea78anana | 0:88abae0234d5 | 55 | if (Rad>=180) { |
ea78anana | 0:88abae0234d5 | 56 | Rad = Rad - 360; |
ea78anana | 0:88abae0234d5 | 57 | } |
ea78anana | 0:88abae0234d5 | 58 | |
ea78anana | 0:88abae0234d5 | 59 | PulSum_CW_t0 = PulSum_CW; |
ea78anana | 0:88abae0234d5 | 60 | PulSum_CCW_t0 = PulSum_CCW; //采集t0时刻的脉冲数 |
ea78anana | 0:88abae0234d5 | 61 | wait(T); //等待一个T时间 |
ea78anana | 0:88abae0234d5 | 62 | PulSum_CW_t0_T = PulSum_CW; |
ea78anana | 0:88abae0234d5 | 63 | PulSum_CCW_t0_T = PulSum_CCW; //采集t0+T时刻的脉冲数 |
ea78anana | 0:88abae0234d5 | 64 | wait(T); //等待一个T时间 |
ea78anana | 0:88abae0234d5 | 65 | |
ea78anana | 0:88abae0234d5 | 66 | // 如果检测到正向角速度,那么就打印角速度,以及累计的角度 |
ea78anana | 0:88abae0234d5 | 67 | if (PulSum_CW_t0_T - PulSum_CW_t0 != 0){ |
ea78anana | 0:88abae0234d5 | 68 | Rad_CW_Speed = (PulSum_CW_t0_T - PulSum_CW_t0); // |
ea78anana | 0:88abae0234d5 | 69 | // 显示正向(CW)角速度 Rad_CW_Speed---> //打印出来速度 |
ea78anana | 0:88abae0234d5 | 70 | //printf("AS:"); //打印出来速度w |
ea78anana | 0:88abae0234d5 | 71 | printf("%f", Rad_CW_Speed); |
ea78anana | 0:88abae0234d5 | 72 | printf(":"); |
ea78anana | 0:88abae0234d5 | 73 | //printf("/A:"); |
ea78anana | 0:88abae0234d5 | 74 | printf("%d",Rad); |
ea78anana | 0:88abae0234d5 | 75 | printf("\n"); |
ea78anana | 0:88abae0234d5 | 76 | } |
ea78anana | 0:88abae0234d5 | 77 | |
ea78anana | 0:88abae0234d5 | 78 | // 如果检测到反向角速度,那么就打印角速度,以及累计的角度 |
ea78anana | 0:88abae0234d5 | 79 | if (PulSum_CCW_t0_T - PulSum_CCW_t0 != 0){ |
ea78anana | 0:88abae0234d5 | 80 | Rad_CCW_Speed = (PulSum_CCW_t0 - PulSum_CCW_t0_T); |
ea78anana | 0:88abae0234d5 | 81 | //printf("AS:"); |
ea78anana | 0:88abae0234d5 | 82 | printf("%f", Rad_CCW_Speed); |
ea78anana | 0:88abae0234d5 | 83 | printf(":"); |
ea78anana | 0:88abae0234d5 | 84 | //printf("/A:"); |
ea78anana | 0:88abae0234d5 | 85 | printf("%d",Rad); |
ea78anana | 0:88abae0234d5 | 86 | printf("\n"); |
ea78anana | 0:88abae0234d5 | 87 | } |
ea78anana | 0:88abae0234d5 | 88 | |
ea78anana | 0:88abae0234d5 | 89 | |
ea78anana | 0:88abae0234d5 | 90 | |
ea78anana | 0:88abae0234d5 | 91 | } |
ea78anana | 0:88abae0234d5 | 92 | void Encode() |
ea78anana | 0:88abae0234d5 | 93 | {//当编码器码盘的OUTA脉冲信号下跳沿每中断一次, |
ea78anana | 0:88abae0234d5 | 94 | f.start(); |
ea78anana | 0:88abae0234d5 | 95 | uint32_t m = f.read_ms(); |
ea78anana | 0:88abae0234d5 | 96 | if ((m - time1) > 5) |
ea78anana | 0:88abae0234d5 | 97 | { |
ea78anana | 0:88abae0234d5 | 98 | if (((ain11 == 0) && (ain12 == 1)) || ((ain21 == 0) && (ain22 == 1)) || ((ain31 == 0) && (ain32 == 1)) ) |
ea78anana | 0:88abae0234d5 | 99 | {PulSum_CW ++;} |
ea78anana | 0:88abae0234d5 | 100 | else |
ea78anana | 0:88abae0234d5 | 101 | {PulSum_CCW ++;} |
ea78anana | 0:88abae0234d5 | 102 | } |
ea78anana | 0:88abae0234d5 | 103 | time1 = m; |
ea78anana | 0:88abae0234d5 | 104 | f.stop(); |
ea78anana | 0:88abae0234d5 | 105 | } |