|
. K$ a q$ A5 r( h' h- l
- #include <Arduino.h>8 ]4 o( e, @, d+ {- B
- #include <MeAuriga.h>
( Z! A9 G/ e v1 p) {, E - #include "MeLineFollowerArray.h"; C6 L$ \4 k& |( G" S5 D
- 3 W* P0 w N t6 k% W
- MeEncoderOnBoard Encoder_1(SLOT1);! H8 |" K( D$ Y" K9 B/ }6 o2 G
- MeEncoderOnBoard Encoder_2(SLOT2);
' u1 Z3 R8 Z( S6 Q/ S - MeLightSensor lightsensor_1(12);
" N- l' ?$ v7 Q' E" p9 n# D6 A5 m - MeLightSensor lightsensor_2(11);9 B( [. o8 c4 u) X
- MeBuzzer buzzer;
2 y: l( e7 N# W) F @; ^ - MeLineFollowerArray linefollower(PORT_6);% X8 U) o! _( B0 i6 T$ C
- % a7 V- i0 a) F) C
- #define Error1 1* V. ^+ X- p1 @0 S) R1 u
- #define Error2 2
4 Z. K2 O1 ]# e3 e1 W i: V% k+ n - #define Error3 3
0 P5 _) f( n* {1 u# J; i
2 ~/ u( p& v; u! G* G6 l- #define Kp 15
$ v' V+ Z' k( H6 N6 G Q* { - #define Ki 0.15' F3 k+ s3 q3 [9 p# S& }* e* u8 g" ^
- #define Kd 0.03
6 a6 d& e* w9 N* z, b
4 Y0 ?" H. E t9 h! D3 z9 r, c- uint8_t sData;5 G3 e2 P/ y7 C0 E ?8 j9 Q
- uint8_t D1;) | g6 k, W4 q* e, t
- uint8_t D2;
* X) S; }; C' @) K P! G - uint8_t D3;5 x( e2 e! {/ v3 X
- uint8_t D4;- m) @2 n7 k3 d* N( B2 |6 {
- uint8_t D5;9 K8 x- u0 S+ w) ] j+ g' o
- uint8_t D6;
& w/ w* G/ a1 f - ; { Z8 L5 |6 _' g% \
- float previous_error = 0;3 ~4 B5 k W( B2 Z% }
- float integral = 0;
7 m8 B( k6 W: O; E g9 {+ { - float derivative = 0;
. Q! S( X! e5 ]8 c - int Speed = 160;
. S" s3 q2 G0 ^) {$ M - float output;: s, ?! @8 G: b% k% S6 F
- ' Q* K- [" g7 y* `0 p$ G
- byte Left;0 ^+ T. w. C8 K4 ^- y) ~
) t( n4 Q; V6 P" @$ Q# y- void setup()
4 _( a0 w5 b( l - {
7 A! o0 r( P' h5 R, d( Y - //Set PWM 8KHz9 H+ C; G; @$ Z. `& I- b0 M
- TCCR1A = _BV(WGM10);9 B' a3 q! t w, }- @) {* H" b3 t0 w
- TCCR1B = _BV(CS11) | _BV(WGM12);
, L/ z) o* o5 o9 v* Y6 A - TCCR2A = _BV(WGM21) | _BV(WGM20);
, C! Y; V4 k$ m& |+ I. q - TCCR2B = _BV(CS21); R$ z& \" l8 ^4 s6 h/ {6 P
- Serial.begin(9600);
; P3 m4 q1 X$ z! ?5 s9 @ - buzzer.setpin(45);
" w* |0 \7 e6 r) y: m' w - }4 O* ]" [; `9 w7 Q& l+ S
% ^9 \7 k% C; u+ m* U7 i- void loop()- k- n6 Q3 i" }9 @. y5 G; W
- {
8 l% T T+ c$ V5 f+ H$ W0 j - while(!((lightsensor_1.read()) < (10)));" ~6 w8 k- P9 f
- buzzer.tone(1047, 500);
& |# H5 \% v4 Y' K) C2 N; b( ` - delay(500);
& J* O, Q7 ?' a5 c4 w, `# K& ~ - do: H! I. d' R" O0 k, T2 [
- {
9 y. @* d/ q6 p - int Err = getErr();' r+ [7 T* ]* B/ P4 N
- if(D1 == 1)
; I. M9 u, q6 w6 c4 j' Z K - {5 i( |/ \' |* G, e4 a
- Left = 1;5 g& }, ^; Q: x# m" U* |
- }) A6 p$ R" \8 g
- if(D6 == 1)& F* p Q* E, t9 O! K7 I! J- h+ Q
- {" m$ w) Q1 F5 v. G# s7 c. n
- Left = 0;( P. Z/ {3 O- c; }: U1 g
- }& f7 @7 f: y( e
- if(Err == 99)" L! A: E3 \9 o
- {
! x+ a- U8 o1 @! [) B - if(Left == 1)
3 E; u% y2 d) W9 u4 [2 Y! j( T - {7 R* ?4 b7 U# f! K: K
- Speed -= 5;7 v+ z" W- s! R Z. F. L1 ?
- moto(0,Speed);8 y/ o3 l+ p6 \5 M# j
- do$ I7 e: @4 ` X3 g* R
- {
. l: i) `7 s7 D$ t2 s- [' h* u - Err = getErr();
8 \: \: W3 A* m - }while((D1+D6) == 0);
) T& b( `2 O. R - }
, d9 S% A# W4 O" T: J3 R" \ - else
5 s- [4 ]2 E! ^! p b1 C" z - {
$ Q9 @) F, Q3 Y: I& J" h - Speed -= 5;# r/ r1 N9 A6 [" X }
- moto(Speed,0);
' r: ^2 Q* P$ A) I" J3 T5 @ - do- Q7 |* a% o+ s# A9 ] M
- {
; G2 g p" \& ]) Q( Q3 @7 { - Err = getErr();, I* u( V# k" ^ ~! c Q3 z
- }while((D1+D6) == 0);, Q1 k7 {7 F! I9 u; c
- }! D. J3 g' ?" \* {5 {2 ^6 u
- }
1 T+ w( C" S. j- o! J4 q - else9 T4 E- Q2 p7 F+ ?, B/ j6 R5 F
- {& T' }2 }2 c7 @% ^7 I
- if((Speed < 160) & (Err < 2)) Speed+=1;
. S1 q8 _# x+ T5 y4 v) @& r - if((Speed > 100) & (Err > 2)) Speed-=2; ( r$ Y8 b6 d5 u" P( V3 V$ G6 c2 f
- integral = integral + Err;% n2 Q0 C5 n# @7 t p- f: H
- derivative = Err - previous_error;
& F- Q* r" c: @7 v* Y3 ? - output = Kp*Err + Ki*integral + Kd*derivative;
# J- F7 u+ q8 ]; m" [4 \+ K. r) w - moto(int(Speed-output),int(Speed+output));& T2 E. L$ ~7 H$ E' c
- previous_error = Err;( _8 C4 V1 R& G5 `
- }2 [# g' _3 N& E' i4 O
- }while(!((lightsensor_2.read()) < (10)));
% a4 [" `$ ~+ a2 ]0 } - moto(0,0);
! u- X, f1 g* I9 b* c# _* T - delay(500);: ~! L+ z+ s |' z
- buzzer.tone(262, 500);
: q/ M2 {+ B4 [3 q: {# T" E+ ? - }
, A9 p4 |9 r. o" ~5 U/ f
- h0 ?- v* y5 }3 E- int getErr() Z) k' c9 Q# z% P5 r
- { 4 D3 K( q) f0 }& t
- sData = linefollower.getValue();
% w9 \! o8 u3 o - D1 = ~(sData>>0)&1;8 u# Q7 z. k) n2 L# ?
- D2 = ~(sData>>1)&1;
. v& G' e# \- V - D3 = ~(sData>>2)&1;! s0 q0 C. f* R# N2 b& _ U& h
- D4 = ~(sData>>3)&1;7 Z5 V* [; M! D2 H) C
- D5 = ~(sData>>4)&1;
# H" }/ g$ A8 b' P& T5 l, [4 ^7 v9 v - D6 = ~(sData>>5)&1;
2 t3 R6 [1 f2 j5 r* f& c - int downD = D1+D2+D3+D4+D5+D6;, f. i/ w& l7 l2 E$ ~$ a
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
! S+ y& I; @, Q/ F% R - if(downD == 0)
3 k0 V* y* z" k6 K4 |* x: D. R( h - {
8 e0 c- ~' V- O# ]) e - return 99;
6 R0 z5 p( b9 F6 H - }( I& ], |( }6 k: i0 t- ?
- else i0 K ]$ T6 R' f! |, N
- {! {3 O! V/ V2 u& |8 e" R- i
- return upD/downD;% ]- s! W; i& a8 |2 }" p. P
- }
$ [& W4 |- N& w# X% Y, c1 m - }
) }* [: W8 s! {7 ~8 ^! E - 1 r& y5 j. O q. {% E
- void moto(int Speed_L,int Speed_R)) o- \5 U5 D: [) x, _. ?" N# G
- {
9 n8 T0 [+ q; }! l$ {; d - Encoder_2.setMotorPwm(Speed_L);
( Z1 c) Z3 M, V; ]- K - Encoder_1.setMotorPwm(-Speed_R);2 b2 h+ s( X. s
- }
複製代碼 1 s( m. c0 v$ h1 a5 | V# Q
MeLineFollowerArray.cpp
' x0 U& Z; \3 T# Q" Z. [- #include "MeLineFollowerArray.h"
$ v! g, ?' t7 `3 Y: G* W+ {3 S
1 O9 Y% v8 d/ O( |5 \, Y# k, M, R- #ifdef ME_PORT_DEFINED( t4 c3 y5 t: V2 T* Y# R
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)2 r6 P; ]4 Y t9 g
- {
; x/ ^. d" }' e: ?
9 D% u6 U9 {" ^2 X% K- } ]7 F8 s/ V7 U
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
% s( M4 z- U+ ~4 r P - {
6 L* ?- W3 \: U. g. Z6 ?& w& Q - _DataPin = mePort[port].s2;
( H2 s+ d u# {! }! V. T$ @ - pinMode(_DataPin, OUTPUT); 2 o4 b7 W# o c8 A2 r5 f
- digitalWrite(_DataPin, HIGH);( a' V, Z; e6 R
- }5 \& D* j! R( v1 \! ]
- #else // ME_PORT_DEFINED7 N7 F) I. N9 d
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
& W! O6 T# q6 T9 i; K3 t5 k5 L/ m - {
0 U, p% U3 D! N) @3 I - _DataPin = pin;
$ ^1 y# V5 X1 ]# ]+ \ - pinMode(_DataPin, OUTPUT);
' g/ N. [/ c( ~% C( f5 p6 Q - digitalWrite(_DataPin, HIGH);
a! q. q$ w1 A - }
9 T$ b( Y% ?2 v3 ^ - #endif // ME_PORT_DEFINED- o3 q+ H6 f& x* W% ]! z
- # z& a. o2 G0 p2 g! V' J
0 k! l5 s+ X8 ~- void MeLineFollowerArray::setpin(uint8_t pin)
2 O* Y) s9 a: I1 \9 C - {
6 |" b, W2 i _3 G) ~ - _DataPin = pin;
% S) W4 M' B+ B - pinMode(_DataPin, OUTPUT);
9 y0 _2 l9 A* ^# O - digitalWrite(_DataPin, HIGH);
) B$ @ }- j" B1 i& y( a# e5 e& H
$ l* s1 v7 o6 b# [- #ifdef ME_PORT_DEFINED! h3 c& z, n3 Q: C
- s2 = pin;
% l8 @5 x" R9 X - #endif
" s" ] H" Y: ^2 `; ]0 j" d - }9 I& I. t9 N* C# ~7 c
- 3 ~* j, G* t4 ^& \! z
- uint8_t MeLineFollowerArray::getValue()8 F8 Q$ l9 I v6 ]4 F# B
- {
# P2 O$ p( i1 y' h( Y9 h5 g - uint32_t LOW_level_read_time;
) V. B+ {) w3 {4 G7 R - uint32_t HIGH_level_read_time;: i: _ t: L2 e- q- s+ b
- uint32_t time_out_flag;
) p% F( ]+ \) f2 m - uint8_t Sensor_Data[3];
3 L* ~% k1 M5 W! R" D e3 V% I - static uint8_t old_data = 0xff;: ~: V) E/ ]! Q7 [. y5 z
. K. a, P. H2 w4 Y; x5 S- pinMode(_DataPin, OUTPUT);
5 |0 @6 k3 P1 g2 n - digitalWrite(_DataPin, LOW);0 @9 { o6 F7 a+ i- ?
- delayMicroseconds(980);
1 x Q# V* e Q1 k" E7 ^ V - digitalWrite(_DataPin, HIGH);5 ^' V3 ?: n* V* l
+ U! _& `! L" `' E- pinMode(_DataPin, INPUT_PULLUP);
8 C4 ^0 p' W! \ - delayMicroseconds(10);
9 j) s3 }" N X% _- v - - B, }: M4 E* Y; _6 Z& |- c* |( R
- time_out_flag = millis();8 W9 A" T$ a8 j6 K
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( c" g! c2 v* R- E; ` - 4 V! b8 u5 B$ S
- LOW_level_read_time = micros();1 A/ J) h8 Y, I, g7 N1 `
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out* B! `' q' \* Q6 {. ?/ \
- {, W& H+ @ ?! X$ U4 M0 L D
- return 0xff;
3 V8 ~: h! G W - }
% x* p/ w& ^# C& |5 i6 |
/ X6 Z; z6 k4 {6 s! u) v1 h0 p# ]- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
4 Q! A6 g0 q1 L* h' S6 {6 a% S
% v2 t0 f9 }6 q- HIGH_level_read_time = micros();# U- E& q- @9 P( q( {0 G# l
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
5 Q$ {, Y3 C; h4 }$ o' m - / h3 _! L, N2 j9 j8 }$ v
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ k; E% X2 m" [- h% C - {
% b. a. O. o' K {% H/ u - return 0xff;
1 P4 Z7 h; h+ _, Y - }
; E V0 ?" ?! E- f4 u6 O# G- l - 0 n2 u( [* i: K
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))( k1 l" | e6 r7 f5 p
- {
9 T6 |8 a: p) }; r! A" A! E& V - return 0xff;
1 b2 s3 k/ O) X" y - }9 Q0 I8 Q- }2 h8 F
( ?3 y+ L- m+ |, O- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 Z/ Y' {8 B* Q( F8 c- c4 ~' ? j
- LOW_level_read_time = micros();
9 D/ W4 p( u0 y) H" P - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
( s! n& ?9 Q% L1 R5 R2 z) ~" Y& { - ' j/ s' D% c/ w+ y% b
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out1 B# O) b( b% q
- {
2 Y0 ~) E( v7 q) N5 I p. @ - return 0xff;
3 E# A6 p" n# z# P3 V# S - }2 h$ ^& b: [+ j+ z* ]% P- g( O1 L
# {" K/ g' m, L. \) u- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
4 O8 G% k) M) R5 N - {
7 ^3 i% \) c7 Q* l+ J( n - return 0xff;
) ?) p1 Q! x) y$ @% W( x8 Y - }
# t( v. `: T: z% B& } - . E, Y/ s1 M- V
- for(uint8_t k=0; k<3; k++)+ m* {1 }( O3 I% i7 e
- {' s' U! u* ?0 ]9 Q+ N7 v4 u
- Sensor_Data[k] = 0x00;) B L H9 h9 Z7 H" `% ~
3 g; _* I t9 ]! d9 Z0 R4 R/ l4 m- for(uint8_t i=0;i<8;i++)( J3 }- S; e4 @0 [5 Y" T9 M2 L
- {
: C- X5 X& p$ ^# \9 X - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level Q5 o' V3 ] y0 O* p. @. S
- HIGH_level_read_time = micros();
: `( S5 P# b B( m0 H; K7 C - LOW_level_read_time = micros() - LOW_level_read_time;! z2 @" [3 u: c* {& p5 d- J
- 4 P- |+ X( s, b" D
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )" q2 F9 t) N8 D2 B/ b6 B
- {7 k0 R. E9 C) j, R( s
- return 0xff;
( m; {- A# k$ v; E/ Z! z+ P - }
# j; P) u. B$ T6 `6 e3 Y% m/ v
9 z% c0 B, m7 ]; T/ ~8 N- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
' k, `. Z7 [) S j$ j - LOW_level_read_time = micros();, n/ [( F, i8 f) z
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
4 Y# A8 T5 S/ F* }4 |, |
+ S3 g4 Y; H1 I9 C/ I# R3 c" G- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1: |$ _* r* I" p8 D0 [
- {# k% i5 K* p2 o( \8 l
- Sensor_Data[k] |= (0x80 >> i);' s0 `* k8 n0 \3 S1 @
- }# W& I! u: c9 m1 ` x$ H
- else if(HIGH_level_read_time >= 100). S5 g9 T" J' r" p0 |6 N, [
- {8 Y( ]7 S8 _& p3 e5 ~; A
- return 0xff;
# d' ~5 `9 x: h - }
" A; |& V! x% g
6 Q6 b# q1 T1 t0 S( H+ }- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
3 i- v/ c8 P! m2 i6 `' @ - {
- D2 f8 h. [' b8 r3 V0 B3 N - return 0xff;
2 @0 G8 ~; N w" A, Z, C - }( ?; O$ `$ t! V3 [
- }
8 H a" q' O$ B- {! j - }
3 q) D1 c/ [! _( h# M
% _( ~9 O/ S* j; P! b$ q; W+ o- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
2 C% ]$ o; Q9 c- X, e) d - HIGH_level_read_time = micros();
8 K0 @+ | ]) H5 l2 D8 [ - LOW_level_read_time = micros() - LOW_level_read_time;; d* I0 u" T7 I+ V6 R4 T1 T
- + `4 u8 ]. g4 `1 o, `$ {
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )7 }% ^* Y! P0 g4 y/ d6 `
- {
, Q0 @6 V# M- P D( C. r - return 0xff;3 @! D0 u3 ^) n/ m1 r9 [2 s+ |, n$ E
- }
( g3 I& \2 E$ O/ g6 e2 V" |/ D2 y - 3 o9 a$ ]; w3 T7 P
- pinMode(_DataPin, OUTPUT);
* S% l% R/ j* o - digitalWrite(_DataPin, HIGH);
\* \3 d* i3 O
7 E; O" d1 p' R3 [- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
& \' W( j7 P2 S: R - {
$ o# Q% w/ u8 A. o, k% s - old_data = Sensor_Data[0];. p4 P6 m" e) ~) ]3 @& Y2 J
- return Sensor_Data[0];
& n- I, l3 s( J' g J6 k - }
" D9 M. f+ a8 R6 L9 _( Q* F. h! f - else
( v- _; a$ \2 ]# g! ^9 q+ x - {
5 N( q% J! U/ n1 F! e* R - return old_data;7 G" L7 `1 y5 U6 U2 Q
- }
- Q1 D- T% i& J/ C2 j3 Q+ r - }, V$ e/ X- G, h$ U* f" Q0 M
複製代碼 * I+ @9 F% G" P3 t+ A8 `
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
2 G" z# o/ |, i2 G+ q
w7 A. m$ l# Y% p6 m |
|