|
& x+ s' h* {% y( N! ]5 t
- #include <Arduino.h>2 Z9 R$ m1 S: Z! S% i
- #include <MeAuriga.h>( C6 e ~& q. s2 K W y
- #include "MeLineFollowerArray.h"
, r5 u. D8 Y( V H( \8 Y& P
1 b; W& \/ L8 F3 M/ j% O$ D- MeEncoderOnBoard Encoder_1(SLOT1);2 d, u) f% k4 D; ]2 N
- MeEncoderOnBoard Encoder_2(SLOT2);
8 M& y/ \ z2 p" @ - MeLightSensor lightsensor_1(12);
' |& A" ]! h) z9 S, p* k - MeLightSensor lightsensor_2(11); I% q4 i3 w X) G, P' I
- MeBuzzer buzzer;
; w% B; F' d: @3 _) f! z% o - MeLineFollowerArray linefollower(PORT_6);
) [" D. p5 U, a! t' t - 4 u( y* r( Y4 }6 ~
- #define Error1 1( {2 _6 }9 c1 k& b( n
- #define Error2 2
# q8 _3 t. z8 U+ T' {4 q& {" x& h - #define Error3 3
/ V1 c4 _/ M" ^5 g5 V
+ s; Q# L# J# ^0 \8 U- #define Kp 15" I5 Z3 t8 k4 L6 ~6 p1 |
- #define Ki 0.15, M4 M) X" ]; f& ? H' V# `$ }
- #define Kd 0.03* B4 {9 T0 L/ C' G/ q: S" A
3 C1 h; G4 S5 s4 J: e( U0 X- uint8_t sData;
4 x& c; P( G, ?2 b3 z9 j5 T - uint8_t D1;
! w6 M7 _3 O$ D/ ^ ^$ n - uint8_t D2;: v* z! k) J( _- [6 n
- uint8_t D3;
9 N- `7 i( g& m+ N- y - uint8_t D4;
* q' H3 Z7 M# |7 I( d - uint8_t D5;" f4 o7 @6 d: g. p
- uint8_t D6;
# p1 i" O3 d2 o3 K" ~" z4 E
1 {2 N7 m% v2 B5 ~+ r: ^( S" y( e- float previous_error = 0;
- R# M" m( A/ H$ R8 ]: c( s/ d - float integral = 0; m1 S2 w/ Z3 x& I! |# A
- float derivative = 0;
. }' _, ~) ?$ _1 T, F7 T8 z E - int Speed = 160;6 g$ T# C9 \% l2 l+ B4 L' _6 k) x
- float output;
* { F9 }4 d% { - 0 l1 w, e9 E- i0 _
- byte Left;
- g! \. ?, T8 L8 t& A - 2 e7 `0 v: @0 P' a* X0 ]
- void setup()
_% R k0 s" r# v# w5 n* S& {) O: _ - {
) Z1 w6 s8 N8 x [0 c" L' N - //Set PWM 8KHz
/ h& A% w/ @- B( ?( l$ S - TCCR1A = _BV(WGM10);9 R% K: \ S& Z) V) c ]2 _) S
- TCCR1B = _BV(CS11) | _BV(WGM12);! G$ Z: C4 n. d- f, \% _. o
- TCCR2A = _BV(WGM21) | _BV(WGM20);6 u6 x; _1 U% `
- TCCR2B = _BV(CS21);( v% }- K: l0 Z
- Serial.begin(9600);/ _0 g7 z3 a r+ u3 t
- buzzer.setpin(45);
# I' B* a; @5 {* S. s5 o - }
& E6 }% M# T( m4 x
$ M/ u* p$ H' I, W: k- void loop()' S( Y/ C" Y3 X/ B
- {% n# _. C. W$ w* d( v9 Z
- while(!((lightsensor_1.read()) < (10)));; Y5 u N1 }6 b& g/ h2 R: I8 I- y
- buzzer.tone(1047, 500);
/ B3 r- l7 ~/ u1 Z/ V% Q. p C - delay(500);
* r! ?3 k: `/ b# }7 ~# }9 n) Y* j - do
6 S6 q1 O4 ^% H7 o$ `! d, @ - {; N- n$ x) n& m! E9 B
- int Err = getErr();
1 `' I2 ]) y+ H% P: ~. S6 x+ X - if(D1 == 1)* u$ j5 H) R6 ^: c( N# k
- {
* a1 f% f0 U T6 h; q - Left = 1;: }3 m+ w0 d3 B/ l; i
- }
1 a6 O; v- g! f& F - if(D6 == 1)# _9 w; X* y9 E+ A5 ^8 |
- {
/ o0 D, N& x5 }. Y. h# S - Left = 0; O% _* o4 o8 @9 i# ~
- }
e0 [+ R. I$ u: p - if(Err == 99)
; e! @/ A/ d3 |! z; I/ @% q - {
+ D$ a6 V) C+ B( \, ~. N: ` - if(Left == 1)
5 _/ l4 E; a7 U2 T6 G2 R - {
; F( W& U4 r5 a1 ` - Speed -= 5;% |' X, G1 c' S9 }. E
- moto(0,Speed);
+ S5 Q9 n W6 d - do
5 e P) O* ?' q# ~4 P; k, S - {
0 \) W5 I' \# P% Z) w - Err = getErr();
1 S5 h7 x9 h4 I! P - }while((D1+D6) == 0);
, T# @% o6 Z6 R, p, E, p7 d - }
: k' J9 O. m" c7 n" o \ - else. Q7 B: L: t0 k! V8 J. }1 p. y% W
- {
M3 f' X. g9 g7 z C& l - Speed -= 5;
/ [/ X' @$ f: I) l% X4 ^: G - moto(Speed,0); 6 h9 \- u* A$ w9 f$ L7 y: g& U
- do) ^' y! K* [9 y3 d, H% q2 U
- {
( x& m! O9 M' C$ n& |8 a* m+ c/ j - Err = getErr();
# d! ~4 _1 z. I) e - }while((D1+D6) == 0);
) O. [4 [4 h6 O& V - }
9 j$ X' ~% W4 Q7 l1 d# Q - }
$ T5 Q8 l! s3 O( f0 V - else
! a2 p9 x/ F* ]2 ~1 ] - {
/ g/ t7 U& W0 E6 N* @ - if((Speed < 160) & (Err < 2)) Speed+=1; ' y e7 F A; s, l9 \0 G7 y; j
- if((Speed > 100) & (Err > 2)) Speed-=2; & O' W$ _! E% _& K) O' A# J
- integral = integral + Err;3 V! ^% J$ w2 ^: @( W0 ^
- derivative = Err - previous_error;
4 h+ }! ^2 ]! {" v! u - output = Kp*Err + Ki*integral + Kd*derivative;0 B! ]* D. {6 |
- moto(int(Speed-output),int(Speed+output));8 J* K! o+ I8 K# h$ i! A4 r2 R
- previous_error = Err;
& D1 X3 E. R( x: A. d3 h - }
" f/ _" C C; i8 f; b' Y - }while(!((lightsensor_2.read()) < (10)));
( M; g9 h% O6 d& j2 w! T - moto(0,0);
" b$ V. v3 ?$ R, v/ d9 k4 _8 W4 i9 l - delay(500);
/ M+ o3 g+ x g. T - buzzer.tone(262, 500);6 X6 j4 F5 J9 o4 M' F# N5 A7 k( r
- }
8 r. B b5 w N5 G1 J7 R - ; A/ E4 ~ Q; b
- int getErr()
6 J2 b [, ~# m9 r8 ~ - { 3 \$ t3 b2 H3 V# I
- sData = linefollower.getValue();
9 v1 w5 I& [6 X8 ~2 T - D1 = ~(sData>>0)&1;% H# T; e: \" Y) R L1 p
- D2 = ~(sData>>1)&1;( @1 S G8 B, t& X, A! ?
- D3 = ~(sData>>2)&1;
% w8 v& ` h* v% w( @0 Z) ? - D4 = ~(sData>>3)&1;
B; _7 h3 i( A* k - D5 = ~(sData>>4)&1;
# f8 q2 ]1 t1 v1 @ - D6 = ~(sData>>5)&1;
( i+ D* k9 o# k8 Z- ?4 k - int downD = D1+D2+D3+D4+D5+D6;
+ }+ X. y5 R1 E9 f( W7 ] - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);- }" n. ^7 ?# t
- if(downD == 0)! @( f* o J. n4 T; @8 g2 }) V
- {
! |$ N. j/ P. N" p; h' ~" i, @ - return 99;( E$ V r/ @. a& P& ?% _
- }! v3 i# w! s5 `# u0 R" \; W
- else
6 m. Q+ e% j0 L3 @ - {- b8 Y9 y- h) T- |9 o
- return upD/downD;
z5 \9 k! j' q1 P! S4 b4 E - }# ]# n+ ~2 b7 {+ m% R0 o
- }
B. |1 ?! _) |$ e
1 |4 E6 ]4 L0 r. f8 t- void moto(int Speed_L,int Speed_R). Y! |$ c; V4 p) r# G6 O) L
- {0 f# z5 p( V6 q; q4 N, z( a
- Encoder_2.setMotorPwm(Speed_L);. p- f6 B. P# q2 G& b( g7 G1 Q
- Encoder_1.setMotorPwm(-Speed_R);
" h2 g& m8 p" a! q - }
複製代碼 # C" s! O8 ^6 L7 W5 r
MeLineFollowerArray.cpp) A7 g; |0 {6 \: o F$ T& F
- #include "MeLineFollowerArray.h"
% }8 z, U& j6 F, d5 i8 X/ L! T( | - + t) C7 K1 Y0 W3 l3 D
- #ifdef ME_PORT_DEFINED
5 P* _0 s+ _0 {+ g - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)+ l# V, u; v' b, b T7 n0 Y: e
- {
) a; f: Y: z9 `# }8 H3 J; ]% n- D
" |; U+ x% Y, ]: O5 ^9 c+ Z- }* ]/ H' J0 G7 Z5 {) T) P( m% l: {% ^
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
+ z3 T0 L2 u1 W3 P - {
" C) |/ p. g0 {" y - _DataPin = mePort[port].s2;' t, \+ Y7 p7 @' ]! [
- pinMode(_DataPin, OUTPUT); - y! ]; V% h) Y) U- M7 j
- digitalWrite(_DataPin, HIGH);' N" C: g; D; J$ l1 o
- }; R8 P. w; Z+ P& K, V+ v
- #else // ME_PORT_DEFINED# Q+ Q7 R% o' e9 g
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
) \4 r, I }, @% T& u: ^ - {2 O4 z4 U- D6 I' ]
- _DataPin = pin;
9 q- B9 g- H+ `! e: ? - pinMode(_DataPin, OUTPUT);
- }0 B2 M8 u3 U8 ~* A/ A& W0 z - digitalWrite(_DataPin, HIGH);
7 C2 e8 w# A6 I/ n! a - }
0 e, x- b, ^' }3 _: R) a5 s6 ^& \ - #endif // ME_PORT_DEFINED
6 i$ p% }0 n0 M2 f( ^3 `$ O% M( q - 4 S8 o. D% \2 u$ v' T% c0 o
' { o6 ~$ _# t; R9 g2 D- void MeLineFollowerArray::setpin(uint8_t pin)
9 l: s$ A/ u5 E9 d - {
8 F& d5 M& {' q' Q% {* c - _DataPin = pin;) U1 ?. K. H% v: s; u/ k
- pinMode(_DataPin, OUTPUT); ) m+ {7 H2 d$ ^: q5 u B
- digitalWrite(_DataPin, HIGH);
$ T8 `/ X/ D2 P0 N2 w - 4 t; V3 y, O9 r- N& C, T: y7 b! J
- #ifdef ME_PORT_DEFINED. ^ i& b) M4 e! ]5 D
- s2 = pin;: \0 c1 h/ C9 k7 G/ t. T
- #endif2 O, @0 d' w+ G$ P) B3 y
- }
8 c8 l' `" G- p' _; c8 I2 [ - / q+ a3 O+ [$ F+ G; w) w
- uint8_t MeLineFollowerArray::getValue()! Y, @& I: w2 ~; u8 ]7 {
- {
V6 b! r d& `: s/ z6 A1 Z9 j2 k! L - uint32_t LOW_level_read_time;3 x0 X- y {8 w; `8 |( V3 i
- uint32_t HIGH_level_read_time;! J9 m! _# v: ?- ~) `0 w
- uint32_t time_out_flag;' h/ A2 |. i1 @+ I" w ~
- uint8_t Sensor_Data[3];( f) s) d1 I8 A# G3 |
- static uint8_t old_data = 0xff;
; r5 W/ R, e) b u* v5 N+ C - ' @4 ~" L" O/ b A
- pinMode(_DataPin, OUTPUT);
9 H0 a! D& H$ r7 j% ?. S( p& K b - digitalWrite(_DataPin, LOW);& Z- w' l$ A. H
- delayMicroseconds(980);6 J- ^; K$ G7 I2 f( {" o4 V: @
- digitalWrite(_DataPin, HIGH);" l& I$ b1 h- w' |) p
3 Y0 }( W1 c* }+ D6 H- pinMode(_DataPin, INPUT_PULLUP);( g+ Q1 a" }! a
- delayMicroseconds(10);8 n* `+ Q" F2 K1 `
- % X D$ e: |8 N2 H+ K
- time_out_flag = millis();
' ?) s+ W& u% C1 N - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );+ ]9 n9 j9 W! O" y% }
- 0 B! n, b& m4 w5 T+ w- s
- LOW_level_read_time = micros();
3 i5 R; B p. o - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
! t) \" l+ s4 r2 S7 {* ?" @( O - {
: P5 p0 C. O. f$ g - return 0xff;
: P' A$ a2 b4 R- O; j - }
) F: }* L* w! D) n3 p% Z3 ]6 y3 D - 4 N; i4 s$ Q* ^
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 R4 Y) D1 `4 H- {7 ^" l - 2 h' O5 \. w- V$ P: q7 a9 T3 ~
- HIGH_level_read_time = micros();- B& I4 a1 p9 H- C
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
2 ]5 r" N. m t - 2 f4 u8 i; C% W2 j7 }4 o r1 d$ ^
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
9 p1 y+ m7 J, c- [ - {
- @! m* X7 q% T' M1 \, ]* m! Z, d - return 0xff;& ~; J! R: u, Y# C
- }
! p0 b/ i* b* J9 U& H: w- r - 0 v- F" W! f |! b& I. s' G
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
2 |6 b6 A- {8 }# v/ Z - {8 j6 I7 H/ H( W! k4 U, D% X3 J, n5 Q
- return 0xff;
6 e# @5 F L8 w3 Y2 J5 w0 \7 A - }
1 `7 I9 ~$ V$ i% ^( B: M- p6 S
% S: b& } A$ L Y* N) n- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );: R8 V7 _5 k* j! G R! @& @7 r6 v
- LOW_level_read_time = micros();
5 x2 Y' R* p A( P5 |) j; { - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level4 U3 U8 j, J4 Y: w/ P* X
- 4 H& e% p7 u8 f, W, I+ Q+ l
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- {( }. x! O) M
- {
8 }' \; _8 r r$ M" [2 {7 [ - return 0xff;- F6 o" F, g4 O1 m4 i/ f% j
- }
- W' W2 G/ o' Q% {4 } - ( q8 E: c3 p% v
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55)); W# ~& y! |, ^
- {+ E- K' p& ~) h7 ]
- return 0xff;& L& Z& T; c9 i- Y9 p
- }: S* t; Y7 h2 Y; i
; |1 ^. |5 c+ ?5 p5 f! Z( w9 |- for(uint8_t k=0; k<3; k++)+ q) E5 i. {- M0 x {
- {
8 m2 k0 l0 h% o( ?/ S - Sensor_Data[k] = 0x00;
* M. C9 R$ \( f: M
- e/ i7 \- Y0 I' p1 l- for(uint8_t i=0;i<8;i++)( J C F& f; x' o1 g
- {
$ E' v/ ?) F/ V - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
0 m$ K( g8 ^' F3 Z! ` - HIGH_level_read_time = micros();
% b3 o q i$ C% n- J5 f3 ? - LOW_level_read_time = micros() - LOW_level_read_time;
3 @8 g* ~5 i$ T - 5 w+ I" p* }/ Y: l1 I4 y3 K
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )5 j7 u$ J, \ D' Y, L& a
- {
' t! T8 J0 ?! g1 i7 ~& M6 Y; l' F - return 0xff;- r+ N4 Q9 v7 E5 R; f p/ c$ l, v
- }$ F6 z/ Q- L& S" a$ g/ c
9 _( A: M+ J2 P' z- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );5 z/ C: w5 Z5 {( K: X/ }$ U
- LOW_level_read_time = micros();
5 x/ |# Z& Y4 q& {; y5 T - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
3 ~2 \$ [2 u) f - . l: J8 w1 y* \) T. z* K# ^0 e& Z) `
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
0 W- F+ b1 E$ I& v4 \- L- Y - {* [. k, l* w2 N' g/ z
- Sensor_Data[k] |= (0x80 >> i);( s, r& w7 D: X$ c, {- c) C
- }8 O# {& U! i( _6 Z* ~- B3 V
- else if(HIGH_level_read_time >= 100)
' L( @& _& u1 a- }; Q% c2 G - {
' z( b) }8 U% ^ - return 0xff;7 } \9 ~; V& s. a
- }- A1 Z) x! p7 r( ^- s% H
7 o- Q( ^9 L+ e, L- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
; o E ?% Q- V8 S0 i* I+ k( x - {
; V* J) X* R8 U0 y( A# { - return 0xff;
6 K9 H6 |& u; R - }+ D) E3 `# _# _
- }, a) g5 F% [7 M* l: \
- }
?+ g1 Z$ a) d% a6 C, }+ Q - " `$ G7 s8 p& e# [( `3 r( K
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
, O7 q1 O6 i" z1 a: e G# n" c& O" b6 r - HIGH_level_read_time = micros();
& f/ ]0 ?% `: i - LOW_level_read_time = micros() - LOW_level_read_time;
/ {0 d4 c8 z0 E/ p } - " P9 z) ~* y/ ?1 h8 q
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
" `0 |# m, D" G# t1 Q+ Y - {9 a9 K( A' G) ~: F) n3 x
- return 0xff;
; P0 U! s+ `, x3 a9 x - }
. [+ [6 V; G9 M5 \, `
: _1 b. E* d# q. P1 j6 w& w- pinMode(_DataPin, OUTPUT);) o% J! r" |5 ]
- digitalWrite(_DataPin, HIGH);
$ i; C7 t: L' `& a- l1 B* G7 E - 9 m B9 u2 A" }6 N) h8 Z
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
; X; Q8 A. {( c - {
4 |2 |0 O" ^2 n$ n4 v; O$ P - old_data = Sensor_Data[0];2 _$ I Z, k; F& o
- return Sensor_Data[0];/ A1 |. |0 {9 P( O2 k/ z/ ^) G
- }
! S; n' M, y9 `! c9 Z) F: X3 v - else8 _; {0 _3 i$ R* a+ k4 ^! k9 x: P
- {% A. ~- S/ E$ Z
- return old_data;' _" L4 m- a1 f% d
- }: r/ w- D B3 J, u& R! c: u' X
- }7 L9 T4 |& C( I6 U$ A! J3 t
複製代碼
1 u/ X0 \2 x$ K2 {; QMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
z4 ^. M8 Q: Y3 A" J4 D2 g; K* r
6 @2 b1 I0 D7 H ~; @* L |
|