|
* x2 \9 r1 W0 K' m$ G1 r8 T
- #include <Arduino.h>
: u$ _, |$ U2 P: |" u' A1 U - #include <MeAuriga.h>
$ O: d! I8 k# c0 _2 B - #include "MeLineFollowerArray.h"
% b L2 w# H! w! F3 m1 Z
; \6 E4 U8 \! {- MeEncoderOnBoard Encoder_1(SLOT1);
- Q6 `% S, B/ \. i N - MeEncoderOnBoard Encoder_2(SLOT2);; a' {9 X1 p& c& Z$ U1 |
- MeLightSensor lightsensor_1(12);4 E) L3 M/ b- m6 g
- MeLightSensor lightsensor_2(11);7 b1 d, U' q9 R0 u# D
- MeBuzzer buzzer;
! k2 Y, j# w6 q - MeLineFollowerArray linefollower(PORT_6);
5 J& R! w+ Q4 t. e7 u
3 g( i J" l3 V' p7 k- #define Error1 18 A- a1 _7 N3 d* C1 E' q3 l
- #define Error2 2
, @# i9 t. |* _0 M - #define Error3 3: V2 m) J# `2 s4 s: z
/ b9 r& h& K& E* k3 ?- #define Kp 159 g. {0 z# h+ T' P: v6 g9 Q
- #define Ki 0.15: z5 z0 l+ [, s
- #define Kd 0.03
/ a1 m4 Q$ W, e0 P - 8 o2 h6 o& F. n3 y# b7 h
- uint8_t sData;# w4 j4 l6 D1 d+ u5 z4 q7 v
- uint8_t D1;+ B- C% y. L) V( E- e) e" ^
- uint8_t D2; F2 w3 U4 j- |: W) e) g/ x. c# X
- uint8_t D3;, x2 g+ x$ Y9 z% G/ a
- uint8_t D4;0 c! E) ]0 O! d+ J `$ Q
- uint8_t D5;+ p5 N& Q$ a( c5 ]* k& b
- uint8_t D6;+ J. u+ {, R& u2 R# m. w
- ) S, D3 t+ u7 L9 o2 J) S) D
- float previous_error = 0;" n# y* P H( W% ^1 f
- float integral = 0; a, m$ H# {9 a9 g9 m
- float derivative = 0;
) Z0 m1 Q1 E9 ~/ U. \9 _ - int Speed = 160;% V* H C' a# t( z6 a+ c
- float output;
* P3 s! N- w7 N% H6 Y
4 n4 c1 B) ]# ]. J8 F) \: P! e6 y- byte Left;6 `1 g: U5 E& y, O
# f3 B' u7 U) K- void setup() 8 U# W; i% i. [1 v) ~ o- W( {
- {! f: E% o' n3 I
- //Set PWM 8KHz1 p; q! ?5 {) H- l( e: k0 i# ^
- TCCR1A = _BV(WGM10);
/ {$ {( J* t7 b7 | - TCCR1B = _BV(CS11) | _BV(WGM12);/ c& G4 |" x A* k- M* b
- TCCR2A = _BV(WGM21) | _BV(WGM20);0 w8 o! h( e7 K7 w
- TCCR2B = _BV(CS21);
$ e! k8 ]6 x1 @0 H' E - Serial.begin(9600);
/ A4 O* H; T; f5 M1 J6 H - buzzer.setpin(45);; E. O2 W9 r8 J7 o0 b1 i
- }) ]! [/ D/ G6 ^3 |# z
- 7 b$ u! v5 b+ f0 U9 ~: p
- void loop(): ]% O! L# q) P6 m; H. a$ C9 ~& D! [
- {
8 Z1 C' W+ d) |. h/ x& ` - while(!((lightsensor_1.read()) < (10)));7 b# N- d- f2 R- l/ X( w ?
- buzzer.tone(1047, 500);
" ^: E2 H0 o0 V2 W4 D - delay(500);
: U/ N7 d: F' ~- S7 v7 R% N" x6 d0 S5 Y - do
* I; T% p M+ q& }5 z, `/ w - {
. Y! `! i+ D: x- |$ ^ - int Err = getErr();
+ `- E! V! q- F. A - if(D1 == 1)
6 A$ r' G- [- B/ p' [ - {
; ~2 E& `9 [1 |2 \ - Left = 1;
1 g+ V4 k0 ^4 i3 b1 c5 O ^ - }2 I$ Q5 f5 G$ z, \# f* d/ u# X
- if(D6 == 1)
8 w# w4 n; T! ], @4 b - {* u: U* }! W% E: ~! o
- Left = 0;
. S( a9 N" L4 }+ ]% U% ?$ q - }- s5 x. \' O! h' g% C2 c
- if(Err == 99)7 u4 M' n2 A- ]! i! y
- {
9 T+ B# S' C4 Z; S0 h- e. O - if(Left == 1)9 }1 e+ _" @& X+ K% {! T
- {6 H8 N7 t: @+ C$ g
- Speed -= 5;
6 ]8 ], A0 ~# w% j/ t - moto(0,Speed);4 v% m$ s; x! F9 M) [
- do I' n% a: V! w' _* f' s# [" K; y
- {
0 Y* M- c5 J5 G# f# b# K: ~& i+ M - Err = getErr();3 V5 g1 I( b+ X3 j* N% g
- }while((D1+D6) == 0);
* a/ ~" T" w" \6 i - }- _6 V6 j" X6 \( C* y7 Z1 g6 J: U
- else
! A& e0 o* N( m - {$ P8 ]$ C! j- q. T
- Speed -= 5;
1 @- v9 q6 V& h. z# g- h' }% r - moto(Speed,0);
4 a+ T# _. Z8 B: H1 o - do% V; V$ h' [5 S2 N: j8 }7 l
- {0 L, Q u: e0 l* g* N6 x
- Err = getErr();! I K& c; o9 s& L% _1 m
- }while((D1+D6) == 0);
* G& D' r0 _8 U# z; S) k# o - }
7 d' }& k! @( X: l7 v* W( k" d, t1 q - }
9 K" Z% E0 q& ] - else
4 p$ y0 T+ V/ G - {) V( q9 E% p2 d w
- if((Speed < 160) & (Err < 2)) Speed+=1;
! [( o$ K7 \! {: i8 ~ - if((Speed > 100) & (Err > 2)) Speed-=2;
1 ^: B8 A" @0 L, z S - integral = integral + Err;
. q* z; B/ P& L# l, t6 ` - derivative = Err - previous_error;2 b% c/ j4 X" l) N' t! Q. ?
- output = Kp*Err + Ki*integral + Kd*derivative;
* O" S' h- t/ ]% [ - moto(int(Speed-output),int(Speed+output));
. G0 h7 C, Y. F - previous_error = Err;
$ Z: `, T6 ?1 y/ g: H4 X$ c" f& D - }
8 p8 A& H( Q' l d X' ^ - }while(!((lightsensor_2.read()) < (10)));
3 h, R% i" A: d( t8 s3 N! Y - moto(0,0);, v! l+ w+ W+ p5 G! B n/ C+ Q
- delay(500);
7 a* j2 E5 C; B8 o4 G - buzzer.tone(262, 500);! q0 A* r+ e/ n5 {$ e I7 l. D
- }1 z, V5 l8 d/ b, D
- $ s) V0 P, ^' q! V4 J
- int getErr()
& |; r# ^3 ?6 ? l) t% c - { 4 ^& Y0 t6 z$ F) M8 e
- sData = linefollower.getValue();
1 B) h) F# M3 N2 I9 h - D1 = ~(sData>>0)&1;& C: N0 R5 h# ~7 C" [; b7 N5 A
- D2 = ~(sData>>1)&1;
9 @ ]6 P; L( T% I! [# V" b9 j - D3 = ~(sData>>2)&1;
$ U5 ?1 b! O( m @( R - D4 = ~(sData>>3)&1;/ a4 I% y5 Y, e7 p! n" q- H; E
- D5 = ~(sData>>4)&1;9 R" r1 Y3 ^# a. ^
- D6 = ~(sData>>5)&1;/ e. k) F6 ^9 D5 b0 X* @3 d5 N5 E
- int downD = D1+D2+D3+D4+D5+D6;
( v4 g8 W% U9 b# s1 ?1 p# z - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
8 s2 M. Q+ l/ R3 P$ @ - if(downD == 0): l, l7 R, s8 v$ W3 t" N" t
- {
1 H3 F; p2 t; M5 k - return 99;! u6 f* w9 Q9 w
- }6 f0 ^+ {6 c+ t5 \# Z
- else
9 h3 P9 c5 g2 C% o3 X+ a% Q - {. i2 s4 _- R* K- f; T8 s& j4 w+ X
- return upD/downD;
( P" n& K2 J) o - }
* C( d a2 l2 z. c& u) `* ` - }
5 l- [. {- j( ?1 ~6 J - 2 }. [! X; c* D+ }4 T
- void moto(int Speed_L,int Speed_R)! W4 h9 F* |# H! m X
- {
+ E7 P9 F4 E& O( ], j1 h0 b' Q4 Q( b - Encoder_2.setMotorPwm(Speed_L);
( [6 ~! y' A4 ?- K, ]( v- Y - Encoder_1.setMotorPwm(-Speed_R);( L' g9 T4 W: r. j/ Y
- }
複製代碼
7 v' J/ J8 V! F, J, VMeLineFollowerArray.cpp% o) o7 A5 [9 }$ C. s P
- #include "MeLineFollowerArray.h"
" V6 e; C( A/ Z% G: S0 J
! p: e9 w. x" n4 y$ ~ z; W3 g: Y/ A- #ifdef ME_PORT_DEFINED
8 X* Q0 E, D) V+ q2 {/ v& ^ - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
5 M& v p) h) O - {) D3 E6 X: m" Y o4 _: C( w
- 9 e2 ~2 F: A2 R7 x- y$ a
- }
. m- r% J" {: _1 y' t4 M- o - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)) J8 u& P& i: \6 @5 z
- {
* l# L" s3 N8 b h: x F - _DataPin = mePort[port].s2;
9 F# F8 ? s, B% E$ w. C - pinMode(_DataPin, OUTPUT);
- d6 W: x+ M" X+ A7 J9 h( e6 S( X! u) i - digitalWrite(_DataPin, HIGH);
+ L. Y9 u L0 Q( m8 \( f - }; }: R0 c8 Y7 X# q# I! a
- #else // ME_PORT_DEFINED2 L) o+ M; I3 u- u. e7 ^
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)( Z, B- p) |; o0 A8 u" r
- {+ g! D# r4 t. O) X
- _DataPin = pin;
/ P/ q% o1 M; N% \# D7 p9 F! Y - pinMode(_DataPin, OUTPUT); Z R7 }! e6 v. T5 G5 f4 d
- digitalWrite(_DataPin, HIGH);4 X- h! o3 b& x6 s- C- w s. g
- }
* G9 B. z; U7 w7 S - #endif // ME_PORT_DEFINED* a1 z. q4 `- Z
2 N) B2 _8 ~' {' H4 k+ b, u2 @- P- * ]; y8 ~: y/ ^, a$ X/ H3 m
- void MeLineFollowerArray::setpin(uint8_t pin)4 Z' D2 ]7 ?1 O/ q
- {; h% T' A! M3 p" u e+ E6 Y+ [* P
- _DataPin = pin;
6 I6 l: F$ f: I/ ~ - pinMode(_DataPin, OUTPUT);
% w2 z6 l7 q% y- X - digitalWrite(_DataPin, HIGH);
, J. B2 o. G) G, R% [3 P - 3 v. u3 M8 ~( O4 P- f5 l
- #ifdef ME_PORT_DEFINED- z* Z& R7 r5 o* I6 n
- s2 = pin;) _0 i- S' A+ ~) g$ }
- #endif2 l9 t4 e: D. Q9 m
- }
. w% I" L* b! h. l1 t7 U) O7 n: s3 U - ; A. }% M& X2 H
- uint8_t MeLineFollowerArray::getValue()
; D/ {) J8 G8 b/ G* b - {( Y5 |6 X1 T: U2 l: d: j
- uint32_t LOW_level_read_time;( F# i7 T; i: I; s* u
- uint32_t HIGH_level_read_time;2 x7 }# _1 d0 W6 s+ ^
- uint32_t time_out_flag;
$ o S: x# \4 Z" P. ?9 u+ A - uint8_t Sensor_Data[3];, [; m! E7 [3 `+ g) u8 u
- static uint8_t old_data = 0xff;9 a: E# S4 d+ G4 q* p3 P# `
' s2 I/ p" D4 W9 }+ V- pinMode(_DataPin, OUTPUT);
# P$ J) e: n# `9 I0 I - digitalWrite(_DataPin, LOW);
6 P; t1 w: o& g - delayMicroseconds(980);
^3 a1 g; m& m - digitalWrite(_DataPin, HIGH);
3 A( U) H8 \4 S/ E8 P+ J* m - ; S# e( S$ v: D( ]+ w
- pinMode(_DataPin, INPUT_PULLUP);
! s& s) P, E5 _) Z: N - delayMicroseconds(10);
/ O" d5 G2 S$ T1 p ?# F8 Z' m! k
7 X8 s2 j5 d, p/ B/ x: o- time_out_flag = millis();
, D3 A$ |4 T: |& q - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );$ p% L/ I. {: P9 w) v
- ; D% L' R0 O" N
- LOW_level_read_time = micros();
, }! K0 ?) L I5 w - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out2 g3 w5 w' l9 h& f! e" Q
- {
; d- v# c7 K h% ]; t; I, [! e8 _" t - return 0xff;+ M4 y1 N: @0 Z: a0 V" D2 Y: h0 U
- }
$ n2 u4 E* X$ B; j! Z2 A. U) h - % c1 E3 M# q3 R Z* `2 l
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ L1 s2 O& s+ X7 {7 m
1 b1 U/ T, t9 Z8 j# v- HIGH_level_read_time = micros();- k( @( h- y; y/ F" c) w( a' c
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
; j7 I% Y( s$ Q/ `* h5 ]5 J$ o
) q) v2 |! t; w) O2 C# `' V5 x- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
( }. _$ @% b; X" l. k - {
. b6 p& m3 m, a C9 C" t0 ] - return 0xff;; A! ?, M7 T; p
- }
& D2 c- p+ S9 L& P0 ^ - 0 z" Q8 d# h; d! c5 ?5 p9 a
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
* l! f5 T! O$ O z% o - {6 A& y3 ~6 P- {
- return 0xff;
! _: Z/ l" O) A; _ - }
1 d) D' O! U. v% d4 ^
4 |+ l, r8 s' L; Q0 E2 z2 p8 ~- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
9 c% |$ H F( [% |/ D3 K - LOW_level_read_time = micros();
* X! m' k- O) W - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
a ]7 _3 ^; ^# q* L
+ l8 b! c" M# e) S$ t, \8 ~- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out. @0 g( i8 P7 x& v& V( w
- {* i# W- ]$ z; c' B: f1 t
- return 0xff;+ @0 I# {% \; H
- }
& d+ X8 ^. x( k% A, U, m4 w! v$ _
3 ~5 @/ h2 O. T# w& x- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))8 N9 N3 Z0 v% M- k2 O2 R3 F
- {; n a8 Y. O4 J/ s, d$ j* F" ?
- return 0xff;
' N0 o+ b! L, a6 a( e - }8 a6 `4 F1 z3 ^ S* v3 h7 J5 D
) P! I5 f: j. P) `8 I- for(uint8_t k=0; k<3; k++)& S! {4 S0 r# I- H4 Z& D
- {
! w$ C# e5 V- ]2 [6 X4 J - Sensor_Data[k] = 0x00;
+ _, T0 A& Y" i8 `: d8 g2 S - 8 n! u# e: W+ \/ }" g$ _9 J
- for(uint8_t i=0;i<8;i++)3 O' G- E! j) u. j9 b& Q
- {$ P4 s8 s% b' N4 k( x
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
, J \; z2 J' u" ~ - HIGH_level_read_time = micros();
0 n& _% \0 G: `/ o2 t, E3 c - LOW_level_read_time = micros() - LOW_level_read_time;
) @, I* l4 b9 ^* | - / D& ]+ H6 [& c* \2 h, X. c/ u
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )3 Z+ I9 R, A4 [. ^. g
- {0 D: m' \4 U9 B
- return 0xff;
: K1 s1 X& Y ~( j+ G - }
4 L: G7 ^# ?3 Q' I - " O6 {( B8 A* P6 ?& w1 q
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );8 P0 }4 A! a( v+ g l' P
- LOW_level_read_time = micros();( h/ ]/ R1 n. [2 F
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level W% y! Q8 g# Q8 D1 k" U
( t) \' A- A# |- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
1 M: N7 v+ D/ N6 }2 H h - {
) F0 I6 j+ B/ w3 p7 J/ C$ r - Sensor_Data[k] |= (0x80 >> i);
' {3 q" B# p/ C3 K0 w% e& f0 u& G - }( r: j Y; V4 Y/ P5 Q
- else if(HIGH_level_read_time >= 100): C( E7 Q( O0 B- d2 d- b
- {
# S- b9 {7 J. C* v! h$ X! D - return 0xff;
% M% N! ?& ?: q8 h# e8 E& W - }, a) g& Q- J' z6 ]
- . V' r8 \. ~0 P$ E% X5 I% \6 G
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
/ ~$ V; Z( C% `1 E: e - {
0 q k0 }$ i; b. }" X! X, w5 t - return 0xff;( o4 E* @; Y/ ~( I
- }
6 p) B1 X9 a) W3 Q B) \0 i4 E - }
$ \; k! o5 }0 @, \8 U2 m - }
( x8 O7 U( L v7 B6 n6 k3 d
" x! X `. W, u' g- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level% G2 t4 p O! h% E- ~: [- i, Q. h
- HIGH_level_read_time = micros();/ e3 K; [" ~4 o \2 I
- LOW_level_read_time = micros() - LOW_level_read_time;
+ x7 k, T0 |$ J, I
' X) [ q- T+ v, u8 s) V$ q' K E2 U- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ O) D: W, o$ h. n: G' h6 ~" v8 B
- {
3 {+ R% P4 Q+ H - return 0xff;
# M @) _, H3 C2 H% P& `3 Q - }
- S- B) b6 D* H7 m5 u- _ - " y! X0 h9 m% z" m' a
- pinMode(_DataPin, OUTPUT);
% d; P2 |5 ?+ Q% c2 D. u) E0 Q$ _ - digitalWrite(_DataPin, HIGH);( r' @+ J+ O0 r0 o' d+ I
- 9 s$ @, {/ N8 D& e2 h# u
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))& O* H3 l, q; W
- {1 z* W$ d6 f2 X* [- O2 S
- old_data = Sensor_Data[0];
8 w0 g0 A1 z2 W$ I3 W - return Sensor_Data[0];/ t: s2 W# p) A( C
- }1 O' }5 J8 r. R0 E K0 K
- else
0 G( K9 T/ _. F! U - {
' A2 j3 b9 d9 [6 K - return old_data;
' O) d9 ~8 U( B) W( D* [. I5 t - }9 ]" e# q* I6 N( Y) n
- }
! } v8 D1 m* ^* [" t
複製代碼
2 @5 G; A8 }1 K- z* x& eMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
* _* w2 l' }4 R# t- a
/ d1 x2 z: C7 h. ~/ V5 } |
|