|
|
9 M( ] u/ `1 E9 m* u1 k- #include <Arduino.h>, ^; A8 V+ S; e6 L* i: r- w8 d
- #include <MeAuriga.h>
S5 z$ e7 ^0 ^0 K2 |1 f - #include "MeLineFollowerArray.h"+ ~* G) K9 g4 D2 i! X* O. s
+ `; Q: v/ W% y& l9 @6 t- MeEncoderOnBoard Encoder_1(SLOT1);" i7 ?% S7 R7 Y7 I
- MeEncoderOnBoard Encoder_2(SLOT2);
' C% w0 q# ]) t4 w2 i - MeLightSensor lightsensor_1(12);
5 y# O8 \$ ^0 m+ m - MeLightSensor lightsensor_2(11); o7 L# e$ U, r$ [7 `
- MeBuzzer buzzer;
) ^+ N& e' t; s! N- x ? - MeLineFollowerArray linefollower(PORT_6);
. G' `+ \: k: u; [/ S
7 G/ O4 n* p; r: F# M1 s- #define Error1 1
$ f/ a$ h _$ _" y, N& v! m' r1 y - #define Error2 2
- [5 g, C0 ]/ B j1 H0 p - #define Error3 3
N2 n/ t9 p8 F3 h: Q1 B
; t) c; p6 C" ?* d( U# S3 M- #define Kp 15
) P& t b, X! M$ S - #define Ki 0.15
& F6 N% W0 C% E9 o4 B. ?- M3 t - #define Kd 0.03- ~- d I: e, |' ]* L: y4 }
- 8 l, x2 I/ \% C7 F6 j; F1 J
- uint8_t sData;* v8 P) n+ q% h, J
- uint8_t D1;+ ]: P# U' C# z5 z0 _
- uint8_t D2;4 C2 y) N$ s; \% ]1 H" B" O0 T' P
- uint8_t D3;
+ O3 |2 ]) R* O' I - uint8_t D4;
: }+ O: t4 _6 F: _1 n! d6 e - uint8_t D5;0 X+ E7 ~: R, G$ X4 E1 Y
- uint8_t D6;- i, P Y9 V9 Q1 m' O
' o4 y5 [2 A8 C5 m# O- float previous_error = 0;$ w4 Y' f' a" @) y3 j& i
- float integral = 0;
1 f e/ D8 S, ?3 J) G' d - float derivative = 0;
3 z: s+ Y) b& y% M9 K! _. C - int Speed = 160;' A% s- W+ l9 D7 K. Q+ }' \" ?
- float output;: w) a4 X- a+ m/ C7 A8 b+ C
6 d9 y' L6 B7 y0 ~7 Q- byte Left;# d/ a: b; n6 o0 m9 s O
- . t3 h, J1 s- _$ X
- void setup()
" G, c/ { V5 f* r" H, I! ~ - {: @' X1 i; ]3 u0 x4 G
- //Set PWM 8KHz( ?% x# O! W4 Q
- TCCR1A = _BV(WGM10);
- D! _# p2 x0 U+ n' A2 o w' e - TCCR1B = _BV(CS11) | _BV(WGM12);0 J7 t0 o! u8 |5 Z
- TCCR2A = _BV(WGM21) | _BV(WGM20);* t8 T9 b. J& O* U2 J. z
- TCCR2B = _BV(CS21);# ?( L, F# I% J2 |6 G
- Serial.begin(9600);9 q* T& r2 G! d/ P/ w( u
- buzzer.setpin(45);
2 i' c3 s0 b) v- f; F - }0 Y% n t) O/ v5 P
- ) q: V) I8 b4 j. I# |: b
- void loop()
3 R; d; A. k) L" G5 ], A% v2 z - {- Y6 f, _. j' o4 r6 h0 z; C
- while(!((lightsensor_1.read()) < (10)));4 K6 k: f- E: o% I* X& o+ b% q
- buzzer.tone(1047, 500);: p$ s0 C, F; I5 ^
- delay(500);9 R. G4 t) ?- ?8 n/ d8 h% [# y
- do9 s! D: K% ^9 c5 M0 {) E- E
- {
9 o6 I2 G. d. i$ H - int Err = getErr();) O. ]/ g; A& U T' p! W: T
- if(D1 == 1)% P1 X. {: q; ^5 L+ D! t' E- z
- {
3 X9 E. y$ I8 N0 P* P/ `% Y - Left = 1;
: b" [8 K. P8 t6 O# ] S9 Y - }
2 ~4 p* y3 f h5 ]4 Y - if(D6 == 1)
4 o) @. l A3 I: }. k - {
2 s: r* r8 [2 n" q! ~ - Left = 0;
& p6 s# h! x# o. f$ U$ m - }
% O0 J# ^. P0 _4 a# K - if(Err == 99)
0 E1 E, n1 C# @7 n - {' x2 q3 d6 n W. O& l) u
- if(Left == 1)
$ r1 o5 [' k: { - {; n& h: M% c- ~3 d# c8 ]
- Speed -= 5;
3 \0 v. G9 r% x1 i( W - moto(0,Speed);0 K- [ v! H+ P1 b
- do
. _6 V1 n& |7 S1 @. [8 a - {1 l+ z0 O8 E8 X6 `4 C$ @. q2 A
- Err = getErr();/ u3 l, n2 ?; W9 f S6 m! b
- }while((D1+D6) == 0);8 H# [, h6 z1 Z; i+ Q3 Z; x' g
- }
' Q, n8 l2 j- ?' b; @9 ^ - else/ y: ~$ G0 m* W' D% z* l, n
- {" U1 `- H$ H5 E0 t+ E2 Z5 |" S; k
- Speed -= 5;) n2 L, o% j1 S* K7 J& F+ L9 Y0 r
- moto(Speed,0); J c5 M) n$ m4 v [
- do
% B9 _2 A! L/ Y( y3 i: t: C& f - {
, z( ~) n c- l- a X - Err = getErr();% z/ e( c% k: s. D- w
- }while((D1+D6) == 0);
2 W" X- ^, B$ ` - }
+ H' R. e2 w( ? - }
: b/ h. k6 V/ M! W, L* r - else
# T2 I# y# Y8 T' O7 @ - {
3 O1 s i& F% E+ n' X3 ~% ^; r - if((Speed < 160) & (Err < 2)) Speed+=1;
' U- r5 p2 w" A7 Y! l- L% G - if((Speed > 100) & (Err > 2)) Speed-=2;
" l- _0 K3 I* T - integral = integral + Err;: p% q4 }3 T9 L4 @* \* k
- derivative = Err - previous_error;
% [. `' A8 ]9 c% p* U J* _ - output = Kp*Err + Ki*integral + Kd*derivative;
" H d7 X! i. p - moto(int(Speed-output),int(Speed+output));
3 Z5 |5 `: b$ y0 J8 ^0 ?' S% ] - previous_error = Err;; N6 X' W& M! D! C! }: \/ R
- }; @+ V5 `4 h3 s. e1 _4 |, I5 u0 ^
- }while(!((lightsensor_2.read()) < (10)));
# Y4 L( H1 i L. ~% V1 a1 ?3 b - moto(0,0);
0 x7 C1 i: R4 E9 K. a - delay(500);9 W2 y! S4 _( z' Y
- buzzer.tone(262, 500);/ w+ x3 I5 k. {7 V! c
- }
0 Q$ N: P4 |2 c2 E - 4 i( P! p W# r. \/ H2 q" x2 y9 V2 Q
- int getErr()$ P. o+ {+ l2 B0 [' }4 A
- {
+ V6 X; U" w) D* _+ L5 w - sData = linefollower.getValue();
4 q4 n4 a3 c! A! r ^% G% z% i: Q - D1 = ~(sData>>0)&1;7 u/ z, t1 q5 N3 E
- D2 = ~(sData>>1)&1;+ ]# _- u$ c: Z& X! z
- D3 = ~(sData>>2)&1; N! E- @: w4 H7 u& B7 o: w0 B3 G7 u
- D4 = ~(sData>>3)&1;; K% u/ ?: a5 ~" S! N% O/ L
- D5 = ~(sData>>4)&1;$ |" A! v. Z/ W0 c3 V0 q1 z
- D6 = ~(sData>>5)&1;& `9 j4 _! Z4 U! b# V$ E/ b
- int downD = D1+D2+D3+D4+D5+D6;
# y4 \/ e. H6 H0 L5 L! i - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);1 v. g5 i9 H9 F+ Z" K
- if(downD == 0)
! M0 ?2 X1 k; b R& x9 d" k* ~ - {
1 X& L! m9 y+ j4 Y; G% e, e - return 99;
4 c7 O E3 S$ [- I0 g - }
7 u6 T2 k: N7 w - else4 k$ R* y- B- a
- {4 ]3 \& j) _6 z1 `$ }/ E
- return upD/downD;
, U" c% w( h* z ]0 V - }
& y: B! L7 n ~9 A0 J - }4 C6 k9 c2 _6 W0 D! R9 \! C
- 0 c+ v2 a/ Q% j6 ^% B3 J3 v' o7 G
- void moto(int Speed_L,int Speed_R)
. {0 o- ]; f+ m ~% D- b- ^ - {; b: h9 i0 q o" Z! o
- Encoder_2.setMotorPwm(Speed_L);+ R @2 p5 q3 m, y
- Encoder_1.setMotorPwm(-Speed_R);
- \# z* ~/ ?' Z1 i& h: Z q4 d: ? - }
複製代碼
. R: Q) ` p' x, wMeLineFollowerArray.cpp- s- n8 z6 j) Q
- #include "MeLineFollowerArray.h"
9 l8 i: j( C, S. i - 3 ~6 x, z6 r4 O, l a, [/ i ?, _
- #ifdef ME_PORT_DEFINED, d5 ]1 f) p/ l
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)5 p; ?- E9 z! g
- {# f5 e' u7 ~6 X
- ' y+ `4 I! U1 L2 @6 r; N
- }3 c1 L: w6 y2 Q _
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
1 e6 r! I( Y" I N9 b. H) Q - {) w, ], [% Y! o. W6 C0 r
- _DataPin = mePort[port].s2;
- Z' B1 O: ~* S% ^6 x9 Y" E - pinMode(_DataPin, OUTPUT);
0 ^9 \/ U% z1 w7 f+ P# y! A7 e - digitalWrite(_DataPin, HIGH);% i* b% j: z$ F- c5 }
- }
2 _3 x, F8 Q, n6 ^) X - #else // ME_PORT_DEFINED
! d+ ^( Z. | f( `' m: m; u& m - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)+ S0 a. W2 ^( J
- {
6 }* p' n. U& M# Q5 q: P1 u3 C1 C - _DataPin = pin;
6 Q! ~6 b1 K' z g - pinMode(_DataPin, OUTPUT);
$ w1 G; h6 W( j$ g7 |! l - digitalWrite(_DataPin, HIGH);1 x3 x; \/ u% i) L* l+ N
- }
: E' B$ S% \. C. J - #endif // ME_PORT_DEFINED
; J9 v' j+ s% I2 o. m; h! Y
) U) @. C" Q6 B; i- G% x$ L/ ^
1 @8 N' H4 P4 ~; Y% n7 b- void MeLineFollowerArray::setpin(uint8_t pin)
! Q# O t7 W, y) W, X3 k - { Q; f* k$ q+ G K' @0 O
- _DataPin = pin;. t- A B" O* r( ~2 a6 u5 z! J* [
- pinMode(_DataPin, OUTPUT);
, l* c" b2 E4 q! r, ~0 g" i - digitalWrite(_DataPin, HIGH);' j3 e0 a- Q/ |2 s8 I! z2 v7 E
& J8 Y; l2 Y4 C% K( X; N( g4 j7 _- #ifdef ME_PORT_DEFINED
O, n- p2 \# m8 h c/ B - s2 = pin;* F+ _. |% x/ y: K9 ~* W
- #endif8 E& Z$ ^, x/ f+ \) J7 `4 ~
- }1 e E1 o3 ?8 Q+ B! T
/ |+ E1 L" U; P4 a+ i( B- uint8_t MeLineFollowerArray::getValue()* N, ]3 f; ?. u/ D" `) _& B
- {- s$ j! _5 A& Z; H2 E% |( {6 |" q
- uint32_t LOW_level_read_time;
. B9 C) M/ U! V/ ` - uint32_t HIGH_level_read_time;
z5 {7 C/ [3 h - uint32_t time_out_flag;; ` i$ O/ ^% \
- uint8_t Sensor_Data[3];
/ o) @$ D( U! L- ] K - static uint8_t old_data = 0xff;" O5 ~$ Y& `1 n0 ~' x
( g) ~/ k# I, e0 m' S- pinMode(_DataPin, OUTPUT);5 S, f5 M* Z: ?$ C5 S) J
- digitalWrite(_DataPin, LOW);( d. R2 E6 w ~9 W
- delayMicroseconds(980);4 p6 @( h8 A" U2 {! N! R
- digitalWrite(_DataPin, HIGH);
9 }0 ]0 F( v* r! o' b
9 k Y! {7 X" Z+ _% G- pinMode(_DataPin, INPUT_PULLUP);4 s% \- M$ k& U% ]) a+ u
- delayMicroseconds(10);. V- [% R. Z1 v
9 W0 C6 A( f3 p# a- time_out_flag = millis();
0 O# S$ r# g. @& ` - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );" Q, ]5 w- ^( S$ K! k) I! ]
- " Z! M8 r' Q% R
- LOW_level_read_time = micros();
3 T2 g! M' \$ d& H6 u/ `% Y$ { - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out9 U+ C0 C v6 [
- {
) g/ ~! L4 A2 E, ~. r7 ^- U% h# E - return 0xff;
9 E" ]' s2 b& c( Z' o- J - }
J0 p$ J y3 g+ E2 }& l% T
# B1 N. F% y$ T9 Y7 _) T, k) D. g" g- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );: N/ [6 p1 `& V/ _! z
- 6 i8 D3 r, n7 g3 k4 K
- HIGH_level_read_time = micros();
9 f4 Q4 z' f( u2 i' a$ L5 K - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level) {( A4 k; Y8 I5 j: Y6 N
]5 x( R7 M( ~- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out1 d6 Z4 o$ \3 z' p* V1 u
- {
+ h9 R+ a# O2 Q! M - return 0xff;8 h# `4 K$ {' O% X
- }
5 r. s6 d* U. t - $ I2 l5 c. e' D- r* K- t. ~ ]
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
7 h1 j/ I# t& N( i. E - {
1 b! o$ j" ]( R' V% |, } - return 0xff;
* V( x( P8 x1 z+ K - }
0 S, w' `' h/ i0 ~ - ; f( r: Q C& P* p, y
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ I$ r( g, A5 v9 H9 C - LOW_level_read_time = micros();
- C, Y$ B9 N* Q4 G" D6 Q' r - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level$ |" m3 k2 [1 A* ^( q
! o8 [2 j* U9 Y! l: x3 M, ?+ U- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
# C V# ^& f* W# z3 \6 P - {- `/ g! s2 h6 Q1 W# h
- return 0xff;
# L# m$ {% \' \" b1 g - }% p/ e& H6 V5 Q) r
: r3 v, W! t/ {+ O- U: s- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))$ x% j/ }1 C9 w& ]$ W) V0 X
- {
8 H3 c5 s( j5 E5 A$ J/ M: t% e - return 0xff;
0 R8 @0 M. c0 q) N ?! Q2 H - }) U; b; F. G6 t/ ^& i6 {, f
! j# \& V' Y2 y9 G/ @( }5 l- for(uint8_t k=0; k<3; k++)
+ X$ f! N: c' G" F! i, p8 j - {
' n# M: S' c* Y/ g% L - Sensor_Data[k] = 0x00;
* t! x4 b5 d8 h* W* U c - ( X R( J- R3 O& e& r- b7 r7 o
- for(uint8_t i=0;i<8;i++)
( { L3 P/ N2 f; | - {- a, d+ z1 M# D
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
# a" W7 I7 @2 X1 d- ?: @0 ? - HIGH_level_read_time = micros();
5 [# S6 K& X3 ~. W1 ^9 n - LOW_level_read_time = micros() - LOW_level_read_time;% k9 M; n+ \3 P) K" @
- 4 J; \' k! j! k+ w
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ), v4 X% v. ^' m$ Z; V _
- {' ]- G0 L6 x* ~! V0 X/ h
- return 0xff;
6 H9 B' n' M! h1 ^8 H) s! a1 w - }
8 C4 F% I9 \8 ~8 q+ H) J- m) K& C* f
2 c* a2 Q* V; C- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );+ @9 N- L3 k1 k4 p
- LOW_level_read_time = micros();
% B* C$ |$ M' P" n- \ - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level6 M) D( \6 j% s2 D9 j8 u
- # w6 c( N% q, o( C5 [. u
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 14 n2 E9 `' M0 T& S3 e& N4 h3 P
- {
8 f: m1 i6 X J" [. \ - Sensor_Data[k] |= (0x80 >> i);& j2 ~ W5 f. }. c; _, ^; C
- }
; ^. c2 k6 j' s - else if(HIGH_level_read_time >= 100)8 R2 Z- O& J6 r! v
- {9 Q$ Z9 h, Y) L" _& n* v |
- return 0xff;# y& p6 z' \, P
- }
0 O$ T4 j0 m ]; W, b, g8 v - 1 s; v2 t% K, Y; Z) e, c, j) ?
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)$ m, U1 a+ R6 Z7 n2 M( Y
- {
& j! x4 x0 N7 Q# p0 R8 k - return 0xff;% V: u* ~9 Y% b4 g& @/ ]% W
- }
4 d, R/ x# n& V7 X H - }
) Q1 \6 p2 c3 S2 G6 l - }4 ~# v/ M/ O6 P/ Y6 t1 o
- , z" n6 X* D2 }6 S1 h! M
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
2 z6 s3 i, q" ]( Q* \* k1 z - HIGH_level_read_time = micros();; B* K$ v a. K6 q, S* J* Q+ y) H
- LOW_level_read_time = micros() - LOW_level_read_time;
8 E% o1 p5 k: B
6 _0 M5 g$ B' P. g; W* a- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )/ v5 n$ w+ Z. M7 Z: ]4 f( |
- {, _( `4 u W0 _4 z: J W
- return 0xff;% }8 F% A i( _, \% E9 p9 v
- }9 c1 ?3 n+ i% I+ V$ N4 } E. i
$ l' q1 n1 X1 d( z; }% d: y/ X- pinMode(_DataPin, OUTPUT);
+ c- a0 }$ ]7 ^! Z7 F - digitalWrite(_DataPin, HIGH);
: @. L# U# H! ^* N! `4 b, y: C
: r% X0 f* |9 q9 H6 p' \- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
' G/ k. x) f) e% ]2 ?( s# V1 A2 V - {, {- W) b! B9 ~; d( X
- old_data = Sensor_Data[0];
% a" A( K7 h" `0 M* \$ W - return Sensor_Data[0];3 e# x2 B2 `; a Q
- }* x4 n1 O8 z; g- d# a
- else5 Z9 i# J7 F( @, ?
- {
% I- Y) w7 t0 I1 M3 k! f' E - return old_data;
7 x7 {: ^9 I! h' q: v, C! j - }
. R e# P+ I: w" W - }
* R- |/ n& Z. y% [9 k4 j
複製代碼
5 B: e/ s/ W* a! { T. FMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
, E9 h6 b8 |; _. X1 | X
7 A1 i5 W0 h1 A8 Q) U& c R- _# P
|
|