|
|
. o1 B$ V2 b. e) u" B, B- #include <Arduino.h>
3 m5 x0 G* \9 G) E* l - #include <MeAuriga.h>
& _* r$ ^8 u& r9 Y! L0 c - #include "MeLineFollowerArray.h"4 x+ |& [" L5 Y
- 7 V# R6 |+ J$ ^- H- [
- MeEncoderOnBoard Encoder_1(SLOT1);
/ R% V c+ u3 ?2 t7 v/ M - MeEncoderOnBoard Encoder_2(SLOT2);! \1 k1 {! [: M" n2 P" R/ a
- MeLightSensor lightsensor_1(12);# g! T! r4 l3 d6 @
- MeLightSensor lightsensor_2(11);( @3 [) @! @' @
- MeBuzzer buzzer;
) v" K- A7 S4 s - MeLineFollowerArray linefollower(PORT_6);
0 Z' _; K; ]: a# A7 v$ L - ' Z7 u0 s+ k( E4 y* n/ m& X) k) n
- #define Error1 1' r6 U' \8 B0 K' |, z& \2 j4 n! w
- #define Error2 2) q; f. R. Y6 U6 g5 `7 n
- #define Error3 3- w4 x8 n! a" ^' o% G
- 3 b: t) ?9 l3 F, e' M* o( w; @3 g
- #define Kp 15% n5 ^; a% Y) x& ~; B! w
- #define Ki 0.159 p# p" |1 t: R; S& @0 h3 s
- #define Kd 0.03) {: v6 R# e, T3 b4 Z2 P" x
- + V' \7 ^9 m/ g7 o( e
- uint8_t sData;
" k9 h9 @" v# H4 @0 Y - uint8_t D1;) k3 Y4 m0 M' a' G
- uint8_t D2;# \7 n2 i/ D: [$ H) Q- y4 s7 f
- uint8_t D3;* @! n1 U( F; i1 e, X
- uint8_t D4;7 [( y/ j ~. D. i4 ^7 X
- uint8_t D5;8 { |2 c% Y# H; }) _
- uint8_t D6;
. F+ i& D2 b3 G- c/ `% O: k' |
3 L& v3 `3 X" o- {7 \5 B- float previous_error = 0;
. R3 ^! H, D7 g1 y2 W( G2 R1 E2 ^ - float integral = 0;
0 _) ^1 d4 ]: `, _3 X9 H - float derivative = 0;- l: \# P, Q' S
- int Speed = 160;
$ v( q) R# u6 T3 D: ]4 z0 ~ - float output;4 W5 j, z4 \+ `$ m2 T
7 ~& }" w( ? w: z" h( i- byte Left;- Z7 m4 B' D% Y; `4 n. w" t/ U
- " a7 m4 f3 X6 Y! D/ {$ s" x; }
- void setup() 9 \, r7 I8 u2 F8 x! `( L! L
- {
6 ]) z* M v0 Z7 x - //Set PWM 8KHz+ O' |2 s: _) }9 x- e1 q8 i' z
- TCCR1A = _BV(WGM10);
9 [4 [' m6 r$ L, c/ n$ `; y - TCCR1B = _BV(CS11) | _BV(WGM12);
% j% J; Y; Z/ Y - TCCR2A = _BV(WGM21) | _BV(WGM20);( ~( q8 @4 g& {2 {8 |
- TCCR2B = _BV(CS21);5 j( `+ c6 |! ?
- Serial.begin(9600);
. p2 Y; R( B* n( m - buzzer.setpin(45);
" J2 z2 e1 S$ {5 y - }
" ^) u! H# f) H- a
% ~6 Y8 p" r, }4 ^: I- void loop()+ l" ?+ q8 K/ \6 V( N9 v: b2 f# F- V
- {
8 T9 y d8 @; e - while(!((lightsensor_1.read()) < (10)));0 ~3 A1 k: S! k# ?% D3 p
- buzzer.tone(1047, 500);
; ~ O7 u6 X8 z5 u - delay(500);
3 T9 {* L. z5 Q/ v - do4 x$ p0 I7 L: t& b
- {' Z6 X: D7 j" m: L2 y$ P7 C
- int Err = getErr();8 ^/ c) }! N# F: S5 l# I# w
- if(D1 == 1) h- u2 i6 t0 r& m; Z
- {, ?9 r+ z: R5 p. z4 H) X
- Left = 1;4 p) @/ ^! c2 r: k9 f& H& T
- }
: Z" t7 s+ K$ ]5 ^, a K - if(D6 == 1)3 A' I# t6 T& f) t& x7 M
- {2 p$ a+ r* V8 \" H
- Left = 0;4 c+ [& L; D6 D9 c. } \
- }6 r& n$ B( d; B7 \- Y+ ?6 n
- if(Err == 99)$ o3 a1 O/ i y8 @: u
- {
( _+ M/ A% Z" t- t w. R - if(Left == 1)- @: |( I. B S) {+ u
- {
' O# D1 M* g9 G' G! ]9 X; a$ ? - Speed -= 5;9 {" ~4 A: Q% n' r: B. S3 L9 q
- moto(0,Speed);
8 b7 A; v" q O m: R' U+ v - do) ?# |. H9 K" J; L. |* K b" C
- {
' |$ h, @7 S( }% x - Err = getErr();2 ], N; V$ k; Q" J2 d+ D' E: E9 X& L+ L
- }while((D1+D6) == 0);
) A X6 E& Z$ p; R& f; ?8 W4 t - }# I0 h& ~, f3 X. \# W+ l
- else
% z: ~/ e" l0 f* I; ~' V5 ` - {
* q2 Z4 N, q! p9 D% R - Speed -= 5;
' m: z, e. i& }. j$ g. T- Y" M, s - moto(Speed,0);
2 ]+ L9 X2 V L, H* {: E! M6 a/ q7 e - do
$ g) \- d. A6 P. Y6 f - {
. O. P; u* j8 s9 |3 M% | ] - Err = getErr();! P3 C: ^* x0 U8 h) `( i6 c
- }while((D1+D6) == 0);
! @* @: @. x' l - }) R6 _5 C" z. {" I, G$ _, Z. h2 N
- }$ L& u$ p0 o+ a. N- M4 n$ O
- else
* d: }: L ^* [ - {! m. i6 `: s" I: N' P' _
- if((Speed < 160) & (Err < 2)) Speed+=1;
& b* x' h2 h, U0 c; D' \6 \ e& a - if((Speed > 100) & (Err > 2)) Speed-=2; 6 Q0 I0 V9 C8 B( C5 F
- integral = integral + Err;, ?& n$ z- T: {
- derivative = Err - previous_error;
. s! {# Q$ T, `; I! v - output = Kp*Err + Ki*integral + Kd*derivative;- L% t3 q! L2 y9 N- A8 B
- moto(int(Speed-output),int(Speed+output));) `& Y0 L' {2 i t
- previous_error = Err;
3 y# F8 `; ]- `0 }1 k - }* r: {" l p5 M1 u1 u6 z
- }while(!((lightsensor_2.read()) < (10)));
+ |* o( C$ `2 ^* R, h - moto(0,0);/ R9 X. `' [- m3 P- S0 n1 t- s* H. G
- delay(500);
8 k) E" H# F- @3 @ - buzzer.tone(262, 500);
9 O. Z$ ]# T7 g: G$ ^ - }3 @$ H' i* x! o+ K
5 D% m- Z$ T) g7 u( p( x) H, M! C1 K/ H- int getErr()
0 g4 A5 Z. J* \9 j - { 9 k1 g) B, |# y8 k5 y2 ]
- sData = linefollower.getValue();" e! m" S$ n+ T2 `8 U- n' V
- D1 = ~(sData>>0)&1;+ g: P b7 G2 Y" d0 E
- D2 = ~(sData>>1)&1;
9 l! _1 ]: b5 p3 a+ l9 l - D3 = ~(sData>>2)&1;
( j' a0 l6 u4 ?$ S8 ~ - D4 = ~(sData>>3)&1;- Z% B' |) T* U* @( v2 [/ S
- D5 = ~(sData>>4)&1;3 h8 q- h$ S7 Y+ T; P. s. [7 g" b
- D6 = ~(sData>>5)&1;4 t' N5 T4 M/ Z1 n6 r, S
- int downD = D1+D2+D3+D4+D5+D6;
# X0 Y$ U$ u1 T a# H - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3); }/ D) ?1 J2 } j H/ M% L4 U
- if(downD == 0)
) m6 e0 f0 {( c& } - {
; p6 G2 F/ D7 c1 U" P - return 99;+ J: H6 O) _- A3 r" I8 x
- }3 O6 ?. R; h( C5 e
- else
( Z' N4 M: I$ Q' V/ R) N - {
8 Y% i- z% ?. y' x% U4 F - return upD/downD;
2 g' Z" u' X: P" S/ I: r - }
4 G9 e4 {6 s- u& N6 ?+ I) V9 S - }
/ B7 a+ v3 |3 @$ j - 1 O+ y4 N0 s5 ^) X
- void moto(int Speed_L,int Speed_R)
5 w9 c, z/ B" d6 k# s( j' ~0 i - {
* s5 S# B$ s4 P' Z/ A3 z - Encoder_2.setMotorPwm(Speed_L);0 y* r% N9 y/ r) T/ p4 K J1 |" m
- Encoder_1.setMotorPwm(-Speed_R);) q1 d6 f+ B8 s8 i
- }
複製代碼
9 w: `( i1 O0 V( PMeLineFollowerArray.cpp, I% e5 V2 ] i
- #include "MeLineFollowerArray.h"
6 z; i a1 \; O
9 T0 D+ b# |% u9 r+ m- #ifdef ME_PORT_DEFINED: }9 B U9 u C9 t" ]
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0); j" ~, N) m; Z# W
- {8 {, H. |' b) A* F$ J9 g
( j* U2 T y4 A- `- C! n6 {- }
0 o9 m# M" [# q2 f! a - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)9 I) O" _* v9 k/ _; N6 R7 I1 c# y
- {3 O% h1 M' Q/ e5 ~, w9 R
- _DataPin = mePort[port].s2;8 W/ ], z+ ^! ]% g8 W2 n& ~
- pinMode(_DataPin, OUTPUT); 9 | M8 r/ c7 D: P7 e
- digitalWrite(_DataPin, HIGH);1 i" v6 o( H5 i$ l' U+ [4 o
- }
2 ^9 l8 z! R* \5 g L - #else // ME_PORT_DEFINED8 z! a! H) p' n" r5 l
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin); ? ]5 c) d& S6 |- i0 m
- {1 D4 J( U6 U7 C4 w4 s
- _DataPin = pin;
7 u. z. g4 M' L# a! J - pinMode(_DataPin, OUTPUT);
! r+ @' ~( A; Z5 b& X - digitalWrite(_DataPin, HIGH);
0 F. F+ O* H; r1 S0 A - }$ \% A9 Y8 l+ h4 u+ q, w6 X- `& U
- #endif // ME_PORT_DEFINED/ R3 V; B! a# n6 ? z# N
- G5 T" F H4 q" W
( b! j# \, r5 `: L- void MeLineFollowerArray::setpin(uint8_t pin)+ l& R7 W& Q8 _3 ?
- {
0 `7 H$ v; r2 _* I6 U - _DataPin = pin;
G4 n$ q2 t0 F( z. t - pinMode(_DataPin, OUTPUT); ! S& y) ?1 M O- c
- digitalWrite(_DataPin, HIGH);2 X' @2 e, `$ K) z% B7 q: g2 r& A
6 Y0 p2 [" [. ]/ J3 z- #ifdef ME_PORT_DEFINED! Y8 C' w/ ~: K: p
- s2 = pin;' L+ a" K4 z( _ U5 M( n
- #endif
7 b% B3 r9 t* j& W+ K& [5 W" \ - }: E) A' N& B5 V0 f' a4 i
- ) n b9 r* I& x9 Z2 e1 R7 M
- uint8_t MeLineFollowerArray::getValue()
5 j: v" u7 O0 T, D" m; q - {* f: O! h8 ]$ o, o# M
- uint32_t LOW_level_read_time;
) N& s8 ]# k* r - uint32_t HIGH_level_read_time;
8 x& c" o/ d* j; a7 L# x+ B - uint32_t time_out_flag;' w; B; g9 [! |- h3 I) h O1 p
- uint8_t Sensor_Data[3];6 X) `* v# ?8 b1 m- l0 z, v
- static uint8_t old_data = 0xff;! H9 r0 c( B7 f' v. b
# C, r8 F1 Q- W+ x- pinMode(_DataPin, OUTPUT);9 o' f+ b9 d4 \* i0 K; n+ J' T
- digitalWrite(_DataPin, LOW);
& J+ z Y( O$ o% W" F. Z$ } X - delayMicroseconds(980);
7 e7 M" t$ d1 R. t - digitalWrite(_DataPin, HIGH);. W; g, J4 a0 I E
- 7 o7 J! a& Z- O) I( G
- pinMode(_DataPin, INPUT_PULLUP);
! S- i5 P" E4 v: Y - delayMicroseconds(10);
$ o! ~& g7 _/ Q) P+ x - 6 T& A7 A% G' P" p
- time_out_flag = millis();% {% y I: ^$ L; Z7 z7 S1 T
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( E0 p! D% Z4 J' C, b( g
. K1 u) Z, F) [5 T, o- LOW_level_read_time = micros();6 b0 Z! J9 B) Y5 ?4 E( G8 ^( p9 n
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out0 {! i0 s# [' T+ M6 {! u
- {
& C0 L0 c/ h) D- ~" Q5 k1 V& S' Z - return 0xff;" K' p' k9 z: T% T8 {
- }* n v5 D1 U8 T4 }8 R8 g1 K" m7 \) ~
- 1 D( O n2 U) c; U
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
! Z1 A5 P6 O* J6 z - * \ A F) a0 _- _5 [% Y
- HIGH_level_read_time = micros();$ o9 |6 k+ Y5 i( g7 k2 ^0 Y( W
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level1 j* F/ ^$ m# j2 ?( }4 `% [
- 2 q1 u2 g; y" V5 ~0 ]# c
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
' m2 D& q% V1 K9 f3 p - {
( B7 v$ y4 j _3 I! W - return 0xff;
0 b3 l. G/ H% B& p - }
! \& |" J3 l; h; v
( u: l; C& s5 p- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
0 v3 S# K( Q+ |% b: m+ d; S9 r - {
$ w- [+ w) q6 W - return 0xff;
8 p2 r3 ?1 i# G. E/ k - }
% n- \/ g' m$ B; Q, i$ }# N) N - $ W/ g% ]: h0 f4 [* \ |0 p
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );* {0 ]7 D( I4 I* r" O: p
- LOW_level_read_time = micros();
% B$ V5 |# \, B1 M( M1 t. @ - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level$ T- R! W! L1 c+ F
- % e) E0 Q' q$ n3 M% e
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out t) t) S2 o3 u8 m
- {8 b8 S, x4 c& T7 C( |
- return 0xff;
* C2 t; u( j; f: m8 u - }
9 l% y/ ]' k* ?8 v2 ^+ c
; x; }$ r8 |- V' y* ? M- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))0 {; f) v- \ ]1 k- n0 E/ V
- {1 b2 w# z$ P1 n5 y& L1 C; t
- return 0xff;
( d; l) j, D* d* t6 q' m - }6 }) I2 {) V1 A- D8 ]8 W
/ X2 ]$ |# f1 K" S. C5 n9 `- for(uint8_t k=0; k<3; k++)
0 R" E5 n4 h8 C: S& S- x - {$ E3 N3 `, R2 w5 |8 m
- Sensor_Data[k] = 0x00;
4 u) w, d9 O2 a6 J D4 ?
2 B' e; a' o' G6 o- for(uint8_t i=0;i<8;i++)
4 `, [" t3 U7 O- B! d' y) z - {
1 F4 W. o/ V0 t& p( L - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level* ~" Y% R1 _- l+ I5 f; }% ~
- HIGH_level_read_time = micros();5 |5 y& _4 F O
- LOW_level_read_time = micros() - LOW_level_read_time;/ [/ o) n- i( k s; w7 \
; o$ J3 I6 o- K! ]" x, s* j- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
) u1 W" ^, m2 ` - {; W1 p) q' N6 u
- return 0xff;. K1 H" W/ x0 Y4 [. P
- }- v/ }6 N* d2 F/ G4 Z2 ]9 a5 o" q ]' ^
^* G1 R* c6 Y- T3 l2 ]- m+ p$ L- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );, Q% a7 ] V9 N+ ]$ U
- LOW_level_read_time = micros();
- `* A! C4 n R6 d" q* H - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
2 s$ o" m! w. E9 y% b/ A9 \# N- z - 4 u, f6 V+ k( Z8 `$ o# l; @5 y
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
# l' M- x, B/ u" T6 j7 P( |2 X - {
* D6 V) e0 o& S8 }5 O- b - Sensor_Data[k] |= (0x80 >> i);' G$ g6 n5 }3 F2 Y8 {2 _4 w
- }
) M& i- t" M3 g: p; H; g8 j! v* _ - else if(HIGH_level_read_time >= 100)
3 o. D* ?2 g; O. ^4 L - {
$ B, k3 B. c3 V6 `- `0 G - return 0xff;+ Q% u% [/ R/ Q. C
- }
$ O; ?9 L8 D1 h, l o, B
7 l. T! V d& C- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
8 M" w# {% D7 k' i) U# s6 t& K" t - {' D2 I* g! @$ ~ o7 I; q, K
- return 0xff;$ `. C1 e2 k6 B" N
- }; {5 C, R: z' _( {
- }
+ O. W2 v! @+ C) B. c' p- x/ U) l - }
3 T/ f% O7 K0 Z( K" [( e* ~! _5 O - $ v; v9 _2 T" x; O D
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level7 q* |* u$ i8 x, Z# e
- HIGH_level_read_time = micros();
/ u& f5 I1 a5 X7 g" I: W/ @5 M - LOW_level_read_time = micros() - LOW_level_read_time;$ c |* N3 c& {
- ; l, h5 X. I |; u3 s2 k) p
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
: A0 s w( r4 b, S - {
' G' h# c$ S9 q a+ M9 I - return 0xff;* _( h" z7 b! M( ~9 O
- }
6 e0 W q3 k* `5 Z7 q+ Y. m+ H
1 H d. L$ Y( B; Z- pinMode(_DataPin, OUTPUT);: V* n* N5 |; O9 H; m& j. Q
- digitalWrite(_DataPin, HIGH);7 N& m- c' a1 G1 Q/ D6 o. r, i
- |! t& z4 J/ }$ F& Q! A) ?: C
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
1 J5 J$ u- q: ]6 Q, L" b! \ - {
: @- I( H$ z9 Q y7 b5 Z. @* P0 R0 ]/ S - old_data = Sensor_Data[0];! y ], s ]. ~
- return Sensor_Data[0];
( T: k ], C- K# g - }
4 }2 O- S1 o" c* a1 | - else" V. {' `- c- d
- {
, [ Z+ f! a4 Y# |4 m - return old_data;
: @& b5 w* s- S6 U! m% k - }: j! f" R6 S2 E* Z
- }
$ e% f! v" A4 a+ k' o5 y3 d
複製代碼
( ]$ h6 D! S% ^' [# nMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
! G% o! G; R# O& h9 M+ a2 l( z' h4 {0 F
|
|