Sik Chiu Chow
/
arduino2
Arduino version 2
Diff: arduino2.cpp
- Revision:
- 0:c9022327ece6
- Child:
- 1:4df9f94facd5
diff -r 000000000000 -r c9022327ece6 arduino2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arduino2.cpp Mon Nov 01 15:41:50 2021 +0000 @@ -0,0 +1,104 @@ +#include "mbed.h" +#include "arduino.h" + +#define PinA A0 //A phase +#define PinZ A2 //Z phase +#define PinB A1 //B phase + +#define HIGH 1 +#define LOW 0 + +//Initialize Variable +unsigned long time1 = 0; // Time +float time_cw; +float time_ccw; +int counter = 0; +const float d = 6/100; //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循环内进行一圈时间长短的计算 + +DigitalIn a11(PinA); +DigitalIn a12(PinB); +DigitalIn a13(PinZ); +Serial pc(USBTX, USBRX); + + + +void loop() + +{ + double distance; + //clockwise turning + if (counter == 2500) + { +// Printf("ok");//Testing + num = num++; + time_cw = millis(); + t = time_cw - time3; + t = t / 1000; + distance = num * d * pi; + velocity = d * pi / t; + pc.printf("The wheel has run ");pc.printf("%f", distance); pc.printf("m."); + 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."); + } + //anti-clockwise turning + else if (counter == -2500) + { +// Printf("ok");//Testing + num = num++; + time_ccw = millis(); + t = time_ccw - time3; + t = t / 1000; + distance = num * d * pi; + velocity = d * pi / t; + pc.printf("The wheel has run ");pc.printf("%f", distance); pc.printf("m."); + 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."); + } +} + +// 编码器计数中断子程序 + +void Encode() + +{ + + //为了不计入噪音干扰脉冲, + + //当2次中断之间的时间大于5ms时,计一次有效计数 + + if ((millis() - time1) > 5) + + { + + //当编码器码盘的OUTA脉冲信号下跳沿每中断一次, + + if ((a11 == LOW) && (a12 == HIGH)) + + { + + counter--; + + } + + else + + { + + counter++; + + } + + } + + time1 = millis(); + +} +void Set_state(){ + counter = 0; + time3 = millis();//发生中断时的时间 +} \ No newline at end of file