|
|
c! b7 L4 M" ^1 M* r, p- #include <Arduino.h>
. F+ a: J8 {6 B R0 Q - #include <MeAuriga.h>
* R, O% g1 t* I4 f* q& W - #include "MeLineFollowerArray.h"% m0 I- S( D3 |6 E- T
- : G9 h& n/ N2 [( B6 H
- MeEncoderOnBoard Encoder_1(SLOT1);
/ w% _+ P$ `, ` S - MeEncoderOnBoard Encoder_2(SLOT2);
* C$ g, @( ]8 E B) Z+ ^ - MeLightSensor lightsensor_1(12);( E9 J8 R' U8 B0 v
- MeLightSensor lightsensor_2(11);2 T, o+ p% v# K' R. D
- MeBuzzer buzzer;
* I. D1 x- k- y7 B, o% j3 m/ ? - MeLineFollowerArray linefollower(PORT_6);/ }" A. L+ U0 s) I6 d
- ' H: g9 I+ T' i. r4 ^( f9 P
- #define Error1 16 H! ]/ w7 u6 j# O4 [' O A
- #define Error2 2 C4 Y% U; L( G9 D
- #define Error3 3& k# _0 F# `" n7 i
) e2 R0 `- b( M- #define Kp 15
/ k5 ] ^, s: j, ?: O7 L5 {2 C - #define Ki 0.155 E0 P6 F2 c" Q
- #define Kd 0.03 j9 q; R7 i" o/ a3 t4 x( Q! v! U; i
- ) U2 ] V+ R2 ?
- uint8_t sData;
4 S7 b; y) t/ ^4 M8 h - uint8_t D1;
" D5 _% e+ w H4 J - uint8_t D2;- o$ F* M/ x! { t- N" x y3 N
- uint8_t D3;
9 Y0 K. T* X" Q( c - uint8_t D4;
! O; ]& Q) ^) ^; B7 ? - uint8_t D5;
6 m$ U3 {2 i0 r! p0 | - uint8_t D6; {) }' o! e- |0 f
5 S, n- y+ ]9 Z8 t! l" D- H' C( ~* Y. u- float previous_error = 0;
6 t8 G3 V+ }2 z3 V5 J - float integral = 0;
! u# S h0 |" E; g% R" X; L Z - float derivative = 0;' O! a9 u4 R- m4 I
- int Speed = 160;
- L; |7 b" N) O7 M - float output;
+ n% W1 ^ z- ]% q - ) c/ o# {+ {1 Z, m1 |
- byte Left;5 s9 L( Z" g3 o* W
- ) S) H0 x- I( D$ E/ g/ Y% _# W
- void setup() ( _9 v/ W0 ^6 b( L: m2 e6 I1 F
- {; s K( W9 }3 j3 h( _9 z
- //Set PWM 8KHz
% _7 Y1 M6 k2 \& c G - TCCR1A = _BV(WGM10);% q! \" M5 R _2 k2 c( A5 c
- TCCR1B = _BV(CS11) | _BV(WGM12);# h% v3 E. v: e- s/ w$ K
- TCCR2A = _BV(WGM21) | _BV(WGM20);
* e/ f3 K; X3 J( N- y' T - TCCR2B = _BV(CS21);7 v! m& R7 b$ i" ?: S% J/ j" t
- Serial.begin(9600);
8 v' `: b( u5 ~8 n9 a8 @; w+ V1 ? - buzzer.setpin(45);
4 F# g& b1 y D, C7 {3 e0 Q - }1 J# L! Z) {2 z# L
- 3 b5 c2 I8 V& ~( z
- void loop()
& B2 e# T$ p' W8 ~ - {
q' u; R" |8 e* J& R - while(!((lightsensor_1.read()) < (10)));
! V3 v/ M' k3 j% B - buzzer.tone(1047, 500);; `4 S/ E/ p: p' v, Q' ~
- delay(500);
+ O' U: e. h- _- ^ `3 g F - do
) m( w- x) Y/ h/ J& J9 i( ]6 k - {
# g/ X7 i& L# M" `! c: r/ T. [ - int Err = getErr();, c) }1 r1 j) K( _9 p1 h# p) s
- if(D1 == 1)+ L/ V: u: F4 V
- {8 I0 |$ z$ S5 X! e6 m+ j" z& o
- Left = 1;: ?" L( S' \; Y; L, X1 u! i( V o/ l
- }
7 {% W4 ]6 P+ n3 }5 n! ^" v& [ - if(D6 == 1)
! [/ K O; }$ k( e/ G - {
! D4 `* `* Y+ V - Left = 0;
; n& B' k) {( a" u - }9 E2 J5 S0 L* |$ c+ v( Q' W1 l* d
- if(Err == 99)% D4 S1 T$ Y# f; O- h
- {. O/ p4 p7 ^6 K- L
- if(Left == 1)/ t+ y' u9 N% y. W6 G# V, e- b6 `0 _
- {' A1 m# G. X1 }
- Speed -= 5;3 a$ V; C, j6 @# M: E! |! H5 k2 c8 i
- moto(0,Speed);
- v, h) `% @+ k4 @ - do
) V" @+ |$ X; w2 h - {( l6 P1 e! Z) |. K
- Err = getErr();: d& i8 V3 k& w6 v0 K
- }while((D1+D6) == 0);4 ?7 n( i8 M+ w
- } |5 P$ U0 D% T& k s- M
- else6 H6 P/ S8 \8 i; D
- {; }0 L- \% \4 ]1 z0 `
- Speed -= 5;
9 M$ m; w d' l9 ]2 k& ?; W: a! r - moto(Speed,0); - Y# c' o1 K/ I0 B6 p3 p
- do
* D8 ~6 X; Q5 c) F5 k2 ^ - {
- b R c+ d# H$ A8 g! O+ Q - Err = getErr();% K- b7 x; W) T A+ m! N
- }while((D1+D6) == 0);
4 [( d; ?- p6 D8 I9 H9 c - }
4 b# Q R4 R; Z+ Z7 a1 @ - }& ~. u, K8 ?, H; M: {
- else
) D" T3 Q* `* L* q - {
$ B5 z- t4 O/ c- [: L - if((Speed < 160) & (Err < 2)) Speed+=1; 7 i8 b2 {2 b6 m. y# N# | N$ H( T* G
- if((Speed > 100) & (Err > 2)) Speed-=2;
. l5 V2 E2 k7 m( h - integral = integral + Err;
% ^% \1 m8 ^! M9 Y7 C1 u) D1 i# d2 g - derivative = Err - previous_error;
8 `. \( m V, M" i - output = Kp*Err + Ki*integral + Kd*derivative;
' G0 B1 H$ P! y" L6 j; H - moto(int(Speed-output),int(Speed+output));
. x2 l! a# G! a- y - previous_error = Err;
; W' ?) L$ K/ X1 Z2 M - }0 P* p2 X& S5 Q* D7 q" F' l+ n
- }while(!((lightsensor_2.read()) < (10)));* M9 z# g( s7 }, c
- moto(0,0);
& _! a* |+ W1 h+ h; U( W9 [ - delay(500);
; |- _5 u L8 T$ @ z2 R* O& ~ - buzzer.tone(262, 500);* K! T3 Y: b# X4 l' } K
- }% o. c3 S; D$ i
- ! T9 F. i% I, ]8 {; q+ i7 u) p
- int getErr()
" h k8 e# {$ x, V4 ` - { % w7 n E4 Y' H. |8 L
- sData = linefollower.getValue();
: D0 Q( ]( i9 o% Z+ v$ ~$ d - D1 = ~(sData>>0)&1;
1 A2 f6 ` }. P! q - D2 = ~(sData>>1)&1;
! D1 }5 C2 v3 N% y4 g2 l! N - D3 = ~(sData>>2)&1;
# Z$ |9 W9 c! E0 x0 M( o) L - D4 = ~(sData>>3)&1; ?3 ~- _; {: m
- D5 = ~(sData>>4)&1;
* O+ G% e$ _. ~& ^' j$ O - D6 = ~(sData>>5)&1;
( J' y" F" ~6 Z; n! _/ l - int downD = D1+D2+D3+D4+D5+D6;$ `. ~2 o& R! h4 d) E; I: U
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);% m0 a# f" ~" g$ A2 M0 x7 F5 N
- if(downD == 0)- x/ d) I7 ?/ X, H
- {
) O8 J% H: i T' q# e3 o2 l7 h - return 99;- Y' x& ~% Q6 O7 u- a
- }
$ \! b3 q: @6 _* J - else+ z) {9 ?( ^' s! l) O6 d @
- {
! y. b( m0 W! E% S1 y. k2 h% A - return upD/downD;
, U$ T' ?# {9 g2 E - }
! Z& K; ~8 p0 a4 O. @* h4 n) b - }
$ d4 M6 L/ L! x( f7 y - : l8 b3 T$ Q, ]5 ]* l! A
- void moto(int Speed_L,int Speed_R)
( h. i1 l: o- l9 B1 K' h - {2 A; ?9 ?- V7 `" n k4 ~' b
- Encoder_2.setMotorPwm(Speed_L);
+ w% T' e+ ~5 M. t9 u: \8 t - Encoder_1.setMotorPwm(-Speed_R);5 Q# F: d( @$ i# A& p0 A
- }
複製代碼 5 a' t/ B8 P: l7 {3 d
MeLineFollowerArray.cpp' F" X# ?6 Z# a# g9 V% G
- #include "MeLineFollowerArray.h"/ F6 n7 r2 M5 k$ ?( A. Q, n ?+ G
- ) y' e0 b" M& K* S. L$ r# L
- #ifdef ME_PORT_DEFINED. F: `# P' o P. `. J& }
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
7 q$ D6 e9 y. e4 o+ Z4 f - {
! a( F$ Q8 ~/ [- f. [) l7 j+ y C - - t6 C0 V! @! {) w+ \
- }
. A. l0 U# A) y4 o* p - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
& Q0 g$ e4 I$ B0 B* V( _2 S* C - {
6 V: V, d6 \5 {/ W) ? - _DataPin = mePort[port].s2;9 t% W$ n! I) y' c' K- d5 U
- pinMode(_DataPin, OUTPUT);
" q8 ~. O/ a5 [# L: W4 n$ }' |6 P0 W; S - digitalWrite(_DataPin, HIGH);
. B$ n7 E/ [' L! A9 K: ]! ?1 q) f - }
i' C6 e, u/ \! Q - #else // ME_PORT_DEFINED2 l Z& M; E1 h) E
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)7 @ G1 Q x% P, y- u- Y
- {
# o& q% I" [' ] - _DataPin = pin;, b- S) d4 v7 T
- pinMode(_DataPin, OUTPUT); + J$ ~0 R8 G* m, [
- digitalWrite(_DataPin, HIGH);
6 r$ M6 j1 a K - }
+ N- U! V+ I, s% r - #endif // ME_PORT_DEFINED
e' B" E1 k; }7 x9 B0 } - 7 P) W, f3 y4 }5 V3 r; R# C: M. [
- ' o4 K8 j/ ^* L# E+ v
- void MeLineFollowerArray::setpin(uint8_t pin)3 b5 v: e5 n; N
- {
- ?' j( ^7 i. A! M5 M4 H9 c& v7 d - _DataPin = pin;$ P( Y% U" G5 p9 a1 d9 G$ L, |2 z
- pinMode(_DataPin, OUTPUT);
) B) W( @6 W5 Q% O( J! }2 f/ C/ u- d - digitalWrite(_DataPin, HIGH);
$ Y, h% r2 [2 i( S - 8 p6 Z& f* M# n$ b. F% J
- #ifdef ME_PORT_DEFINED
, Q2 V5 K( T, Z - s2 = pin;
8 g, r& e8 k! H [1 F - #endif) j* n. j; [& ?5 z& }6 P
- }
) b' ~( b/ [0 R+ ]: I
i6 h( P5 A' h4 I8 |/ ~- uint8_t MeLineFollowerArray::getValue()
6 }0 g, g% i) c3 n5 C - {
8 [" j% |& c: A7 v - uint32_t LOW_level_read_time;5 U% m$ r z/ q; q& t6 q0 ?* X
- uint32_t HIGH_level_read_time;
& g+ K' C. m2 b J" r5 K1 U - uint32_t time_out_flag;+ c+ z9 ~- ?6 L1 }, h i7 j
- uint8_t Sensor_Data[3];
' e* c* u! S7 D4 f - static uint8_t old_data = 0xff;
: ~5 u( M0 }" x I1 }
9 _: m h; g& {# A- d) C5 @- pinMode(_DataPin, OUTPUT);
" `7 w7 f1 ^# H3 @$ u9 O2 N" m - digitalWrite(_DataPin, LOW);
_0 r- D( }/ `& x# E - delayMicroseconds(980);% Q& J# l/ i$ k4 i X
- digitalWrite(_DataPin, HIGH);! \! O5 u- i1 E
( [) I) Y0 q* Z9 [- pinMode(_DataPin, INPUT_PULLUP);
3 o7 z! W0 [: D5 i, Z# o/ `2 U - delayMicroseconds(10);
3 v8 F0 r4 W( \3 R$ ]8 n3 h9 O - 1 P8 V/ t; g# j3 [ o
- time_out_flag = millis();
/ X+ `0 p% P3 Q4 W9 E# m5 L - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 d$ D# b% v. j3 l0 X0 x: x) y - . O4 v2 @: Q/ O/ P5 B8 L
- LOW_level_read_time = micros();7 s$ C- H& _2 V! M
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out. N, R: |' C3 o5 Z6 b+ P" d
- {
7 N3 R( C+ k, P7 j/ V8 f - return 0xff;# c( l( A# n. F) G1 y9 ^
- }3 E v8 K) M0 {5 k. ?4 A6 H1 W$ ^$ T
/ A" W7 ]# [7 Y$ C5 B3 Y* A- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
; }2 ]6 `$ ~ ?3 m: c
, M% ~! G% a* `/ C, y$ ^; `$ v$ ^- HIGH_level_read_time = micros();
1 d. Z4 m, T% Q% J - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
$ ^+ U2 K( }0 `' [ K$ Q8 B! W
$ [4 l" Z+ K- S2 G6 E% b- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out5 J9 Q* m0 p4 J2 U. u/ W
- {
! J# S3 u+ I K- p - return 0xff; y4 M z3 x- R F. W: J: p5 n1 Z
- }4 P, X6 P0 d8 p% Z
% Q' N" f) [5 ] }1 A+ Q+ J- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
0 l7 X% @* P7 Q5 t8 H" s4 G - {
$ N1 @ X# ?; _2 T p - return 0xff;
! ? m$ S* n$ K6 u - }& L8 z4 F2 }6 f1 E% v* |
- F# O1 b7 n7 B* {8 R* V
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
2 r* y4 E1 q# L6 H3 O/ S - LOW_level_read_time = micros();# U1 G( S8 G$ K6 M
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
0 \$ F: Y& a1 }+ D. D1 y% q! W
4 y9 q! k; d1 E) p/ h$ Y- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out J" ~; I, O1 j* q4 v- X
- {
9 X5 E1 O+ W& h, F0 K - return 0xff;
: p5 O& l& H' d9 U H2 _' h - }
0 ~5 f, f" `8 S( J/ G - / l2 ?; s7 ^: @: x# ?
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))3 G8 \ t7 r; ~8 Q: d+ j& T
- {2 f$ o0 E& M1 z
- return 0xff;: c* [/ i2 X4 F( G) s& l& ~
- }
" n% A2 ]4 k4 k8 A
# Q2 J6 o6 p" l" g% r; P- for(uint8_t k=0; k<3; k++)
/ K% O6 ^$ \7 N: ^. O" S' J6 I - {
6 p! B7 n l2 p* q0 L& [) w - Sensor_Data[k] = 0x00;" j- |$ _/ q" J2 O2 u" E
5 s; k7 G A6 P* X8 S8 ~- for(uint8_t i=0;i<8;i++)! k& B# J, [+ ]0 X
- {
3 F# z& }5 T# T, [6 c - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
7 B( f4 m3 a8 r9 ~3 J8 t - HIGH_level_read_time = micros();8 E/ B' D" h% O0 k
- LOW_level_read_time = micros() - LOW_level_read_time;6 M y7 j/ e& S0 N/ z" }% Q
- $ J7 p) e: S( O
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )5 y: T, x. }; u0 N7 g9 }( u
- {
+ u2 I7 P5 S4 P( ^! J. k5 [3 Y - return 0xff; T' [, U7 e) @
- }1 i" O4 `* E. a$ h9 \/ V/ x
# T i, B! l+ o- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
- Q& \- V5 h: N' J% Q - LOW_level_read_time = micros();
+ B. ]/ ]- y5 `9 h [9 Z2 Q( r- t - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level: @" ]1 f5 g8 d7 E
- ( [) X J( E5 H* x5 x6 W
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1# _- B9 j8 q+ t' R
- {
6 {$ E( j6 H \( J# {' J$ F - Sensor_Data[k] |= (0x80 >> i);" b/ N5 a5 m' x3 H0 }
- }. V" ~" S6 S- h! D) V& W; g, b M! n
- else if(HIGH_level_read_time >= 100)$ j) r1 r- J- b* x. m, R# V
- {1 Q/ O% h: s. ^1 ]' D; V$ ]
- return 0xff;9 V+ Q9 l w; ?" V# w/ |
- }8 C1 ]: k+ `" w( ~1 F `0 e4 u! a
- 1 p2 Z% E' N. C" i: m
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
! H: d0 z% p/ [ - {' \$ W5 v- p1 h7 z3 r: {
- return 0xff;% F+ |# C- p4 J" |3 W; _1 s' q
- }! P' y/ E2 e/ ~
- } A: _1 [) x7 ~& @4 F8 N
- }
4 A' M+ a& ?( k* ^/ h - * A ]1 H6 o2 A3 t/ H2 J% ~1 C
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level( E9 n a+ [1 R. f: x" u
- HIGH_level_read_time = micros();# j7 L2 ]8 g/ \- o6 W
- LOW_level_read_time = micros() - LOW_level_read_time;
8 C+ U5 A) [; `# i
/ {( P1 Z9 K B, o" z9 @- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )0 K4 B( F- F; E# d, p( | R
- {
8 Q2 Q. F+ S1 z2 u5 K - return 0xff;) b7 O5 l v! U! u2 h' J$ U4 V
- }& t, P2 O P: x6 j4 C) M
- ; x: g/ u0 x% b0 Y# K- d3 u2 S! H
- pinMode(_DataPin, OUTPUT);
+ ?/ F8 `$ \/ o8 g2 G C - digitalWrite(_DataPin, HIGH);7 u9 J3 S, L& A" q
1 F- y+ {8 o2 C7 k. K) ]8 ~: r- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
) G! E6 [" E7 [ - {$ I( p8 _$ @4 R
- old_data = Sensor_Data[0];
; _ }+ l% ^8 ~! Q - return Sensor_Data[0];
9 ~ v& ]1 O: f! a - }7 `# S! P8 A- f
- else9 H J1 @! V2 u/ D. Q2 o. M
- {
, L' g* r) n- { - return old_data;; w2 f$ z; w8 k4 R
- }& s( w; Y/ q$ Y. u" p. V4 D3 f
- } M) a0 a: \% t8 P( `7 [
複製代碼
$ L3 G) s' ~. |3 l: U |: @# h7 s+ d7 BMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
8 ~, y6 r# L5 Y* ]% A% o
% p# m' @& o( D6 M9 A7 ^ |
|