|
|
$ D/ j5 I+ E5 D- h- #include <Arduino.h>' p& H6 F, A3 [* D( [! U
- #include <MeAuriga.h>6 F4 B) s! M: D( L: c
- #include "MeLineFollowerArray.h"9 G& ~8 x% U: i+ k% C5 W: |
- " L5 k9 @$ g* m; g0 Q& Q
- MeEncoderOnBoard Encoder_1(SLOT1);% |- z+ \" }$ f7 b4 Q! o6 k1 Q
- MeEncoderOnBoard Encoder_2(SLOT2);
- d7 x6 U, |) |4 u - MeLightSensor lightsensor_1(12);
7 f# b' [; ^( s! ?* C - MeLightSensor lightsensor_2(11);
! Y& Q' W( {% |' Y - MeBuzzer buzzer;0 ~7 q0 F% F2 h% \! I
- MeLineFollowerArray linefollower(PORT_6);. @. S, @: u, r
- 7 J" G& U3 ^# W0 R
- #define Error1 1
# P7 n6 m2 @. }/ V' P - #define Error2 2
" a* K9 k" N8 |- L/ b4 } - #define Error3 3
" o3 Y# M$ z0 `- N+ I - : G4 H, e: [& }6 V6 b
- #define Kp 15
" k- {( H. ~ k8 k' k - #define Ki 0.15
- N3 ?1 l& R: t+ N& ?# t" ^: r - #define Kd 0.03& @! @9 x0 \0 u4 A" u5 j
0 m8 ]! f% \8 ]; K. j0 f- uint8_t sData;
) R" H! Z4 }; {, @) ^ - uint8_t D1;1 e0 P( y/ {3 g' L9 j
- uint8_t D2;
8 J: B' u/ Z/ Q$ o$ l - uint8_t D3;+ r$ B) j2 w3 Q0 d
- uint8_t D4;7 }& z, p/ G" }0 d7 `8 j! v
- uint8_t D5;* ~. F" E1 G& w6 H( {7 K, |# o
- uint8_t D6;
( Z, w3 R% j |
$ j" V, I4 g7 W/ D+ G" U% M- float previous_error = 0;; @' b/ \0 ?! q, b2 L) t) d
- float integral = 0;7 s: y. _/ [) X1 x
- float derivative = 0;
6 M# J H- }* e8 W9 a7 g - int Speed = 160;
8 y1 a, Z0 r3 H( N- p - float output;
1 G& X7 f s2 X: ^' u - 4 i0 G4 o) A; E T' q
- byte Left;3 a, u0 H/ W$ X/ b1 j& c
) S4 i6 m! `+ ^- l' W- void setup()
# o! U" c$ a8 B1 S' S# [ - {$ R- D' `- R ~
- //Set PWM 8KHz
5 V! I3 j7 E' f) G4 l8 q - TCCR1A = _BV(WGM10);3 o* ?+ C- W6 r) J
- TCCR1B = _BV(CS11) | _BV(WGM12);
& V2 f; E. L z* ] - TCCR2A = _BV(WGM21) | _BV(WGM20);% |8 J6 ~. x- }" N" I2 f
- TCCR2B = _BV(CS21);
7 M P9 C+ {$ D0 f7 V& C - Serial.begin(9600);- ~3 S' C: w7 G$ S) B
- buzzer.setpin(45);8 k1 a: S, P5 z1 i \/ z7 k/ w7 D4 t$ R' y
- }$ X3 k4 V) O, x# V* X, i
- , e( H6 q) \5 y" ^( g; X5 ?
- void loop()/ o' y4 K& k* H3 n& |' a
- {
. {* i/ w& g+ E: Q - while(!((lightsensor_1.read()) < (10)));1 ~0 G' H5 V/ R- Y; j4 z
- buzzer.tone(1047, 500);
% S& q, J" F" e6 x" o5 O3 i - delay(500);
! _+ B* [5 p2 d1 ~! B1 P - do7 @' O) S* h4 R3 h, d
- { z% C2 R8 W7 r/ u: T( ?
- int Err = getErr();
% U& R+ t8 F. \$ B3 t' l0 ], D7 F - if(D1 == 1)# H/ L- e6 c' E4 J6 T
- {
3 [! S: i# C$ d4 d$ X9 j% E! ] - Left = 1;
0 E( p# G. }- J) G - }
+ j }9 }3 A/ g3 h X/ i( M - if(D6 == 1)
5 w2 t' y) Y5 e7 Z - {! I. \ [9 C0 I1 s- R
- Left = 0;2 o7 \2 w0 j/ s2 L- a4 z
- }% k: P+ @' U7 W' h6 m/ p1 _# Q* ~6 R
- if(Err == 99)
1 M3 H6 w3 t+ c. V5 s - {
: W8 u: w! y% w - if(Left == 1)
$ D3 m( x5 ~" t0 T! } - { ]) u& Q4 X3 n
- Speed -= 5;) m; O' s8 s. j% M* m9 a
- moto(0,Speed);9 m# w1 W0 e' s$ A8 \) s) g& T2 {
- do
" ~% I' P; E, w6 v - {; ~, Q$ @2 V- t
- Err = getErr();- [. X2 X6 _% H( ^
- }while((D1+D6) == 0);
. f% b: [3 E, s5 p - }
4 ^) N% X& Q3 }. r$ I: } - else4 c! D) Q5 f8 \0 D
- {
% e. D* @- u2 G6 d$ b+ r# J - Speed -= 5;
1 a* H: `% O/ |0 A/ g) ]+ U, x2 Y - moto(Speed,0); ( {& {/ _4 D5 K8 t
- do, T3 K9 b' Q* R0 G/ m
- {& E" R. S8 o S) D! o5 D
- Err = getErr();1 I+ n- C; I1 a/ e u$ j9 Z+ i1 I
- }while((D1+D6) == 0);
: u7 r/ y1 t1 d' u - }
4 [9 |$ @0 D8 g5 W$ |" h/ d5 ?( I# P - }
2 T. w( f4 S- [ - else
0 k; i+ X0 _; k! ]3 S g - {
; C" ]% i! p; @' K+ J - if((Speed < 160) & (Err < 2)) Speed+=1; l, g. V- d; c: V( t5 N. C
- if((Speed > 100) & (Err > 2)) Speed-=2;
, A) R0 l' h4 G# H - integral = integral + Err;6 w" S0 Z0 Q' d" a
- derivative = Err - previous_error;) b- k2 f! k# Z+ F( U; E; z/ X
- output = Kp*Err + Ki*integral + Kd*derivative;( E: R" \' |7 N a
- moto(int(Speed-output),int(Speed+output));$ Y6 i4 B5 B0 @6 O6 |3 I* s3 X; Z! @
- previous_error = Err;- ~5 t. H7 U# a
- }/ ~* H7 @1 v/ h/ y" U! E% `
- }while(!((lightsensor_2.read()) < (10)));; N3 E1 L, M1 T) p5 t# P
- moto(0,0);+ Y$ N& U9 {6 f3 A$ o
- delay(500);+ k; U' I/ Q ^% [: z3 |. n
- buzzer.tone(262, 500);. v4 \7 t' R" H ]
- }
; ~5 [9 y; f- s j$ h( w9 F- {7 ^) U - & ]* H) [" `9 U7 B3 m9 M; W) e
- int getErr()" a" u1 u" k X
- {
' t- S( u, q) G( e% M e - sData = linefollower.getValue();
6 h: s$ d: X% [, ^$ A - D1 = ~(sData>>0)&1;$ Y' @8 ^% C R+ v/ [5 w7 p' @# o7 u9 p
- D2 = ~(sData>>1)&1;: y: | F! o9 N* @, b
- D3 = ~(sData>>2)&1;; W# }3 H. |2 f& A& |& |9 I+ S
- D4 = ~(sData>>3)&1;* p: U& @3 j5 [' w
- D5 = ~(sData>>4)&1;$ w* k. R9 O0 j( |) G+ [% V
- D6 = ~(sData>>5)&1;3 N9 X5 R# a3 _$ M
- int downD = D1+D2+D3+D4+D5+D6;/ G* X8 z# E5 m3 ^
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
9 x) ]- {# g2 U5 E' [0 Y - if(downD == 0)
9 J8 ^; C7 M5 ~ - {( k% l$ x- ~& o
- return 99; q3 I% Q1 o' F
- }# ?7 z9 [" M9 _: | A
- else5 R/ [, R3 h" d6 U& f! x$ v* D2 g" x1 x
- {7 q6 W- j, S0 R! p: z h
- return upD/downD;
" D: l1 X: v4 {8 u# B1 r - }
6 @4 J' ?5 D$ M7 X" c. S% X3 v - }2 g6 n3 u4 c I- N7 @3 W
- + b+ i- ?, v: [0 T5 i3 N6 h8 S5 R
- void moto(int Speed_L,int Speed_R)
) G9 P, c% ~0 Q% o- _4 B( `5 q - {( d a; {% R) ^8 z( `8 i; B- y
- Encoder_2.setMotorPwm(Speed_L);
. S' v7 K! y" m - Encoder_1.setMotorPwm(-Speed_R);
3 N/ s2 C' X" G/ Q# D+ m - }
複製代碼
0 a2 S! c% M7 ? m0 IMeLineFollowerArray.cpp1 M0 k# b( o' G- z4 \9 p
- #include "MeLineFollowerArray.h"
# h5 X1 R! O# |! y' O( d
$ D2 W8 W3 ~: M! y ~- #ifdef ME_PORT_DEFINED) M8 S3 ^( P7 I6 m1 D0 t4 j
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
2 i( W" b# L' E! l A3 b( L - {
5 s0 j* O$ V& i U3 u0 u9 G - ' ]- R4 E: t- [; \
- }5 J- E4 u+ j- ? Y4 ` E& Y' }% b1 C4 p
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)/ _! ]/ D @& I9 `
- {0 C# w( C- L$ r) h4 Y
- _DataPin = mePort[port].s2;
! Y4 Y, r3 Y) C& Q } - pinMode(_DataPin, OUTPUT); # i, E9 g4 Q! z3 P, ?
- digitalWrite(_DataPin, HIGH);
6 @2 V4 d. ~! T. V8 v - }
+ r2 V- c+ y; P( g, } - #else // ME_PORT_DEFINED4 u E/ I5 U9 ~4 M# v6 M
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)4 O) G( C: m8 t) Q6 C% i% h! _
- {/ z; n; ?1 L1 o0 p
- _DataPin = pin;+ P5 q$ _3 I" C- w
- pinMode(_DataPin, OUTPUT);
$ e! F* S1 U6 Y) z6 c - digitalWrite(_DataPin, HIGH);
$ @: h5 o# U7 r. ~5 p+ U - }
( W f* j! z, A* U L - #endif // ME_PORT_DEFINED$ }9 Z7 P8 P, [
7 f2 B3 Y0 T4 ?. Q- $ t: B9 J" n6 S# W0 G- F/ V7 p
- void MeLineFollowerArray::setpin(uint8_t pin)
3 [" V, Q4 U% k, c - {# O, Q. t& i5 H7 m# X S/ `
- _DataPin = pin;) g) ~+ u; C( }. u
- pinMode(_DataPin, OUTPUT); ; ^, v" ]: I% ?5 k* \8 C
- digitalWrite(_DataPin, HIGH);
4 P6 x2 c* x6 r3 ~ - 6 X/ |& u0 Z2 o4 A
- #ifdef ME_PORT_DEFINED r# ?1 @7 D2 l( u
- s2 = pin;' J6 ~! [- e- F, R
- #endif5 Z9 _' F3 h# d! D* V, o
- }
* ^( i$ H% G6 r# C% ^* n
& r, E- r' h" A: E# ^0 ]/ ^% [- uint8_t MeLineFollowerArray::getValue()
7 U9 d" O- Y7 |' h6 h - {
! j, [/ Y6 r; p& b! I" ~9 H# F - uint32_t LOW_level_read_time;
) o8 v0 m7 o1 p% U, g/ J5 z. i9 v - uint32_t HIGH_level_read_time;
6 S U4 |5 C6 J8 [ - uint32_t time_out_flag;
' M/ Z6 e; u. [( s7 e9 b - uint8_t Sensor_Data[3];
2 s+ m6 w5 H( y( [4 m+ c - static uint8_t old_data = 0xff;. j% e; A' L/ u' z
% r( \+ f1 R8 ~% I" O# V3 h. I" O- pinMode(_DataPin, OUTPUT);; u O+ D) ], B" V; p( s* x; p/ b$ x
- digitalWrite(_DataPin, LOW);5 P' _" v! }) ?- V/ t& g
- delayMicroseconds(980);, e5 s4 D9 A0 q) V# m9 u- M+ ]' e
- digitalWrite(_DataPin, HIGH);3 h9 ]4 e6 J. p
. W" T& z# W e; n) h! u; Q" W- pinMode(_DataPin, INPUT_PULLUP);( w& K8 _$ G) L# g2 w( ]
- delayMicroseconds(10);
- P3 u( w. T7 p" ^
8 M& X3 H% |! K; o- time_out_flag = millis();+ d! s6 B9 y' R7 ^; e& T% N! H
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
: n3 g: A T2 i9 A: @0 G+ ^( q* Q - ! \# @* Z. r: F6 ?# ?8 y" }. e$ b3 j
- LOW_level_read_time = micros();4 f: E3 F) I# k/ b
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
" |. B2 g* B6 I$ [3 ^, { ] - {
5 \& D1 p$ }9 |# \0 ^" l - return 0xff;
5 `) p" {9 n& q4 b, @ - }9 }: V6 O- Y- j7 m; W" k# N8 i
$ K) E8 h7 R1 R T' s5 ?; Q( T- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 a9 l( n2 h v5 Y
. R! _7 x( H- o6 ]/ c* l- D$ f4 s- HIGH_level_read_time = micros();% K+ @- G* Y! o: c% F, v
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
2 c2 G% }+ y3 X; I' C3 S - % w- b+ \/ F) f; \1 t) \
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out9 |0 q: k7 c% }5 u0 n% H
- {3 v8 R( T% s9 l# D) s0 O3 I7 ?. O& W/ L
- return 0xff;
# O q; [( c& ?! H' ^ - }
1 |& w' Z( U5 G4 b# k
7 P- w2 S2 p3 c" F( `8 ]- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
2 a, X& t P3 v; z: u# l# e o* m/ J - {
4 C8 g9 n5 h: ]6 C/ M. j3 U& j - return 0xff;8 D6 `( h( ]. l- w/ Y Q5 z
- }5 J6 p# D' O/ q n, \+ {2 m. t+ ^
- 6 n$ V- M: f/ M! o
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( E- T7 N S# s- n8 M
- LOW_level_read_time = micros();
F+ J; z2 {1 z4 ^6 J/ ~6 [ - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level3 p* {! x, X! p* H9 C1 |
- 3 D" j5 A5 b* n4 @
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out, \( M' t: t) z" y5 W0 L* j
- {1 q$ O' F$ m n5 [
- return 0xff;2 ~: F) `" D9 m& J: b
- }6 d, [3 u4 n! ]
( D! P) ~. j( ~' j- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
$ P! C$ a9 _- l9 d# Z A - {- H/ p: h" H* j3 _% U$ e7 l! ^, h
- return 0xff;
7 [: q: C" I0 ]5 B% @8 [& T - }! J$ o H' A' ?! P Z$ a" [5 g
4 a! b( m: I1 t7 J" H/ `3 R9 B- for(uint8_t k=0; k<3; k++)
" n6 U! P3 u: {' X% L5 A- z - {5 M$ t% e0 m/ W. P) f
- Sensor_Data[k] = 0x00;
2 R+ ^, w% K- ]6 E - 1 }8 K! F- n: s9 ?( ]) G
- for(uint8_t i=0;i<8;i++)' n2 j7 C# w, J% f; h' s7 X- a
- {
- G+ M# l- o6 i* F; A- ` - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
& P5 ]& M2 O5 U - HIGH_level_read_time = micros();4 S( Y/ d! n6 G3 K
- LOW_level_read_time = micros() - LOW_level_read_time;
7 C; ]5 v) Z) h( G; J9 E
8 f# [. j: l. N7 |- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ Y, g5 Z9 x0 d) i5 g1 W: P* T; g
- {
% V7 y- K: G, z; | - return 0xff;# R! P* P. ?$ f, Y, v2 h [
- }7 n- x0 }" Z4 j8 ]9 G
: H- r0 p2 |2 n. Q: v; l- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
% q/ z7 s" Q5 m+ K( e$ K - LOW_level_read_time = micros();
3 I" n3 V+ {. K' Q - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
9 @/ ^7 n& r! k0 P/ T. j
& c1 y3 I# e) O* n% V! J- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 17 D- \5 {$ q( p
- {2 d, L/ Q# }: g% a) o% p) q+ O
- Sensor_Data[k] |= (0x80 >> i);
3 G* G B$ j! I- A. B - }% o4 `" E+ y2 p. x
- else if(HIGH_level_read_time >= 100)
6 o4 I$ y( @8 m& N, f4 R. h1 V: | - {
: { o: ?9 u) x2 @ - return 0xff;( E% t5 ~9 x) K1 n- r/ J0 S
- }6 {$ w/ n5 g. L( r, W/ z
- ~' D7 ~8 ?! x, I5 s* C- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)7 K" Y! N. p6 o' ]6 x% Q7 e
- {( n* \5 k& E* `* a6 D
- return 0xff;
; a& h$ H$ z- R1 P! u1 @ - }
% j! K" b- B' u6 Y - }
; ~$ Y. p9 p! A" |- x: r. C - }5 D8 @' I9 U% g% M! v, R+ s8 H
- 3 f e! I/ A$ {3 z1 w
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
- s1 S1 C+ b2 ?4 v6 e, x - HIGH_level_read_time = micros();
6 R' \6 T. [% o* P2 a* Z - LOW_level_read_time = micros() - LOW_level_read_time;
$ v( J( Z* n5 X3 V% `6 G; h - * S' S2 ?2 f+ \' s
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )$ H9 g6 |% w; v$ r
- {
( c; i- T5 j+ t - return 0xff;) ~3 c9 \) S2 l- l
- }/ \% u& a. M& x
& M/ m' h0 W, L+ m4 I- pinMode(_DataPin, OUTPUT);
$ [! n+ r1 \ o7 H/ ~ - digitalWrite(_DataPin, HIGH);" l: V, Z+ B- H& A6 _% F9 o
& m. `, X9 R/ r; E& ]- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
4 F V/ T* m' D) I - {
4 ~! V( V* l* g& a& o2 p6 k% h - old_data = Sensor_Data[0];
7 p0 v! @4 G* Y/ h - return Sensor_Data[0];
. [( ~/ {( g. Z - }
) I8 D* K6 W) ?/ T - else$ ?% \* K. S9 D4 d
- {' C3 f0 i1 F6 `- Y t* z
- return old_data;
' M: ~$ `4 u3 ~8 z# x/ ^5 [& I - }/ J# s5 y) d( b* c) Z; d
- }* E* W; A* w& d+ q- {1 q
複製代碼 1 |: W# @) Z9 `! ?2 M! ?. q8 g6 m
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
: N8 v* c4 v3 ]5 l
5 y# p5 }& X8 ?. u5 E6 C |
|