|
|
* ], d1 c# |4 X# `+ J- #include <Arduino.h>
K( r/ U- F# q# @2 ^ - #include <MeAuriga.h>
( V8 S& j$ ` k7 v7 o% [ - #include "MeLineFollowerArray.h"- e1 {7 s5 m' I% K2 `! K* h8 u* R; W
- 1 A6 L6 L, z1 E/ y" V% K
- MeEncoderOnBoard Encoder_1(SLOT1);
; x$ c, R0 _: s - MeEncoderOnBoard Encoder_2(SLOT2);
9 r6 I g: c& _ e* f. s - MeLightSensor lightsensor_1(12);
6 i5 G G( U5 S - MeLightSensor lightsensor_2(11);$ V% L z. w% F/ S$ h
- MeBuzzer buzzer;5 W9 I; G! A9 l$ Q |! p% W
- MeLineFollowerArray linefollower(PORT_6);) @0 P0 Y0 h" Z- m$ w {/ r1 }
- 1 i, A/ j& q+ E2 _
- #define Error1 1) E/ S! I6 b# J1 c$ v4 h ]3 ?6 |+ C2 t
- #define Error2 2* `; z7 x" v2 K4 ~
- #define Error3 3: i1 P: e( n/ g0 o1 H
' P4 t, n1 |3 K, f+ V3 `$ N, @+ X1 V- \- #define Kp 15( L% p$ k! Z) T- }5 e
- #define Ki 0.155 @7 h9 B8 I! r4 K: H3 `) r
- #define Kd 0.03% Y) F, B& h X" a/ I7 N
- : z8 x5 R6 p- c4 B' A& i
- uint8_t sData;2 G" T" o+ ~5 `" y
- uint8_t D1;2 r2 y2 J9 z, H v8 F
- uint8_t D2;
9 ] ?; N: a5 s5 |# I# g) h1 w - uint8_t D3;
1 ^3 j7 Y) @* t+ P- q, { - uint8_t D4;0 o( U4 |5 b* y# Q
- uint8_t D5;$ S1 V% }7 T) \: Z4 ^
- uint8_t D6;, c" B( K- [: m" ~7 c6 z0 P" C
~: N, a4 e% w! r; ~/ D4 N- float previous_error = 0; ]8 ^) e: @2 @% q9 A! Y" {* |; y
- float integral = 0;/ k" b" x, B+ ]9 @. G
- float derivative = 0;" R' |; {& t( \8 v
- int Speed = 160;9 F9 a6 K R% I+ C& O2 w" C
- float output;7 u) r( F+ X4 ]. e L; F
- A" {8 S4 a! y% t- byte Left;
; u: Z0 Y5 d) |3 b6 |2 h, ~. ~9 j
. d$ R$ Y0 @2 C0 Y( v/ _ k$ R# Q- void setup() & n/ c8 R1 j+ r& c5 l
- {, ?( k5 k7 N0 M$ P; M
- //Set PWM 8KHz, V2 d! a2 a: w" A
- TCCR1A = _BV(WGM10);
5 n3 ]1 z P( [4 A# G. x& r - TCCR1B = _BV(CS11) | _BV(WGM12);
1 d5 z3 A: F% I9 Z3 {7 \: ` - TCCR2A = _BV(WGM21) | _BV(WGM20);, y4 F8 X8 k' {& l3 w+ x
- TCCR2B = _BV(CS21);0 b0 r3 \ Y) K8 @9 m
- Serial.begin(9600);8 I ?' E2 a/ a7 ?2 b0 G
- buzzer.setpin(45);
; N2 V) v! M8 y% V p" l - }
2 y6 O" I. ^- l# T( m - - n3 N+ x: x/ c* w0 J" Y9 T
- void loop()
e) Z( c/ W5 b - {
8 g6 g8 S/ E! j" t! |' T# m2 O - while(!((lightsensor_1.read()) < (10)));5 A( Q3 T0 z+ r& F2 u n7 s: j5 b6 r
- buzzer.tone(1047, 500);
' M, C1 ], ~( W ^/ m$ R - delay(500);
- o% \# g9 ?; ~, O - do- F$ s i" u( }2 n' m0 v/ G
- {
1 n) \3 h# t, {( I - int Err = getErr();$ l- c$ X+ q. B0 m
- if(D1 == 1)
( ]$ k Z$ o8 t/ D0 x) X - {
9 \# O2 R% q; c& A/ a' Y4 u - Left = 1;
1 y, b% i/ @5 N/ K% j+ L2 H - }
* B: J% o( E" G9 E - if(D6 == 1)
$ Q3 o. Q, u6 d6 D3 Z8 Z0 } - {
# H( a2 b( U4 L - Left = 0;1 G% H& B: S6 T5 @
- }
/ f! O% P& c! Q7 V9 K - if(Err == 99)
7 N- M+ L! k) U* M9 a* L2 w: \, g - {' l8 q8 j5 I& e$ E
- if(Left == 1)$ I) f# g" s- T" w& h. A
- {4 `7 S7 x" G% E1 \$ w# y- V: g
- Speed -= 5;
5 P1 M& T9 H' z$ K" e - moto(0,Speed);
5 V0 d7 ~' v) e0 C0 A0 O - do
% i, j# n5 P4 ]" _5 S - {! m( D) f: x# I2 P, ?5 ~
- Err = getErr();: C: E \" R/ L9 Q3 e/ }. y. X
- }while((D1+D6) == 0);
2 w0 {" ^" @5 k0 ?& y - }
! N/ P% @4 r0 v5 v) w1 Y8 b( V+ D - else
% h' Q: c& K0 U3 @: Z+ Q5 I - {5 b- G g2 s+ ^' s( S
- Speed -= 5;
6 O t3 m# D, U& a- C m - moto(Speed,0);
( S* {4 M4 o. ?( Q. s& F/ ~9 Q - do
4 X$ w8 O1 m- P$ j, J9 p - {
* i1 W4 v3 [- Q' n - Err = getErr();
$ x. _0 a% Q5 Z4 [ - }while((D1+D6) == 0);- @& | f9 W2 Q- A G" M
- }2 V- Q* E4 c4 f6 `& S) Z$ ^4 D
- }' L% U# z$ ^ ^6 l+ t
- else
( ~+ x) ^3 a5 O4 J0 p - {
^3 a. a5 c: R7 f; I5 i5 w - if((Speed < 160) & (Err < 2)) Speed+=1;
- l% M& }# m" k1 } - if((Speed > 100) & (Err > 2)) Speed-=2;
; a. ?: o" C( n4 w, S" z - integral = integral + Err;' j" @& p- ~1 I( |4 [) k
- derivative = Err - previous_error;
$ \5 |+ t8 s, I; D% s5 |0 a - output = Kp*Err + Ki*integral + Kd*derivative;
8 ~6 X2 M& E0 E - moto(int(Speed-output),int(Speed+output));
. a7 o! U/ L0 z& n3 u, \ - previous_error = Err;
3 L; _4 O" v! d& y - }
' n8 D7 n. I+ q: o - }while(!((lightsensor_2.read()) < (10)));, Z: m) p+ d* O( u) `
- moto(0,0);' q+ `% |4 J8 ^! j; N! o6 E
- delay(500);. o0 l. d& c1 z7 w3 R, L
- buzzer.tone(262, 500);* K: i$ r4 ?% R6 v
- }( m8 D- w* D/ t2 L, V0 }4 Y1 T
- * d) c1 B8 h( G- L1 C; V
- int getErr()
/ }; I( G) C0 c1 P5 W7 O3 Z7 C - {
) T2 a9 H ?* z8 v- s - sData = linefollower.getValue();
8 t4 L/ W8 C, j H/ P# z - D1 = ~(sData>>0)&1;
, B% J1 `9 u8 b$ N - D2 = ~(sData>>1)&1;, y! N- W( L6 X9 E8 z0 x: U5 A
- D3 = ~(sData>>2)&1;' ~# c/ L U( g% [0 o% C- s' ?
- D4 = ~(sData>>3)&1;) h) G0 O% Y* E
- D5 = ~(sData>>4)&1;% g; _& ^7 W0 z
- D6 = ~(sData>>5)&1;$ ]: s0 i+ o9 ?, e( W& ^
- int downD = D1+D2+D3+D4+D5+D6;3 ?0 D; e; P3 m
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
1 j; I% H3 T2 j - if(downD == 0)
( m- o! r6 b; @ - {! G5 D( p: K- u" ^; b, _( w
- return 99;; x0 w6 o( D: O
- }
& Q% q, i4 T3 }2 y - else2 F, {8 L) n! l/ q: Z
- {0 _) e% W6 d- `, S. s
- return upD/downD;( `4 |* m, P6 ^
- }
1 {& z0 c0 @- f+ Q# @ - }# m/ t: C! s/ E' D
. }; M" M! z* X* K: N$ [* Q0 V9 n- void moto(int Speed_L,int Speed_R)) m2 z) d6 d% A& v* W) G
- {
/ A! p k, V2 N. h6 r& s- a7 V - Encoder_2.setMotorPwm(Speed_L);
9 _8 Y# V# m r2 R4 c, _ - Encoder_1.setMotorPwm(-Speed_R);
) \4 l$ R; L8 h) O* P' a# K' ?" G. i - }
複製代碼
, w1 J c7 T- t7 k1 {- ]MeLineFollowerArray.cpp D9 j, W- N& Y" ^2 B, y
- #include "MeLineFollowerArray.h"
2 x# L& n6 L9 ? - $ i* c) i( F5 `8 c
- #ifdef ME_PORT_DEFINED
% M7 Y A0 x, ~ - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
" N# @. y8 E, C& \" g5 [ - {
0 e3 V' m- ~4 O& t
- x4 V) f; J% w+ `- }
/ F# D' E: |9 P( w$ d - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
, s U/ W5 p1 U4 }1 J - {
, \/ }$ n L: t! O' `% m - _DataPin = mePort[port].s2;7 {! H5 z# V7 H" b5 I7 K. P
- pinMode(_DataPin, OUTPUT);
& I! a# m* s2 ~5 N1 h; q1 V - digitalWrite(_DataPin, HIGH);
# z+ J+ g' \% y6 {( A - }/ b) B3 {7 ^: h
- #else // ME_PORT_DEFINED
7 L3 i' u8 D3 o* k* e5 ~ - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)5 a2 ^! ~; l9 n7 ?
- {
& R2 k R& l% U2 I) Z7 y - _DataPin = pin;
. T( Q7 Q" @' l# g, X - pinMode(_DataPin, OUTPUT);
- L3 l/ c7 h% Z* K) x7 o5 u - digitalWrite(_DataPin, HIGH);
* ]# I' v: a4 ]1 V& O# t9 R - }: Z* ~: C5 N) Y; ` x
- #endif // ME_PORT_DEFINED# s$ f) _$ D& P2 O: B6 `
4 a' V$ W& g6 n/ |( C: Z$ h( ?( w- ( n; I7 p( F' q& v
- void MeLineFollowerArray::setpin(uint8_t pin)
0 V" |3 M8 T5 s# s: p - {
1 u4 \9 b& I4 [3 J8 {* s - _DataPin = pin;6 d. g/ ?3 P/ H! l, E) r3 j
- pinMode(_DataPin, OUTPUT); + O i! y6 {3 K; f
- digitalWrite(_DataPin, HIGH);
. F4 \. X! n; m& ?* u7 j
! E; [! b, G* h- #ifdef ME_PORT_DEFINED
2 L; W. k- r; u& U: U) B - s2 = pin; L: _1 b; x2 N( e
- #endif
; Q, e- h4 Z5 X3 c, Q( ^ - }
) p5 V" ]8 k3 z! r O' _# u - ' Q% B% G) Z% |" Z. I" v
- uint8_t MeLineFollowerArray::getValue()
4 R- ~9 Q6 |1 ~8 `+ w - {
$ _, k# x: z+ K# z - uint32_t LOW_level_read_time; n7 t) T1 g8 A0 ] r# i
- uint32_t HIGH_level_read_time;
$ K+ g5 r5 H& [ - uint32_t time_out_flag; k* n6 p- F8 s6 o1 a. P$ e
- uint8_t Sensor_Data[3];
! q4 J% N! r$ k" Q- I - static uint8_t old_data = 0xff;
3 ^# Q+ z/ a. H+ L* s% P
9 j+ y% ]" X# i. {! s+ V$ s, M- pinMode(_DataPin, OUTPUT);; A# z- G" y3 y
- digitalWrite(_DataPin, LOW);8 a9 n* F0 o! b6 E
- delayMicroseconds(980); U2 ^8 R8 m: E/ Y; |2 }/ ?+ T! y1 K
- digitalWrite(_DataPin, HIGH);
5 @4 U6 @* q: i; v
5 M7 V) `* [& n6 Y- X- pinMode(_DataPin, INPUT_PULLUP);4 b+ S; b1 }" a. K4 ^. a( F4 ?
- delayMicroseconds(10);4 v* U, r0 Y& p" g; ]2 x% p
- / c) Z! R4 X; |- A
- time_out_flag = millis();! Y6 }% g3 l# H: {2 O5 C
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
) u# t( E* z" { ]4 O
$ x0 ]! p+ ~8 K+ m- LOW_level_read_time = micros();' y- E8 d: Y" x0 ]" C; ^
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out+ y/ y* T, f% c, n6 W% ]
- {# I& b' V9 l2 E+ b
- return 0xff;/ L) S; x* b6 o# U
- }
, r1 f8 p9 y, m3 q$ W! L2 F6 b - 9 R" u4 i2 X( C" @# J% @
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
& F6 I5 n0 z8 C: K8 W; D/ F
) h' V- V' T$ F6 O, g8 t/ m! m4 D- HIGH_level_read_time = micros();
$ f: B# ]0 i! _" s$ C# G - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
. f& g2 K. y% i7 i$ w1 t - 2 ~4 T' s+ l, i6 h, b
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out! g s" j8 o5 o1 l+ @
- {9 S. F, t2 z" M. ~5 X( }/ l0 z
- return 0xff;
& o) S9 `" |9 j' v( I - }
3 w3 N1 R( ?8 q B - 0 U+ F- h5 R* g I: L1 v C& ?1 l+ K
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))$ O: }, T0 Z7 ^
- {
! q2 s |# [: w+ R b0 _ - return 0xff;
* o& E2 s4 L* ^* ]! x7 T - } b. }% e) } Q- l- p* h2 O
( [7 v R" D% d. M) G5 u* J- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
) q8 P7 ^* m3 u/ ?5 {& x - LOW_level_read_time = micros();
' u' {) P6 s* h% Q4 x3 t) U - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level. f! G a% C8 r3 X3 N$ o! n- [
- # ?+ U3 q2 z& j7 _& Q4 R% {2 E; [
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out. ]$ d2 q: D P: W5 ?" B* S& Q- I
- {
% W& A" ?( { }0 w% H - return 0xff;
" L, t; N$ }; N! H$ _& |( x/ W' `$ n* E - }
5 g/ x) E+ Y( Z1 ?) U8 K: L
' }: P2 i! \; R e5 ?4 X( }# {- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))9 g: @5 u6 V1 y0 w8 `
- {: I0 [3 O' r' b P; a5 @
- return 0xff;
8 q$ `) W2 N0 A0 Z3 R& y- t$ Z; N - }
. Y2 t# W1 E9 e2 I' n1 T! U
6 m y3 A0 J3 @- for(uint8_t k=0; k<3; k++)
# k* {" G! V* G" C - {
# j% V( d% v/ z. T: L) F+ G4 F$ n% q, u - Sensor_Data[k] = 0x00;
- F/ r( `% x( t - 5 Y5 x$ N. ? w2 j- s" L1 T
- for(uint8_t i=0;i<8;i++)
- G1 W! s# H5 N3 O: j( o - {
& N: p7 f. L9 H - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
! V- `: u: i8 H: O" i( q. P3 a. \ - HIGH_level_read_time = micros();: ^+ z) R7 d5 L, d
- LOW_level_read_time = micros() - LOW_level_read_time;
' f" p( h0 m& b m: [
3 o* g# O1 `& x- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )" y0 F7 ] C9 C6 h5 J
- {
8 q, D1 e; O/ ]3 Q8 H E1 g8 o - return 0xff;4 C H9 t$ o1 H& V. U+ r
- }' f8 @. h5 I. H. `. ]
: l5 Y; s$ a, m) \6 k- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
* V5 _2 m/ [* h4 `4 D - LOW_level_read_time = micros();
i! s- M7 y5 U: t - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level2 E9 e. q; B" i# ]. j, y& q
9 p0 |& c2 M% O- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
9 X# N- ]4 S: H5 P$ Z% i - {
0 v1 I9 D3 C' i! P - Sensor_Data[k] |= (0x80 >> i);
" u- D6 k; l. {) ~0 g: @ - }, A' B" T/ J( i2 D& _# X/ g; P
- else if(HIGH_level_read_time >= 100)# v: _* [& e6 n- i5 i2 Q5 W
- {" G5 t! [, X$ n0 G& j1 @5 c. V
- return 0xff;, ^3 C' x y- v! _
- }' b- l0 w8 W" Z. H
5 O b, P G& j' `- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
: \% u" w' Z* t$ ^! c/ n1 l) | - {" Q# P# S. Q# D! h
- return 0xff;
" [/ [- V; X" W - }
+ J/ |& J8 d; C+ m9 ]! \$ W5 G - }& H1 y. T, T Z
- }! D. Y" x! S9 K# L
- ( m s2 x* k6 K* S0 I
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level8 \5 h( a1 n+ H( A: ^8 D
- HIGH_level_read_time = micros();
) a* v" w4 [# O+ a5 M" W6 A/ ~ U+ E" X - LOW_level_read_time = micros() - LOW_level_read_time;# V8 ]4 U& x; ^6 L2 @+ U8 u" s
, ~5 Q, z: _: ^ ?1 K- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
' d+ H, e6 f, ~ - {
4 Q i) ~/ l% a - return 0xff;
9 T6 |4 B; T" O6 Y - }* y+ h K& R3 [6 K8 V* `
4 p3 U& i7 N q! ~" P- pinMode(_DataPin, OUTPUT);
1 v, E8 S" `2 ~0 c, B - digitalWrite(_DataPin, HIGH);+ R* ^& f) w/ A9 M, Y/ {: L
- : I* Z6 C$ V. A7 f( u* p
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))0 Z! D, c7 v' \! V
- {
3 b! Y5 ^0 l8 l - old_data = Sensor_Data[0];& w9 s0 a7 i( d. |; f* `
- return Sensor_Data[0];8 h+ k% k' y5 u/ G) L! P9 y
- }/ w) W4 l& ]2 p6 K
- else
% y! {2 i' N' {' I* q. d9 n - {
6 D4 t9 b+ k$ H/ X3 P% p" u - return old_data;
; N. N; L2 ?! w: Q2 Z - }
3 t" J- F4 b4 B# ?& @0 a+ q8 u - }- ]2 T# W7 z( g) b! |% b+ a
複製代碼
7 Y) g7 L$ ?% m) i zMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
9 O4 Q; T2 m! m) B' {: A7 J! v' |6 a% i' T3 Z
|
|