![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Arduino version 2
Diff: arduino2.cpp
- Revision:
- 1:4df9f94facd5
- Parent:
- 0:c9022327ece6
- Child:
- 2:46365fee3503
diff -r c9022327ece6 -r 4df9f94facd5 arduino2.cpp --- a/arduino2.cpp Mon Nov 01 15:41:50 2021 +0000 +++ b/arduino2.cpp Tue Nov 02 15:27:52 2021 +0000 @@ -1,9 +1,9 @@ #include "mbed.h" #include "arduino.h" -#define PinA A0 //A phase -#define PinZ A2 //Z phase -#define PinB A1 //B phase +#define PinA A1 //A phase +#define PinZ A3 //Z phase +#define PinB A2 //B phase #define HIGH 1 #define LOW 0 @@ -13,31 +13,83 @@ float time_cw; float time_ccw; int counter = 0; -const float d = 6/100; //Radius of the wheel +const float d = 0.06; //Radius of the wheel const float pi = 3.141592654;//PI int num = 0;//number of turns double t;//time per turn float velocity; -double time3;//外部中断1产生时的时间,即捕捉到Z相的置零信号时,用于在loop循环内进行一圈时间长短的计算 +double time3;//Time of phase Z detected, use for calculate the velocity +Timer f; DigitalIn a11(PinA); DigitalIn a12(PinB); -DigitalIn a13(PinZ); +InterruptIn a14(PinA); +InterruptIn a13(PinZ); + Serial pc(USBTX, USBRX); +void Encode() +{ + //Ignore noise pulses + + //When the two reading is bigger than 5ms then count it as correct pulse + //uint32_t m = f.read_ms(); + + //if ((m - time1) > 5) + + { + + //When Phase A is falling will be detected + + if ((a11 == LOW) && (a12 == HIGH)) + + { pc.printf("backward "); + + counter--; + + } + + else + + { pc.printf("fordward "); + + counter++; + + } + + } + + //time1 = m; + +} + +void setup(){ + a11.mode(PullUp); + a12.mode(PullUp); + a13.fall(&Encode); + //a14.rise(&Encode); + //a14.fall(&Encode); +} + +void Set_state(){ + counter = 0; + uint32_t p = f.read_ms(); + time3 = p;//Time when Z phase detected +} void loop() { double distance; //clockwise turning - if (counter == 2500) + uint32_t n = f.read_ms(); + if (counter == 25) { // Printf("ok");//Testing - num = num++; - time_cw = millis(); - t = time_cw - time3; + num = num + 25; + Set_state(); + t = n- time3; t = t / 1000; distance = num * d * pi; velocity = d * pi / t; @@ -46,12 +98,12 @@ pc.printf("The time is ");pc.printf("%f", t); pc.printf("s."); } //anti-clockwise turning - else if (counter == -2500) + else if (counter == -25) { // Printf("ok");//Testing - num = num++; - time_ccw = millis(); - t = time_ccw - time3; + num = num + 25; + Set_state(); + t = n - time3; t = t / 1000; distance = num * d * pi; velocity = d * pi / t; @@ -59,46 +111,16 @@ pc.printf("The cw_speed is ");pc.printf("%f", velocity); pc.printf("m/s."); pc.printf("The time is ");pc.printf("%f", t); pc.printf("s."); } + //pc.printf("%d \n",counter); } -// 编码器计数中断子程序 -void Encode() - -{ - - //为了不计入噪音干扰脉冲, - - //当2次中断之间的时间大于5ms时,计一次有效计数 - - if ((millis() - time1) > 5) - - { - //当编码器码盘的OUTA脉冲信号下跳沿每中断一次, - - if ((a11 == LOW) && (a12 == HIGH)) - - { - - counter--; - - } - - else - - { - - counter++; - - } - +int main(void){ + setup(); + pc.printf("start"); + f.start(); + while(1){ + loop(); } - - time1 = millis(); - -} -void Set_state(){ - counter = 0; - time3 = millis();//发生中断时的时间 } \ No newline at end of file