|
|
1 ^; n& u6 t! x+ M! t& U; y' H% c- #include <Arduino.h>
* K% |( }7 s! _% n7 \ - #include <MeAuriga.h># V( ]0 j0 V- d, `! D
- #include "MeLineFollowerArray.h"0 l" @- o8 x' _ Q' E2 D
# Q) e+ `$ Q: w q J- MeEncoderOnBoard Encoder_1(SLOT1);+ u: m. I% x1 M
- MeEncoderOnBoard Encoder_2(SLOT2);
' x2 m- } h, G7 V% o9 d3 {* O) d - MeLightSensor lightsensor_1(12);* z9 B( U1 }9 y' h2 Z5 V: t
- MeLightSensor lightsensor_2(11);3 E$ _2 W& D+ Q4 n. _8 V
- MeBuzzer buzzer;
' T/ o/ W: l; x* v! S8 r3 \ - MeLineFollowerArray linefollower(PORT_6);9 }% e0 a6 T9 h3 {$ S0 X
- & ~. }0 E; B1 B: C& b+ n
- #define Error1 1
|# U' w ~& N o e - #define Error2 2& Q" |; R" n; X. p
- #define Error3 38 D% z$ j4 U4 O7 f
- : N8 v& P& V' }& b) `) S
- #define Kp 15
: @% h. n/ B! i# ] - #define Ki 0.15
# d9 H) r% Q$ q( c1 \3 e0 I - #define Kd 0.03
0 a1 q7 `6 r$ t5 } u" I - ) l6 s* w& I* S
- uint8_t sData;1 p* {' |( t$ `' }+ \5 Y- {! D. f
- uint8_t D1;
! J. J5 n7 @4 e, U( x - uint8_t D2;. D2 q5 D$ u% k* x
- uint8_t D3;
. r5 u" }2 V8 K2 F - uint8_t D4;
! ~* r: V+ Q G! ~1 w+ o - uint8_t D5;2 x% G& P4 k( y# T
- uint8_t D6;
# D0 Q; K# C& Y, z+ o- q f - 6 C( T. \0 |) T, z5 T# Y7 X
- float previous_error = 0;
$ G1 F5 z* b( w7 h% {" p4 K3 r - float integral = 0;
' c/ z$ m _" x% m* N! Z - float derivative = 0;
- ^& Q, m( `, Z# E% @; P - int Speed = 160;
1 O* }8 P, j+ c- g, @* N; K; V - float output;, R" J4 p' c$ s, l
, R8 N5 A$ Z% X6 N" L- byte Left;6 F4 [$ \5 |2 f1 ?+ e
- # k) F/ T, g6 r" ?0 M( a* R
- void setup() / B. D/ l5 F+ o) W( X- w
- {
+ h& t" ~+ N- W, w - //Set PWM 8KHz
# H' W) }5 U8 Y7 _8 Y' ~/ M2 h - TCCR1A = _BV(WGM10);" J3 q7 l" D5 `) ~0 [+ l
- TCCR1B = _BV(CS11) | _BV(WGM12);
" Y; U, B/ S# O; @* E - TCCR2A = _BV(WGM21) | _BV(WGM20);9 O' l, x* b$ S0 a( u+ C f
- TCCR2B = _BV(CS21);
) g( S, H' y& z4 Y - Serial.begin(9600);& |% k8 ^: s# L2 g8 L! n
- buzzer.setpin(45);* v5 O- ]) E3 m
- }1 C# _4 ?) M0 F, o( C
- ; z7 V& d2 Z3 g/ K5 Q
- void loop()
. ], a. ~8 ^; { - {
! H" G* W' |- I, g3 i - while(!((lightsensor_1.read()) < (10)));
6 M1 Y- g% W: a, P - buzzer.tone(1047, 500);
- ]6 T0 |& L; k; `) y$ F - delay(500);9 d! I6 A0 `8 W9 F( R' _
- do
9 ?0 p3 X, i+ T - {
9 t T9 P5 u: D3 o: l! `$ e - int Err = getErr();
% [) ^5 s3 \6 K- E; ? - if(D1 == 1)
0 m) a3 E% R7 u2 ^4 D - {
$ M/ @ e/ o( O/ ^ - Left = 1;
* g5 }0 M" E$ z' }, Z1 c _' r/ a - }
/ E$ L- o6 V7 G; h2 b - if(D6 == 1)9 e7 c( F2 k$ Y4 [1 P8 p, o w" F1 n: e
- {& [5 k. q5 |5 u8 w4 W$ L3 t8 {
- Left = 0;, l; F( A8 @8 U9 x) w3 J( w
- }
+ y7 g. T5 E8 H9 E - if(Err == 99)
% a; V, e) t6 C - {3 J- o3 _2 k8 \8 |
- if(Left == 1)/ @" ^# M% V1 U4 y& m) G6 c& @
- {* ]# u: U$ K4 j, G+ d
- Speed -= 5;+ R/ v* x: h; Q
- moto(0,Speed);
" E4 t7 G6 L; _1 H ?" A H* h - do s! ?9 ^9 _0 E5 W2 J0 Y
- {
( |% T- w7 W7 y5 x* I; z - Err = getErr();
3 W% I. ^9 K! Y; @7 V; U - }while((D1+D6) == 0);
- w) U- w" y! k$ G" c t3 W - }
Y' `+ ^0 Y3 V - else
3 }8 N) U0 T, l7 ]7 h& r - {0 T- O! E% I. Q7 P1 o- x% ~
- Speed -= 5;2 L( }4 d& X' ]0 s
- moto(Speed,0); , N- e7 y" O4 z. t7 {2 @5 v- J
- do3 _9 a- A( K! G1 f) E c
- {
8 U0 ]8 f' D! |& {7 ]) r R$ v - Err = getErr();9 o' Z. N- ^# A( I
- }while((D1+D6) == 0);7 o$ F* |+ [" H7 Q8 V- P$ v
- }- u8 I7 D% W7 f) ]7 d
- }$ b5 k l: P* i
- else
8 k/ D4 o4 E# n/ e2 | - {* r$ [- T% m9 E p* r
- if((Speed < 160) & (Err < 2)) Speed+=1;
. ^: Y2 L: R+ ?% h - if((Speed > 100) & (Err > 2)) Speed-=2;
9 Y/ k. |+ ^. P$ a4 O' v - integral = integral + Err;
5 r, p; L7 t- a) w* B+ B$ n - derivative = Err - previous_error;9 Q* V, y1 F; |4 K& N' F1 r
- output = Kp*Err + Ki*integral + Kd*derivative;# J5 h1 l, y! ^
- moto(int(Speed-output),int(Speed+output));
. i0 V$ j' U* q" D- L: @ - previous_error = Err;
% D! ]7 r& f4 d2 h0 D. x - }
8 U1 d3 |7 x |* I% ?" H: n6 Z - }while(!((lightsensor_2.read()) < (10)));
$ f) O* x( F6 n7 e2 g+ ^% z - moto(0,0);
4 B: O2 L4 Y9 x9 N - delay(500);! L- Z7 e! {; `! r; L
- buzzer.tone(262, 500);9 q7 G3 A) [% l# u. \1 u
- }
6 ?* l' E0 I. r) x& V( `6 W
* g( U' W, [ x* N" |- int getErr()
k# }3 {1 N3 P% L0 D4 t - {
8 L; W1 ^! s$ O; m - sData = linefollower.getValue();
1 ]$ Y: x! p- T% S+ e) u% f7 R: x7 p - D1 = ~(sData>>0)&1;
7 e q8 `6 [: q& s - D2 = ~(sData>>1)&1;
, o/ O& d0 |9 U% V - D3 = ~(sData>>2)&1;
: S& N! Y; ]% p% C$ \5 f2 C# D# E! g s - D4 = ~(sData>>3)&1;4 l. u- w0 _9 q% d N& c
- D5 = ~(sData>>4)&1;
% Q. B" F& O8 E: F - D6 = ~(sData>>5)&1;
0 P* b, d) M; a& H - int downD = D1+D2+D3+D4+D5+D6; m$ C4 r# T8 p4 ^/ a9 V
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);" z( B+ K7 x" J( D, [# q
- if(downD == 0)
) {( I& w9 ^) ?& G0 X& Q" a8 C - {9 ^: G3 B. X( K0 P! `0 X z, T
- return 99;" k- D$ ~3 J5 o3 N
- }
6 C/ Y0 m* |( e - else% g9 q; I; e( T F# J# u$ z8 P
- {
! J o: ]3 k; B0 v2 b) O - return upD/downD;
6 Q7 Q- [ k9 b# o0 o6 y! x7 b* D3 R - }/ v2 H! x1 C8 g0 U, o$ U
- }% [" a, } d3 q# O) A
- 6 N9 R; a+ ]8 F/ n3 ~' d3 c' y
- void moto(int Speed_L,int Speed_R)
- G; t& b( I0 ~7 M - {3 V( \( d( G7 Q9 q& Z
- Encoder_2.setMotorPwm(Speed_L);
% k- i/ p* n) ^ - Encoder_1.setMotorPwm(-Speed_R);% k. l! n* F- ? F
- }
複製代碼
1 R6 Q3 \7 y; l0 MMeLineFollowerArray.cpp
9 w7 O9 A3 ?- m, g7 K3 _# h- #include "MeLineFollowerArray.h"
- d' r' W' U3 [( n' C% D1 K - ) }# S7 `6 D+ Q: c
- #ifdef ME_PORT_DEFINED( M; C6 q- @& U4 [& T- ]
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
6 z3 ]( y& f+ p9 o% Z3 H/ U- s - {4 N" e4 F2 k& W9 j- Z
8 z0 j9 j" Q2 D+ @, ^. Z- }/ X: i3 x- }6 F/ @
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
: r; d7 m6 C. b( `; `4 I - {
) ], n8 j% C! R0 h5 v( {4 a" M3 f - _DataPin = mePort[port].s2;, R. E a, V4 y2 z; c2 e& h
- pinMode(_DataPin, OUTPUT); & N$ O, T, `8 b, p
- digitalWrite(_DataPin, HIGH);
0 _5 X8 ?% `$ o& [( _0 A - }
5 ^3 T3 A: J! u' r - #else // ME_PORT_DEFINED- c ?7 C/ F$ G# {5 e
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
( _1 O) D3 x. \' p - {$ P+ [- e4 p! p& N9 B U! L' i7 Q
- _DataPin = pin;
* y: T' e% _4 w; b6 D9 R - pinMode(_DataPin, OUTPUT);
7 W. S: J: u5 w - digitalWrite(_DataPin, HIGH);
5 t: s3 c% x- |6 m - }) V; p" ]' C: g+ \" h! ]
- #endif // ME_PORT_DEFINED
0 }1 v7 e, [ S# ]0 i- h
, ~7 R' s& Y: p5 Q3 X5 }- " X% B$ S6 N. i, R
- void MeLineFollowerArray::setpin(uint8_t pin)9 F. o7 Q9 m! n7 {# x
- {! W3 j% |1 d8 i* o/ w+ j) W
- _DataPin = pin;
" e. s7 Z p w - pinMode(_DataPin, OUTPUT);
4 O9 s0 _9 m# c1 R$ X# E - digitalWrite(_DataPin, HIGH);8 k7 O8 D. ~+ t6 F( N
: J; L# r. ?9 S+ X w! S8 c$ J+ o' [- #ifdef ME_PORT_DEFINED. F5 y4 j/ t1 @" J0 c9 E$ z
- s2 = pin;
9 A) a) ?, r/ H+ ~/ V) n0 T' P4 G - #endif
5 z2 O8 @2 `$ l3 F: C8 r6 T - }
6 e% S* {; v4 G C: _7 e - + I: g/ ?* E e# p9 z& E9 ? L' R! {
- uint8_t MeLineFollowerArray::getValue()! R2 J V, O6 @2 D4 e4 ~
- {$ V; m1 _- q4 h% P( _/ {9 a
- uint32_t LOW_level_read_time;4 O3 {8 O8 l) M! l# v$ n
- uint32_t HIGH_level_read_time;
' T6 X7 a# k+ ^; S' l" t: d; k - uint32_t time_out_flag;
9 |) d, G+ z/ E9 w s - uint8_t Sensor_Data[3]; Q' d% e2 A! b0 B
- static uint8_t old_data = 0xff;
: M q( X) u5 H4 x. [
: N: N2 Q: w+ K% s: {- pinMode(_DataPin, OUTPUT);
7 W- Q) i5 g2 n5 _8 u% ? - digitalWrite(_DataPin, LOW);
3 x& H0 ?9 e' x, M' F, \: H - delayMicroseconds(980);
f3 n P( g1 U, v - digitalWrite(_DataPin, HIGH);* b; \5 @% [# P2 I }7 }% ^
' T& K9 T6 }% t; I- pinMode(_DataPin, INPUT_PULLUP);3 d; g- d$ s8 K0 o4 k6 L H
- delayMicroseconds(10);. A# B2 f7 n: n3 b3 ]$ D7 C7 w' D
- % R% |, M* H j+ {
- time_out_flag = millis();, n4 H% T# x+ g( L. a( ~. d- S
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 |" w( @9 V5 {2 r
2 ~8 V5 {' w! f" o! _' ]! H- LOW_level_read_time = micros();$ w- c7 L/ V9 I7 E8 D9 K4 e
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ o0 s# p3 Z3 @, \ - {4 Y' f0 o+ _9 m" V
- return 0xff;& W+ Q& _) w. `# o, n3 t
- }0 J {( R0 M+ N, ^& O5 P/ Y: P7 g
- $ f. ^: F$ D7 R6 ?! h# _
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );& ~, Q3 w+ p' n F; {/ z, ?
- * w T- M5 Z; _ r) ~' Z
- HIGH_level_read_time = micros();
9 i6 F+ p7 g! L. `; ~$ ] - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level- B/ f8 v3 T2 b) k& B2 U5 \9 p% z
- $ S7 B V5 k, P7 J+ Z0 ?
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out" Q9 ?7 Q9 x; l1 E3 W% b
- {
5 s) Z- @+ G: i6 P- L. Y - return 0xff;
; z/ E+ v1 G. y& Q/ t) q# T - }
$ {* ^, P4 z* [3 i* o
5 S' f7 A# i: E! ^- G4 B ]- [- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
$ M9 `, i V0 E: f c - {
9 [1 }9 M2 N9 a. l+ ` - return 0xff;' G, [* u9 `/ ?6 L1 P. _1 y% L
- }
8 W( W0 {8 z5 ]& c- q
; O) \0 g. x: S' l3 L- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
3 e3 b* y. H' |7 S- v1 R( t - LOW_level_read_time = micros();
! i% f% ?7 ~% t. ? p# p - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
9 Z# Z; Q! m2 L) ^1 B$ F5 Y
y5 v; p! B3 }5 J- w7 J0 w! h5 F- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out/ d( v, f6 R* c0 I
- {
. _8 P( `. P" V# z' m - return 0xff;
9 x2 `4 `- f6 j0 ?1 L; |8 A, S - }! X6 I4 G5 c8 U: ?. Z+ g
6 I3 v4 K6 x" M- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
+ t' f0 X/ P" {9 l# D7 w( I7 n - {
3 n% w2 K6 ]+ r$ Z - return 0xff;+ h. j4 l; d7 D
- }$ G4 d/ e2 X, `7 g7 R
, S% |& o/ M2 ?% U- for(uint8_t k=0; k<3; k++)) U9 [/ Y2 W5 g" j: U% A/ Y
- {
9 R; R' e7 p' i$ y* ^ - Sensor_Data[k] = 0x00;* M: ` L$ o6 c1 s# |
4 h, t. {8 O. m* G+ h9 O- for(uint8_t i=0;i<8;i++)
- W4 [4 V" u6 P1 Q/ ] - {
: G' T9 ]9 b' B( @. V - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
, a# T H4 N) Z- v/ N0 D - HIGH_level_read_time = micros();8 f9 |+ W! Z9 f) b7 k/ @) `
- LOW_level_read_time = micros() - LOW_level_read_time;: m$ Z' |( Z0 b6 Q F! I* ^% e
- $ x, F# c$ E t( Q6 @- ]* \! B9 S
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
& p& \( E" T! _: k - {) Q8 \% {9 a: R
- return 0xff;! P6 ~2 b4 o% {6 u! f5 t
- }% D3 r: \: H; p: X
& O/ ~, N+ a9 H: s1 y- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
9 G" G. l- O5 ]$ W - LOW_level_read_time = micros();
/ b3 }$ ^2 N& P - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
. }: g/ i/ H/ H0 k* a( y
4 n9 |4 t2 X6 Y9 y- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
% v% c' {; p! y d$ |8 v7 K( i - {
; O& ?- Q5 M! ^2 V1 w; l - Sensor_Data[k] |= (0x80 >> i);" K7 W0 L# H& n! Z& ]" h
- }
4 o# h4 n. E) f! M+ C$ ~8 @, g - else if(HIGH_level_read_time >= 100)! P8 [& u1 ]5 w) J
- {
9 L: {, ]! f9 s! j, t' o1 d - return 0xff;
8 h- H! ~+ ^+ |8 S+ e" c H3 s2 y - }9 Q- ^+ B8 [5 s R1 w
3 J# ~0 E" m+ `* u- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)9 n- j2 q8 m' W( a% ]) v4 F
- {9 s" g" E% g- b2 M1 g# L
- return 0xff;7 M2 p8 V1 Y& \) g' l7 B1 t- h
- }
9 n3 q0 T& \) |/ T - }' f! {! A# i# N# [3 }* [
- }
3 S, E3 k3 J3 a# |! t
8 p% ^- ?* s9 F* I( ~- i, Z- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level* ~1 b# _6 h* p# w
- HIGH_level_read_time = micros();$ P' `- u c7 \
- LOW_level_read_time = micros() - LOW_level_read_time;
& d, I% V2 g. v1 U- N f! P# V: g - 4 w- O( X) Q# w* v1 P) u
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ H# {' M/ f* K8 u2 T5 y+ t
- {# f7 B' {5 K) l2 [1 ?# b
- return 0xff;9 F( j) ^, x7 j* h: b4 ~
- }6 }, u4 B) s" ]8 ]: u/ \
- : Q L7 K2 |+ J+ t5 Y
- pinMode(_DataPin, OUTPUT);5 j1 O) `+ L+ Z( Z+ x# p# b7 x
- digitalWrite(_DataPin, HIGH);
$ o) `- }% k5 M2 |9 m- n7 r( q
1 \! e: H# h( j* y5 w7 q- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0]))) e0 h' C1 Q- j2 z | L% n' |& Q# r
- {1 n# f% n1 C; b
- old_data = Sensor_Data[0];" t9 a# Q# W/ q+ D
- return Sensor_Data[0];/ S8 }7 c2 l" s
- }
! {4 h8 O7 x& ^- R) C; ` - else3 q# E( Q, p0 L S/ z
- {
4 ~7 ?1 k9 t7 r& k - return old_data;
" [ Q, e6 V) T - }% b! r( U5 f5 ^- Z
- }
9 J. B" Q- D+ z7 o. s8 v
複製代碼
. b( ~% A5 T' I# c B) KMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
4 J6 a' j/ q1 V! B+ D$ G
% o$ K$ r: s @/ u* a2 n, Y |
|