|
|
# {3 d; l/ a+ r4 y+ i
- #include <Arduino.h>+ ~3 X* d3 L( F- X6 k1 h
- #include <MeAuriga.h>9 [/ ?$ S8 T- e$ ]! r D$ g- D
- #include "MeLineFollowerArray.h"% `. W+ h( [/ R& V
- 3 G( k& o$ M4 m8 X4 j
- MeEncoderOnBoard Encoder_1(SLOT1);
! }: b Q M/ ?. R# j4 O. O4 ? - MeEncoderOnBoard Encoder_2(SLOT2);
; K8 n0 k4 i9 E' l8 ] - MeLightSensor lightsensor_1(12);
1 a0 j+ ?3 e5 J9 s& L" c - MeLightSensor lightsensor_2(11);
. f2 F# S$ N+ s - MeBuzzer buzzer;
7 d: C* j. |. f* L: T! x - MeLineFollowerArray linefollower(PORT_6);; P- ?( E4 J+ `" o* ]2 ?
9 n R3 j4 m" h1 a8 P- #define Error1 1- |( n* y0 ?9 _8 I; a+ d
- #define Error2 2- S i* l/ A9 V+ D/ }8 q- H
- #define Error3 35 n& G4 D" J6 l8 P) G
- # y; g5 H- R3 @) {
- #define Kp 15) D" B; Q0 i: P$ M
- #define Ki 0.159 z9 e1 ~1 E. W$ Z% z
- #define Kd 0.03
/ {7 b" t9 \5 V) s; X ^5 J - 0 A( u' ` J- X9 \
- uint8_t sData;
# ]9 w; u8 [' Z3 E. s0 ~0 G# e - uint8_t D1;3 _7 Z, ^. O4 g! Z) C- u; n2 `4 V6 Z7 B
- uint8_t D2;
8 w6 w" h! C! x. V9 j - uint8_t D3;
- f/ `+ M) e+ k - uint8_t D4;& H! t/ |6 d8 @9 @& ]
- uint8_t D5;1 n! u' r5 J5 X7 L4 L
- uint8_t D6;& P# [& b, `' }% A
- ( k" e }4 ?3 [
- float previous_error = 0;
% p6 g0 J: w0 c - float integral = 0;
7 l. }# e% w& B( G% W% A0 D - float derivative = 0;- T, n8 X6 [1 K0 L/ C* X# \- \
- int Speed = 160;
! `/ F' i' X3 C$ n" @# y) i - float output;
X2 {; s! D# R* ]7 b - 8 c) G) Q6 B0 p+ z, e7 R, R: I
- byte Left;
1 H, I* A8 Z! j( F - ' _2 I9 e9 i4 G4 s
- void setup()
- v# b! ? j- e# ?/ X' H - {$ \+ F2 q2 E/ }# h# X3 m
- //Set PWM 8KHz
" v1 s" ~) Z; ^8 P X5 O - TCCR1A = _BV(WGM10);5 z' C& L+ R# q2 L" ]2 J
- TCCR1B = _BV(CS11) | _BV(WGM12);
: i; p0 P4 | v R, n5 D* v. F" t. { - TCCR2A = _BV(WGM21) | _BV(WGM20);
7 E/ ?6 f. F9 V- o( N! \* U - TCCR2B = _BV(CS21);
) W" Q V: w( b9 `& J7 W - Serial.begin(9600); D; t0 I6 I& ?& @* b0 H& d0 v
- buzzer.setpin(45);7 E8 i, n1 Y9 r4 a0 y# j
- }
5 t9 W: ~# ^) O1 M% B" b: H - 0 `7 C9 }! V \' {! }( U
- void loop()
" N9 k B: b* |4 t/ s) n4 o# Z - {/ @5 P6 [" S; j, }. M
- while(!((lightsensor_1.read()) < (10)));
7 y' R$ n) V1 q& C. C - buzzer.tone(1047, 500);
1 B, r0 V8 z, U, a - delay(500);
. Q6 w {4 G5 b* [$ d5 e6 Y% S - do' K4 }# L/ N- E4 e4 o+ m
- {
8 q- c9 H7 t9 x! J# b+ ` - int Err = getErr();
4 l5 X1 p5 i. j) l5 K% v4 k' Q p - if(D1 == 1)
& m5 ]; `! i: O* V$ T" ~! g1 }# T - {
0 L5 e& M- V7 f$ y5 I- x - Left = 1;' [1 V% H* q. S: F, U
- }7 u: h' ~- W7 V* X8 B5 [
- if(D6 == 1)1 G. l8 Z% |. V+ a: g, h/ t
- {! i* {" `( |! {
- Left = 0;
3 D. d. A: t$ b+ K) h$ x6 p! V - }: [0 {) P# U$ S3 M3 Q
- if(Err == 99)
$ u9 z8 ^1 X2 N - {
4 J0 i2 h5 A! \: o) p9 \3 K7 ` - if(Left == 1)9 [0 f. b' R o) m; Z- T
- {
$ ^+ ~. A! U* l* ?$ k; J: M - Speed -= 5;- O, S! H- r; p/ t* Y
- moto(0,Speed);
7 N/ [7 S; U: B$ Z+ s# ], i9 h - do
- K/ b& F' u7 { - {) K2 U& x# J' D
- Err = getErr();
$ I" a, l" w& `* {8 s& P7 n3 L- L - }while((D1+D6) == 0);! r! z4 c( A( m% \
- }7 P4 g% w, h9 P: |
- else
9 h! x8 c2 l8 n" s, q6 L. r - {
$ x' p2 e/ D0 p) B - Speed -= 5;
, {/ I4 e! A/ e1 q3 F) d3 S. Q - moto(Speed,0); . D% z f j$ c9 d: p
- do
- c( S( [% s$ @' l4 M - {9 F/ Z+ i& x! x$ o: M
- Err = getErr();7 o% ^7 A4 r, B( q6 Z
- }while((D1+D6) == 0);. |4 v6 p, a% p: ]0 n- ?9 `
- }; W+ J" v! Z8 I
- }
1 r; h5 U" `" B, @: D% t& h - else% M9 I( ~7 L u H( x
- {3 p+ o0 r" l2 I u4 n
- if((Speed < 160) & (Err < 2)) Speed+=1;
- W1 h' n: R' m: ?' R - if((Speed > 100) & (Err > 2)) Speed-=2;
* V: q3 p2 O r4 u - integral = integral + Err;# }$ T7 |! }( j5 Q
- derivative = Err - previous_error;
1 _$ r. C" I* i) _1 e3 ?' q - output = Kp*Err + Ki*integral + Kd*derivative;
: j. l5 D5 I4 ~* e/ d# G - moto(int(Speed-output),int(Speed+output));5 y* n# ~' ]% a0 T, ?; R: ?1 g
- previous_error = Err;; |0 H3 x M; V8 D! f
- }5 h& m1 `8 ]. }! R# Z8 ]0 @
- }while(!((lightsensor_2.read()) < (10)));6 A. D8 n" ~( U4 N- S, a
- moto(0,0);) p0 E: {0 R% X$ k7 i% ^. }9 k
- delay(500);0 h# `* m. ~7 j2 q8 k+ I9 [; s! w
- buzzer.tone(262, 500);
% L7 J2 ?/ N. m6 f) ` - }
# Q! G0 c& c* `1 a. l# C6 t
+ e: c+ e9 U' A$ }0 I- Z- int getErr()
6 @. W9 E" _4 ^- S - {
% l9 r$ Y: G. P9 Z6 N2 ?% {/ K - sData = linefollower.getValue();
* ?' f" w) A2 v6 M - D1 = ~(sData>>0)&1;/ s* e3 E" M" F9 X7 |/ i3 i' _
- D2 = ~(sData>>1)&1;) d& r5 B& W- Y4 _: {2 ^
- D3 = ~(sData>>2)&1;
/ {+ y c% Y0 D. s: l+ ]- T" t( K - D4 = ~(sData>>3)&1;
5 B) r* D. q. @ - D5 = ~(sData>>4)&1;
, F2 O: ? E; q' y, N - D6 = ~(sData>>5)&1;
, u: i @: I- x9 t3 l } - int downD = D1+D2+D3+D4+D5+D6;
; p9 B0 ]% b4 H+ B( ` - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);. H- J9 x7 k1 [3 m
- if(downD == 0)2 f2 m/ a' j( z4 @. r
- {4 q/ ~- f8 C# R" _; |$ g# F
- return 99;
0 T3 k( ]9 i. ] - }
$ z8 F9 |2 t( _- G- ?: g8 B - else5 `, l% p! t3 D: r
- {
+ \! [3 ]/ q+ Z& H/ E0 S - return upD/downD;1 ] h6 Z# F% m& f
- }2 e9 u' A+ m+ o8 l5 j! m- V( |
- }
# L( K$ J! ?5 b
5 ]6 `, I/ z- H8 b- w2 N- void moto(int Speed_L,int Speed_R)/ l0 o% y3 y. T( f) J' Z* a# C3 Q
- {
. A% @* E4 n; ~+ N; Q/ k - Encoder_2.setMotorPwm(Speed_L);
6 b3 U: j$ N8 P# z& H- j - Encoder_1.setMotorPwm(-Speed_R);
" c, f# g- D, r( \ e p! H - }
複製代碼 1 k2 P8 K1 x4 c4 `
MeLineFollowerArray.cpp( C. E( [# q& {3 i3 W" F1 X& {
- #include "MeLineFollowerArray.h"! L8 s7 _. D3 O- [
- T6 U! _, _* n1 v' ~2 D& ^! @- #ifdef ME_PORT_DEFINED1 m7 k7 M4 E; R/ ]5 w: [
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
7 @; e' _" H5 z7 R9 ~ - {% p8 u8 ?3 C) h2 _6 p" y3 _
: m' w! F/ [2 |- Z8 U$ X- }
1 I/ l8 n& D8 o y2 { - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port): h t! g+ G$ X* o, o7 @, @
- {
+ ?* u+ b8 `1 Y4 |: c; m; g - _DataPin = mePort[port].s2; k+ W0 X9 y4 R) V" r; Z, r
- pinMode(_DataPin, OUTPUT); 3 R: x; }/ } N" g
- digitalWrite(_DataPin, HIGH);
( ~: Z- ~- D" t6 t - }
/ y O* \$ a/ s% [( w - #else // ME_PORT_DEFINED
5 [- _4 V% x! i - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
/ G7 S$ F* o" w2 A9 `6 ? - {
7 Y! ^% p2 d- N& h! B - _DataPin = pin;
% C9 E& ]- [* N. @! b - pinMode(_DataPin, OUTPUT); - Q6 d' ?0 j- A8 Q; k4 P6 S9 e
- digitalWrite(_DataPin, HIGH);
# l7 p& D! ]0 I' B - }
) H/ e7 d8 ?3 d3 y0 h% Z - #endif // ME_PORT_DEFINED
! ~+ s$ L6 k8 ~, p - : I9 c, `8 B+ G V8 n6 d
* J3 Y0 z, Q3 M* D/ L1 L" x: d! K9 l- void MeLineFollowerArray::setpin(uint8_t pin)7 l$ ?* m6 v& q+ S3 u
- {
1 t/ C3 G) x4 U! j! E* d3 Y - _DataPin = pin;
8 e, H- K* k, w% z z0 I8 a - pinMode(_DataPin, OUTPUT);
. V7 u( e7 J5 Z3 j3 J, q - digitalWrite(_DataPin, HIGH);
5 q- z W3 w q! T1 T8 Q1 T9 K4 K8 a
& A9 j/ p/ Z/ T+ J2 v) ~3 I- #ifdef ME_PORT_DEFINED% V9 R$ e: L5 L
- s2 = pin;
' P+ B3 @& u" ` - #endif8 G' y( A& v! A) f& a1 {
- }
" F3 Y% ~1 B0 g8 O - - R0 c: V, t/ E$ f4 m
- uint8_t MeLineFollowerArray::getValue()
, Z' p, T; R+ O! T - {
& z2 f, d. S2 v+ M - uint32_t LOW_level_read_time;
9 v4 ?' _* m/ ^2 b9 @ x - uint32_t HIGH_level_read_time;: S" y/ r, }7 o/ k/ g
- uint32_t time_out_flag;- _" n( }/ K* [
- uint8_t Sensor_Data[3];. `) [" H4 p. H1 i B& a" h
- static uint8_t old_data = 0xff;9 V9 f( k- O, {. d! V
- % V4 A5 c# ^* s+ P. q; R# f
- pinMode(_DataPin, OUTPUT);
1 C3 h" Q6 W4 c - digitalWrite(_DataPin, LOW);( G, @& \9 l! m+ _7 A7 g5 L
- delayMicroseconds(980);
8 {7 ?5 X9 g+ @2 p - digitalWrite(_DataPin, HIGH);
4 r) I. K+ i2 i4 T
& T+ o! J m4 O& `5 @- pinMode(_DataPin, INPUT_PULLUP);1 @7 f: [) w; ^/ P( Q1 s; g
- delayMicroseconds(10);
- a/ D" v, I- _8 O0 s F - - j& I7 {4 ?) n
- time_out_flag = millis();! t2 t1 k# ?9 o1 i
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
; H" J6 r9 s2 G. H
& C( R9 ^" P4 m$ j Q/ b" Q- LOW_level_read_time = micros();) t# K& _0 j, x* b" F: c3 g
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out6 Y# `. u+ b6 q6 ~4 k* M6 K9 [2 e
- {
$ t; r/ a2 P5 t" w& ^6 { - return 0xff;, X( U, @' V# C# C
- }
: u5 K; j- _6 L4 V9 K1 x0 a - " v6 m1 l( y9 g
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( W$ E( h: n, E2 {( E( _ - 1 w1 j$ n3 \2 W2 M1 T9 ^
- HIGH_level_read_time = micros();8 J: d7 r% f3 A/ Q6 C
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level) Z* a( {# e) A4 |
- ' q' F* X) N2 ~: _1 M
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out2 Q. C4 |* e4 G1 }5 B% e
- {4 a+ F7 V; {9 @- ^# `6 V
- return 0xff;) Q1 j$ ^( \) ^1 D! q9 ]- h$ X1 a
- }
/ X3 e* k, o- s4 G5 M6 v$ }$ N
% M) A" \' ]% o( j( L9 G: A- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
' b9 E6 o. [1 N - {. s' ?* T' g2 l, A+ F' J: l! T
- return 0xff;
' e' L" `1 s% u t/ j - }& ?8 r/ D* L$ |* m9 m) z2 n
+ B6 c: A# R- ~/ g$ G+ d- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
# H$ p+ }# i1 R) Z9 ?% A4 M9 P - LOW_level_read_time = micros();
# ^; C/ j/ A8 o6 A - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
$ R V% F' i, `* h+ D$ @3 S. U9 c - ! G6 n0 ^$ d T0 i- [
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- P6 ]) n. a/ V: Y8 i. o4 V
- {
@9 Z! V) s* Q# ?7 j I1 ^- n - return 0xff;8 z# K/ Q0 K- P1 P
- }9 G+ q. X! ^3 C' S) I7 _9 g
- . ~' N) v( b. | ?7 s' [7 t" a3 d
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
! h" b4 K1 o1 f& l3 c3 t - {
% a9 M6 q9 P0 w( { - return 0xff;* N; X2 N/ z" \: r3 [
- }& { P6 [) {. F4 G" v( v$ z
- % Y; ~: U9 ` [9 l, q; w# j* ^
- for(uint8_t k=0; k<3; k++). d& @& Q z% v, _1 V, ]( A2 k
- {
, d0 Q& w4 ]: w7 @ - Sensor_Data[k] = 0x00;
6 k, k3 \9 F3 V) X
. E! i# {. n: B; U- for(uint8_t i=0;i<8;i++)8 F- ~9 `" F: [4 H _& j2 x
- {
9 g* }, u, q; `! X7 B - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level5 ~$ |, z2 J( D' ^$ j
- HIGH_level_read_time = micros();$ M' |4 b5 n, q: {7 u
- LOW_level_read_time = micros() - LOW_level_read_time;
8 V; L ?/ t9 R2 q# T+ e
. F8 M& ?7 \. f) V4 }" M H- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )/ W( V; S; F: r2 T
- {# @8 I% N& L& D4 b1 A Z" _" c( o$ |
- return 0xff;
: [$ Q( r/ Y0 q8 v8 {* p5 h) ?4 a - }
% h Z) g: K2 m0 D4 f - + g: s6 m7 ~/ X$ {% N& u
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );2 f/ i* {9 b3 u
- LOW_level_read_time = micros(); I- F8 i! F5 t; T
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
+ _- e# S& R( m: d5 _2 U5 g- \ - - p$ n+ |5 b3 w( z% A
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
, x& Q, O: H0 L( N$ o - {
9 c- G# Q7 Z1 @/ x/ N) } [ - Sensor_Data[k] |= (0x80 >> i);
8 \, H8 ^9 C( v2 ^+ b# I! \6 n - }
2 h( ~: Z& v) `3 V - else if(HIGH_level_read_time >= 100)* h; u. j4 ?, w1 S
- {( ~7 t4 V t- [& L, u' S0 x
- return 0xff;; R: ~: O2 y& Q3 U& o
- }
. o [0 u4 @0 r% u' b& ] - @/ N0 V6 x3 Z, f2 h- f" u( I
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
% q V. J1 i5 X( M6 R9 T# T" n2 d1 T+ p - {; N/ V, L' a8 c$ q% D. u/ p+ m; C
- return 0xff;
9 g- q% C- n" v( A) N - }
7 M& C# K7 G) p - }
# y9 k, |! W+ ~& E: ?* V - }2 M1 I' ?# y+ e9 Q9 _
/ e* `) h$ v: j* H* T7 u4 h9 L. w- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level, ]. u y; f8 n. X
- HIGH_level_read_time = micros();0 G6 ?+ ~/ ~& m0 S6 Y
- LOW_level_read_time = micros() - LOW_level_read_time;% b& U$ I+ _2 E
7 ]0 g h; h$ b- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
: _3 Q, X2 ]2 @$ _; u - {: D( K3 O W6 f
- return 0xff;0 n) j2 R" a7 R0 q
- }
; X2 Y3 ^6 l1 C6 ?) e
- E* A. |+ q/ \0 B: T$ E- u4 G! u$ `: j- pinMode(_DataPin, OUTPUT);6 X5 Q: K8 _7 V4 w5 \
- digitalWrite(_DataPin, HIGH);5 M! n* g g- H0 l8 ]7 q8 {
- 2 e0 H; [: E- j4 b7 w7 O- D* Q
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
. t; S$ t/ X' c. W X - {5 @8 |* F O+ S) g
- old_data = Sensor_Data[0];8 R8 l6 R6 s+ T
- return Sensor_Data[0];# _! A( A( }; d' [! s- s
- }
- |+ P7 t- a+ F+ ^$ U7 F; t2 ] - else
. B% E/ G5 J- s9 }& z6 y+ o - {
6 m. r# l; [4 d7 w - return old_data;5 i& A3 d `3 D; Q
- }, }8 c- D: A$ y7 I
- }8 a! Z6 n3 g6 @: ?
複製代碼
& V' `8 \0 C$ ~" EMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
3 Y" P2 m( a8 t3 a7 a7 w) r# P
/ z$ w6 K. q- \- [4 f" D/ o! a |
|