|
|
& M, Q8 Y; x) W% N7 V Z6 g
- #include <Arduino.h> ]9 l3 T4 y: O6 a( z( U. L- |. f+ }
- #include <MeAuriga.h>
7 ?/ v0 m: ^/ m0 I - #include "MeLineFollowerArray.h"
9 u: P+ X5 I9 U1 w B4 I" L# ?
9 M! A# o2 B& l- MeEncoderOnBoard Encoder_1(SLOT1);: o8 G2 c4 Q h! H( d, K* i) f( U
- MeEncoderOnBoard Encoder_2(SLOT2);1 ?7 n, ?" j7 C J6 Q
- MeLightSensor lightsensor_1(12);
% U" z0 T$ d! G' v" p; b" B+ O - MeLightSensor lightsensor_2(11);
. H4 f& d6 u# }, ]5 x - MeBuzzer buzzer;
V- E; I( Z! I; N4 C T- e* d - MeLineFollowerArray linefollower(PORT_6);
% _0 I6 \9 \1 G( g+ `
: a E5 {# k+ d8 [) }: U- #define Error1 1; {4 V$ t7 q6 V. q! B3 i1 C8 W, J1 x
- #define Error2 2
3 i" M$ u/ k8 a - #define Error3 3
/ ] C* ~' o5 C - [; u7 x/ h' ]4 p6 W
- #define Kp 15
( D3 l$ ^- R, t) B+ u - #define Ki 0.15 S8 N5 n+ q0 e9 c
- #define Kd 0.03# I3 w1 K; X& E6 @/ A) X8 y
+ n& T- {1 B4 ]) H; a9 W3 E/ e& h6 R- uint8_t sData;( Z5 D9 i4 d, q; W& W3 W
- uint8_t D1;/ n3 `8 L1 p& A- r6 i9 o9 ?
- uint8_t D2;
8 X7 @" l+ [+ I7 g: F/ {& t - uint8_t D3;
1 a0 M( K: u& X! f: X" B+ p7 {& y - uint8_t D4;
- U# o+ q, d' o2 v- p j - uint8_t D5;! U k1 J: _5 G$ U4 X. P
- uint8_t D6;
5 V. O; m5 @5 _, Y - & F4 S: S! f+ C+ J; Y2 e
- float previous_error = 0;
! n1 Q: Y1 F# G! ?: z; X - float integral = 0;
6 z+ R. b2 l9 W0 t - float derivative = 0;1 y8 d0 v* r: i+ G, p% X( @; G
- int Speed = 160;- ~/ j& q) U: j; w
- float output;) u6 u% _9 V0 Y
- & F0 \: v9 p* c5 |# D. ~! M
- byte Left;
( Z- x& \$ N: _+ K$ k$ I
& n5 E/ v4 K* D% l2 | A; V- void setup()
2 S% t) t% {0 k+ i6 p5 a) \7 ?2 i - {* _, g, B3 _/ d* j( m
- //Set PWM 8KHz
7 }2 H6 r3 w) t0 g* p6 ~, C - TCCR1A = _BV(WGM10);2 N; C( z0 z U0 z3 x# A, h
- TCCR1B = _BV(CS11) | _BV(WGM12);5 z1 B! H5 N) ^
- TCCR2A = _BV(WGM21) | _BV(WGM20);1 i7 }# k* a S* }! t* ?; c
- TCCR2B = _BV(CS21);
. a" V, V( c& @* Y1 ] - Serial.begin(9600);
% F7 b- }3 g. ^5 }, l1 Y" O - buzzer.setpin(45);
' g! l) |7 H- c' `0 J& ~3 K. q, ~ - }; j- j9 w. D% q( W
8 \" j4 B+ R- t5 y' Z* w- void loop()( }7 i% }% G# ~3 W
- {* u5 j$ r3 |; q8 P* u& a. z
- while(!((lightsensor_1.read()) < (10)));
( G8 q: B1 L0 o - buzzer.tone(1047, 500);
1 a5 e* @, `5 s8 A - delay(500);: K$ n3 q* n- K
- do
2 S& x A1 h N" u - {8 z! o: a' Z( n5 T' T
- int Err = getErr();
+ Z |1 S' X6 P( N( x - if(D1 == 1)
1 x l* w. x" C6 L3 l - {
. x: |8 t9 O" n$ Q - Left = 1;% g' _; O6 M5 n1 _$ D5 I' v/ Q0 j) E' u
- }9 p5 ~" v* ?% t
- if(D6 == 1); _ R) s% I+ z9 x& h
- {/ o4 g8 L+ ?1 I( e1 e1 @
- Left = 0;
; b4 i0 J5 z- e4 I) _ - }
6 _! B8 \3 M7 P! O* P1 W) T - if(Err == 99)- Z, a4 w! g+ X- r5 ^
- {
! B& F5 F3 O9 x, C - if(Left == 1)* x S" N, l$ _& O
- {
# ?2 z- h* t% `' e! q$ t. P* I( i - Speed -= 5;: X3 A( O( [; }( Z3 g
- moto(0,Speed);# q, H- w9 Y# ?+ R
- do
1 L2 X0 n, R+ [5 z8 p. J - {
& a: x4 g( C4 `. v" G# `6 v - Err = getErr();
4 g2 ]: `1 @' ?& F1 S2 ]# j - }while((D1+D6) == 0);: H! c- B$ S: H: V, A
- }
; T5 W# s( \7 V' W d; ` - else
) _: R0 M6 W) v: j. x1 ^! q- Z8 x - {7 A6 b( n ^- E1 l- x
- Speed -= 5;. ]9 F, F8 D- I. L% G/ \5 m
- moto(Speed,0);
5 d3 M8 ?2 `6 x7 Z1 d- T - do, x7 X! {- v _+ V
- {
u; ?5 }, c2 n - Err = getErr();4 c- D t# { r! {& o" Y& ]: E9 c
- }while((D1+D6) == 0);( o: u6 l2 `2 n" S) k
- }
6 L6 j7 v; i4 y( A; V/ s - }
6 [- [& v( C5 P4 V1 \' b; ?/ s% E - else
% D! J7 Z( H2 X) o9 q7 A) R- { - {$ z+ ~1 F, L6 N& m) L8 Z! @( j
- if((Speed < 160) & (Err < 2)) Speed+=1;
: v: i' a3 D( K: i- ~. Z0 \ - if((Speed > 100) & (Err > 2)) Speed-=2; / Y$ q3 Z% X% Q6 C, {* j! s
- integral = integral + Err;
, T- @* ? p9 X# e. K - derivative = Err - previous_error;4 { Z$ F0 b- v- ]) D, e* n
- output = Kp*Err + Ki*integral + Kd*derivative;
0 l q2 q+ d6 A8 O6 E - moto(int(Speed-output),int(Speed+output));9 Y' z5 s! U9 {) {4 d
- previous_error = Err;- M$ s/ g( Q: W# I. b$ k8 i% B5 X
- }
: I( {% s5 u: i; X, @. H - }while(!((lightsensor_2.read()) < (10)));$ ^; Y" z$ l8 g# {# k6 I2 O
- moto(0,0);/ @5 P0 w; \- \/ X0 J: f
- delay(500);
3 M5 z6 o, Q8 `# r - buzzer.tone(262, 500); O0 Y8 o& L$ `% f
- }
; n3 i2 L9 D: O c6 R7 C+ j1 M - 8 j) l+ B) Z# B$ Y( R4 v
- int getErr()' D9 k$ y% ?: ^
- {
2 S/ T! h7 U9 y9 h- z" m( J% D - sData = linefollower.getValue();' ]5 X$ X9 j8 b# Y
- D1 = ~(sData>>0)&1;) B8 J) I) A3 k* A
- D2 = ~(sData>>1)&1;
7 Z1 T6 s2 F0 ?5 C' w! r/ D: x - D3 = ~(sData>>2)&1;
5 X6 s. [. x( i- S* A! [ s - D4 = ~(sData>>3)&1;# {0 w/ O; x5 `! h0 A! o
- D5 = ~(sData>>4)&1;
$ T$ D8 g/ p; X8 H- l, j% S( Y - D6 = ~(sData>>5)&1;7 G* S2 X( C2 {; u
- int downD = D1+D2+D3+D4+D5+D6;
' o: o1 }1 F( C( _2 V' r! V - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
* L& ~; M6 G+ C! K4 s f- l - if(downD == 0)
+ Z% P x3 H9 ` - {
! _+ i) s3 z/ j# b - return 99;
: R$ E) W! i8 d, N - }0 ~" {+ t1 Z$ [6 ~. z
- else, M. { _3 j5 w0 f& T' ?3 M: N
- {3 ]) b- L u$ c% X
- return upD/downD;0 U2 ?7 a. B% U
- }) m; l* K& O9 O8 Y4 {
- }3 Z+ b! |, F6 x. R/ l- x
6 M3 O. V9 \! f& |" m- void moto(int Speed_L,int Speed_R)
, m# N( Z2 h0 \1 N4 k' C& J9 e - {" T! D, l9 Z$ a- d* L5 B; G
- Encoder_2.setMotorPwm(Speed_L);# s$ o- [6 a- H# f7 V
- Encoder_1.setMotorPwm(-Speed_R);1 p: z1 D/ ~* V8 a O2 h
- }
複製代碼 ) @) c+ D7 Y+ y! H: D, P- r
MeLineFollowerArray.cpp' Y* l, m9 a! W2 }+ Z4 V
- #include "MeLineFollowerArray.h"
: w% K o7 x5 S: Q3 h - + n! M+ V" I) a. [/ H2 H0 W Y
- #ifdef ME_PORT_DEFINED/ i2 T6 P1 ]: j2 l0 P
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
/ x) ?( a) f5 ~: U - {! c6 X) _* V" Q$ ?
6 U ^; m2 y4 f0 m7 F- }* g# [) _0 z* A. s
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)- D& Q. `1 ^. E' ^9 O m
- {8 A2 E% W* B S4 v# Z
- _DataPin = mePort[port].s2;
+ I& V& H# P9 v - pinMode(_DataPin, OUTPUT);
+ _2 `* K5 y4 s7 K$ ^9 J - digitalWrite(_DataPin, HIGH);( R% w% S" o( j7 G" {8 g
- }; }& C1 l" N5 {" u
- #else // ME_PORT_DEFINED
0 f2 n8 M8 w; K$ @$ s i - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)8 Z+ U+ y' Y" O2 }2 \
- {
! y4 z* ]+ h! P4 d1 a - _DataPin = pin;1 `4 @% @; {' {, f+ U4 V
- pinMode(_DataPin, OUTPUT);
6 ]4 g8 x; e* ? - digitalWrite(_DataPin, HIGH);; K1 s4 M# Q# L7 {) ?$ |' X1 _# ]
- }1 S6 y" O8 d' _% D! v
- #endif // ME_PORT_DEFINED/ Q$ u7 C+ ?& C& y
- O( c3 A1 `. l j9 ^
) g) v- m$ t6 U7 T- void MeLineFollowerArray::setpin(uint8_t pin) `- ^( E9 p P$ a
- {" I) _7 U% O7 X p# y& i7 Y
- _DataPin = pin;" a, K }& s9 \2 p# X+ ~
- pinMode(_DataPin, OUTPUT); 1 _1 C1 V' M8 H( A" W
- digitalWrite(_DataPin, HIGH);+ N, o* @/ H$ ^* |' x
- 8 z8 [) t, Q1 H- a, F y$ P
- #ifdef ME_PORT_DEFINED! i) Z5 H6 m% f0 o, p
- s2 = pin;& r) F' ~) O( C# q, y& g5 {
- #endif
! k: ~$ P) a! J( q5 |( |) g - }
* }9 X, s; \0 H2 L( Z* z# ` - 4 {/ D& e: M; ~, [( t2 Y
- uint8_t MeLineFollowerArray::getValue(): [. w5 ^* \2 v7 y2 O
- {% d2 G- {* a! r& O7 V) z* m3 R6 w
- uint32_t LOW_level_read_time;5 d) S( L. D* b/ F. w
- uint32_t HIGH_level_read_time;
1 d0 B" e' h$ R3 b4 l* F - uint32_t time_out_flag;
5 D G) |, l! M0 {9 H - uint8_t Sensor_Data[3];" q' f( C' A2 Y! W0 p
- static uint8_t old_data = 0xff;
0 `/ Y% c9 x. S) Z; O
7 B# L% G, S" G1 w# z/ Q) V4 W) F- pinMode(_DataPin, OUTPUT);- e9 M- ~+ D, C
- digitalWrite(_DataPin, LOW);$ f( D) m @- ?8 O& L: A
- delayMicroseconds(980);0 p, h8 d3 {$ T S
- digitalWrite(_DataPin, HIGH);
, I, u2 t$ C9 s! Q' P6 b3 H0 l- @
& C9 ]: K; r6 X- p( p! D8 [( Z- pinMode(_DataPin, INPUT_PULLUP);
7 q8 {8 x9 ^, B6 } - delayMicroseconds(10);8 R" `* }2 K# [6 X) G! j0 K1 g$ M) ] j
- ; Z+ H" K4 q: b2 f3 n7 }& o! R3 }
- time_out_flag = millis();
# D# l# E0 L3 e - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
, |/ {( c8 r! P
) J% N4 H7 k0 e; g! y- LOW_level_read_time = micros();
0 A+ r1 `' p8 |0 p0 {- w+ f4 _ - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out9 [9 w) C! @- P- {6 t, S& b
- {' `. C& J9 { k8 C+ H
- return 0xff;! \, |4 A+ {5 E
- }
9 h( L) N5 \. y
) w( F8 {9 @% T- q6 ?+ G- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
7 U0 L. e1 }' _$ r/ ?6 x: C
2 X' F4 T4 B, A1 b T- HIGH_level_read_time = micros();2 ~, g9 e$ ^8 d- p( p
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
' \" P2 s3 T* |) b) z
0 D9 }2 s3 Y w9 R' C. K- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
. ^) l* Y1 s' ]& T8 ` - {; @: B- J0 p4 t3 A j E$ a
- return 0xff;
2 `3 L9 [+ l% G- P: U! _ E4 J! g - }
A! R4 i2 G% F# Q0 @ - 9 h$ u$ {6 l8 H4 F8 m+ Q4 `
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
J) ], j3 D2 ^ - {5 f9 S& l; P% g5 J- K" C1 F
- return 0xff;; T+ y; d/ ~ M8 |2 u
- }
* X3 l8 ?! ?7 a$ R6 d- G' T' U0 ? - : K4 l7 e- E3 Z5 Z/ e8 D3 K6 d
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
) s. ^7 j' Z) D3 _5 |; F) J8 t - LOW_level_read_time = micros();( g0 C! B9 R" k7 H
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
9 `8 V, v, \, y% o - 0 ^4 R$ Z) v1 D; W+ q7 h
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
# ?8 x2 T9 s9 M, J2 `& F - {
. L" R# x# I) G u/ p1 T - return 0xff;
F# m, \% ]8 L! z% K - }9 L- P$ Q$ Y* |# w0 Z3 c/ G( x8 d4 a
- " ?7 ?" M4 ]1 {1 ` X2 J' _
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
2 K* }; U, q6 [7 m$ G1 \ - {0 k4 Q6 M3 o1 j+ N1 i3 D: ~# F$ n
- return 0xff;& f4 Q; i% u2 P5 l5 j
- }' t4 f! W$ {" e8 @3 l
- ; T/ Z! O- {! R* Y8 |7 Y
- for(uint8_t k=0; k<3; k++)& R- S, v/ _- d5 Y
- {4 A+ L3 ~: D9 Q: N+ O& S
- Sensor_Data[k] = 0x00;
5 m8 _6 K6 n' o$ O
; N! M" @4 q6 V! M4 d% O- for(uint8_t i=0;i<8;i++)
) k% C4 b4 y8 L$ b- D% O - {$ x, `1 `& K5 m, j# @$ x- A/ P w' |
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
/ D, E$ ?2 X5 S3 z3 d - HIGH_level_read_time = micros();- ]1 k* D& w9 b; P* L
- LOW_level_read_time = micros() - LOW_level_read_time;+ |- u* f- I9 W
" @1 R% x& _5 W: D! k5 `- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )( v, u* p0 y3 k# [ f2 R$ n( n
- {9 A) Q: g" F# b# C/ @' |
- return 0xff;$ P* K! K' U+ b
- }
, C. N. P) y4 p9 t1 t4 f
7 w/ T" U! R5 d3 h) O# F2 d! Z% C- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );+ H! `1 ?& T; N: [" E+ } b
- LOW_level_read_time = micros();8 N4 r) X8 }& M% M9 c5 A: o
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
6 n; i* s) {- W3 f. D
3 O4 h" o( H+ F4 x$ S" i, U- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1! B. S7 z/ ]( w$ J% d" B4 i
- {
" M0 W, g4 C) ?# B4 z - Sensor_Data[k] |= (0x80 >> i);
2 I5 j5 k/ J C1 H - }$ v7 A' ]2 U( M/ R- V6 D2 |
- else if(HIGH_level_read_time >= 100)
" r$ a/ C* _- {, h! D! I - {/ `# s! n: h, e
- return 0xff;
+ S. _0 r/ d5 i! n' |9 i - }* g9 ~9 g' Y; W$ _
# O3 H6 ?7 v1 e; x* o( g" l- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
' m2 h- _; |/ c6 W7 P& f - {
6 x1 x' {; k) d2 ` - return 0xff;
8 x4 F9 {7 ]# i- x3 @+ H - }
8 J5 I: X6 M( e/ _ - }- x, z4 N9 \+ O& e5 s, s+ d: ]
- }$ X8 o1 G' n S( D; i. D: o
- 5 i0 {+ r) r, `% w7 s
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level; f3 C8 @. O- d. V: o" t! c
- HIGH_level_read_time = micros();
+ _7 C1 l- W# X, g, w" j7 N - LOW_level_read_time = micros() - LOW_level_read_time;
- j% x/ I3 k6 ~$ e4 n* P
7 b) y3 P3 N. [; l" ]: M2 k4 n( S. i- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
$ m5 t2 {3 C5 Z' ] - {6 |1 G9 u2 E' T' q) p6 B
- return 0xff;/ ]9 f8 I# f2 z5 l9 E; w3 k: G/ _- o) m
- }
0 n u* y2 l+ f+ ~, l, X8 _$ W - $ U# S5 i* O/ k
- pinMode(_DataPin, OUTPUT);
0 a9 f- C! h" ]( }5 R' L - digitalWrite(_DataPin, HIGH);0 U% v) H% A* s: P, N ]' k) }
- - _7 @5 d# m( n! `0 F8 L! E
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))- B3 |4 G- |; W- o" e3 Z; M
- {( i0 ~0 j, W8 V/ S+ e8 y) _+ ?
- old_data = Sensor_Data[0];
+ R% }1 ^! b9 x3 b6 S+ B - return Sensor_Data[0];+ e8 G- u g/ I9 z" X& e
- }
" m3 z) M! ^5 Y4 ]: z# j6 _ - else
9 `) [$ x$ a+ A; J - {* c1 t- [* {+ i
- return old_data;4 d9 [% j# o0 M, E# \
- }
( E; v4 v, I/ z0 x4 P - }1 c" Q2 E G6 B7 }% d
複製代碼
( R7 R2 \, E! Q& t/ {% ^7 m. w3 lMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
: }3 I) R1 k$ |9 {
& _* c9 X) q. {% W: H( M9 t |
|