|
|
# D0 r% u% t6 L+ ?- #include <Arduino.h>
1 b1 J0 `- g, x2 J d - #include <MeAuriga.h>
1 e! b9 C- z* C; E - #include "MeLineFollowerArray.h"' M- O1 G5 Y0 N: j }2 G9 L, L
- 7 Q* u& b( U0 x/ z. \3 _+ e$ h
- MeEncoderOnBoard Encoder_1(SLOT1);# v8 Z4 M( G. t' L8 W
- MeEncoderOnBoard Encoder_2(SLOT2);- r/ H/ J, a4 `+ v$ n; o* |
- MeLightSensor lightsensor_1(12);& i: e+ F2 f/ }6 i
- MeLightSensor lightsensor_2(11);3 J8 d, M9 j, W% Y b7 K# I
- MeBuzzer buzzer;! U! h/ Y8 F* C6 {/ b5 p- h
- MeLineFollowerArray linefollower(PORT_6);% H9 K S) B1 ^
8 U3 Z& N' ~5 a7 o+ C6 ~3 g" f$ r9 m- h- #define Error1 1
. ^$ _. \; ?9 a) N1 h( k - #define Error2 2 U+ M0 f& r; _2 i: Q
- #define Error3 3
/ j9 V. k0 M8 \5 R' h! L+ I
; H% x0 B4 ^( k( [- d; @$ z9 j' A5 J- #define Kp 15
6 v K7 L* m+ z& t - #define Ki 0.15% w- ^2 H9 g* ^
- #define Kd 0.03$ I2 i' A! W: N, Q% N o0 d
4 X/ M6 f1 f9 b7 G' \6 K) c6 ] V- uint8_t sData;0 @7 X3 Z* D4 I; T* Q) `2 z: ]/ f
- uint8_t D1;0 M4 ?" y0 @2 U* R
- uint8_t D2;
" w! U9 t5 t* l0 S9 K9 ] A: [ - uint8_t D3;
, w3 S! M e. U& L" J - uint8_t D4;# h) y1 M! S' M8 E) L% c# J) Z0 q
- uint8_t D5;# a" h% D2 Q* G
- uint8_t D6; \ s, c* {" ]/ b
- 2 g/ d9 n. z) w: P* t7 s
- float previous_error = 0;0 @8 J' R6 a' g
- float integral = 0;3 i- N$ _& l$ P$ H
- float derivative = 0;$ d. }7 \' |* l0 X! @% P u
- int Speed = 160;7 i+ y P! x$ j
- float output;
$ a8 U9 Z/ O8 f* k - - n# e" F6 ~* B
- byte Left;) T$ K" j& ^# O% g3 [- Q- t
- 3 \ v$ C# a% c! z. S% [$ ^* a
- void setup()
9 z1 u- ?# B; C: ]- r - {$ @% u1 @* m8 a- j8 ]* p
- //Set PWM 8KHz; M( c# x8 x9 M; i
- TCCR1A = _BV(WGM10);- z- E H# {6 R. n3 ?
- TCCR1B = _BV(CS11) | _BV(WGM12);* G/ C+ C8 n- \& c+ ~3 W
- TCCR2A = _BV(WGM21) | _BV(WGM20);
& p6 r& ]% }- ^/ w0 N" s8 H$ [ - TCCR2B = _BV(CS21);+ x! u X, w* P; e" p; G9 M) Y
- Serial.begin(9600);/ D* @' a' @2 C# @
- buzzer.setpin(45);$ E- ~/ T* X- B8 C5 i/ P9 f
- }: n% L' w4 k/ ~) X
% D. K9 E" w1 b+ u m0 d1 K- void loop()
1 U% u( @7 X2 b) H; D) `+ [& X) o+ ^ - {
6 Z8 }/ S1 H/ F* T P W - while(!((lightsensor_1.read()) < (10)));7 I+ m) C. H0 m+ I! H
- buzzer.tone(1047, 500);
1 Z; ^ b6 w2 m - delay(500);. e1 p1 q% V2 ?+ V! t$ d
- do4 m* G( P( W6 K* N* h2 t! t. U$ y
- {9 ]) h9 t o4 N" g6 N' O- }
- int Err = getErr();; j3 @" e Q# ~/ F8 ?* k
- if(D1 == 1)1 R1 m# Q) J) G3 u
- {
3 m$ H3 B7 G ~* d% I. ^. B2 D - Left = 1;6 f5 ~ v; s4 y
- }
: m% ~, \5 X b# t1 H - if(D6 == 1)' F/ V4 B6 R8 c
- {8 F" f! W9 d) a4 [: i! M, y O
- Left = 0;
3 b- ?% v% l/ V8 B! W5 B - }
* |- D+ c! a* P9 [3 W% ~0 b6 B - if(Err == 99)1 g% p+ G+ }% g8 ~& j* ]
- {" c& ]9 l* j7 G. y# b+ K
- if(Left == 1)0 l: i6 N9 f, S$ L$ L# Q' ^ o& ?2 X
- {: ?( s$ m9 |" f7 N. m/ }
- Speed -= 5;
% a: A1 h9 f6 h) O1 V - moto(0,Speed);0 r$ P& T: l' }, L6 l
- do
9 x3 Y$ z5 o$ F& \" B - {
; L B4 ?% |3 s8 b - Err = getErr();
+ j7 @5 ^0 T' v# k% ? - }while((D1+D6) == 0);
; q9 L/ {* @5 l: a1 l - }8 D4 l# y ]4 t/ S x; m" `/ n
- else8 I. ]3 F* c" @) B7 i5 S; H
- {
2 u/ X' g3 k$ z' U) L, _7 @ - Speed -= 5;6 N( f4 V" Q( Q* D
- moto(Speed,0); & p6 n2 F1 E2 G% \' Z* i! s0 D6 d
- do
: G- H# I: m6 e, e - {
# |9 ^7 N7 j7 ^5 ~/ {1 z* k - Err = getErr();( u2 l# R* e7 q/ o; h* D( ^+ C
- }while((D1+D6) == 0);
5 ?5 t) x9 z" Z - }
6 a) A) ]/ \! E0 @" p( `0 m - }
6 j$ f9 L1 @9 [, R% T, x+ \ - else/ s7 k7 n+ M. ~$ Q9 e q5 V; w* K
- {2 h4 ?9 z: b& ?8 y; ?
- if((Speed < 160) & (Err < 2)) Speed+=1; & E9 t4 ~' I6 v3 I" `/ W+ _
- if((Speed > 100) & (Err > 2)) Speed-=2; , d9 U4 @* B f4 @9 l
- integral = integral + Err;
, r) W* v. p: t8 N - derivative = Err - previous_error;
5 I' j# Y' t) d( L0 `# s) E! { - output = Kp*Err + Ki*integral + Kd*derivative;
! C3 _7 }/ q _: n - moto(int(Speed-output),int(Speed+output));5 o6 I5 T, `6 G; `- D
- previous_error = Err; O6 G$ S. b# [
- }' H2 n) f3 Z% z4 H
- }while(!((lightsensor_2.read()) < (10)));
4 `% J+ v% z+ O4 G' p! C - moto(0,0);
8 x/ C) U1 k# ?. F1 I+ x- E - delay(500);1 u/ u4 a$ u6 t3 j
- buzzer.tone(262, 500);
8 a7 k) n9 o* a* f. J - }
% K0 ~3 C2 H p8 O
( p; M8 h$ b; c6 q5 X7 T- int getErr()$ o9 Q! _8 M6 s
- {
' B3 d; ~, D" ]9 \0 z L( ^" K - sData = linefollower.getValue();
! y! j* V0 o. ]) t( g - D1 = ~(sData>>0)&1;# F1 f, ~6 j1 _ o" e: {# e
- D2 = ~(sData>>1)&1;
* y1 w" a: c2 I. | J7 }4 f - D3 = ~(sData>>2)&1;$ s4 v: B2 C4 Q, a- R
- D4 = ~(sData>>3)&1;4 T% }5 U4 R9 E6 o) I* q
- D5 = ~(sData>>4)&1;
4 _1 I; D% A, V q$ t' e# T3 ] - D6 = ~(sData>>5)&1;
2 j) t+ p4 {) o - int downD = D1+D2+D3+D4+D5+D6;
+ J9 l5 Y# r) i - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);+ B$ c9 A( ^! n# Y' O
- if(downD == 0)9 I g5 |! _5 w. o: m. Z. ^0 f k
- {; U3 a5 X& [. I6 ?8 p
- return 99;
% }" {& X* m; Y8 B - }2 f4 g0 _) x$ P( \
- else
( v, B; O# y5 d9 e# ?& O - {
( k* ], P% i% ?0 D - return upD/downD; W2 _. m) c8 q0 A _
- }$ a- y1 ^! I2 e9 S6 U
- }
$ o! O3 j! d+ @( F& i - ) W3 R$ x" H u
- void moto(int Speed_L,int Speed_R)0 e1 B. a+ t0 A- N8 _; v/ l) @
- {. s$ s7 T6 O$ n% B* j
- Encoder_2.setMotorPwm(Speed_L);# O- l2 u/ [ i
- Encoder_1.setMotorPwm(-Speed_R);
) B9 b/ z5 w% U: _9 S - }
複製代碼 3 K# v" ~6 x/ f# ]4 p5 v+ L" P
MeLineFollowerArray.cpp
/ Z& y1 a. W& J6 t# L1 O W- #include "MeLineFollowerArray.h"7 ]& @ B' G' w$ C
- ' w4 b' n% w9 t3 U, c
- #ifdef ME_PORT_DEFINED
5 ?- q# S6 ~9 M; {6 S% U$ W - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
# L* b3 x {, C9 Z. U9 x - {( d& l2 s7 u( q& r0 ^. C" S
- G5 X n- D$ _7 J' W6 q0 ~$ m- }
& w* V7 {, O. d: G3 H, ? - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)* f$ u0 E O2 a& x1 S) J
- {% e& r( d" j! l5 O- z
- _DataPin = mePort[port].s2;; l" b Q1 O- z4 `. O/ e- x c( j' G1 Q$ r
- pinMode(_DataPin, OUTPUT);
( m$ Q8 o) k% F# w4 G( L' j/ H - digitalWrite(_DataPin, HIGH);
- U: x: [0 w1 l; j% W - }
4 X* j0 v! {2 q" B8 x, e' y# A - #else // ME_PORT_DEFINED
7 T: J% z! g$ { - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin); |# N: r# R( I0 x
- {" U/ r: P- ^. L8 r6 S/ C
- _DataPin = pin;
) j/ m$ i, c' E( G - pinMode(_DataPin, OUTPUT);
c$ M) N1 k' H4 ]2 b% { - digitalWrite(_DataPin, HIGH);
" M0 D; ^5 Y! x - }
( z/ |3 s/ q( }; q4 B - #endif // ME_PORT_DEFINED3 x. I A# s0 x
+ Z8 v/ K+ B u `- $ @$ V: A* _) Q, n" L+ b
- void MeLineFollowerArray::setpin(uint8_t pin)
/ b8 O( B* ]5 y" R2 u - {- f4 x7 ~, x) G Y
- _DataPin = pin;
1 z4 \9 @+ }- q5 _ - pinMode(_DataPin, OUTPUT);
# M' l. _; t3 \/ h8 |: e - digitalWrite(_DataPin, HIGH);* g+ a/ M3 j1 M' V( Y
y' h N" B/ f0 L6 I- #ifdef ME_PORT_DEFINED
* A4 f1 z& ^ Q* L% Z - s2 = pin;5 G' R1 U8 P+ b
- #endif9 k/ g# `" n$ G/ [/ I5 _; ]
- }5 H8 X8 x/ l4 P9 J! S* m r& ?0 A
- 6 g# C# g( ^/ `9 R. h* O
- uint8_t MeLineFollowerArray::getValue()6 }8 V4 y" [: ?+ O1 ]
- {( S4 L2 u8 ~; C. q
- uint32_t LOW_level_read_time; j' N, ?7 @0 [% Z+ w6 c1 M
- uint32_t HIGH_level_read_time;
G8 M1 c% N0 a% _5 S - uint32_t time_out_flag;2 i3 \/ G1 s2 x! A) B
- uint8_t Sensor_Data[3];
: C! G- o9 Q7 J# D - static uint8_t old_data = 0xff;: A4 F3 b. `) T0 y0 o5 o- p
- 5 P5 [/ t6 ^2 ?, ]
- pinMode(_DataPin, OUTPUT);
+ X: W7 _! T$ y/ R { - digitalWrite(_DataPin, LOW);- a( r" V0 ~7 l. l" T4 t
- delayMicroseconds(980);
9 B4 b3 e; `# E0 G% x - digitalWrite(_DataPin, HIGH);1 C1 V/ X: a2 L. F3 l& l4 n/ r
- 2 U" E2 z6 O0 b5 a2 z! u
- pinMode(_DataPin, INPUT_PULLUP);
$ k+ x7 b/ B4 R8 a+ { - delayMicroseconds(10);
# Z. ^9 w1 C: W# K; ~! d+ E; }) @
' T1 ~: D0 \5 L% C- time_out_flag = millis();
) n& _% h t5 t - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );2 F. L( [" A3 ]. i6 E
- ! _, ~% O# B4 A5 J
- LOW_level_read_time = micros();- l- B% d# ?/ I' |
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
$ e# u7 W. m+ g9 B+ F - {
7 O2 r0 P- \5 C, b( j - return 0xff;5 C4 D+ F+ x2 l4 Z! o0 G, j
- }' s% B, E. w# `' a: F/ R- T
7 b, e, [. h. e- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( e$ U# b% n! s" C; j2 c# H
- & @- L; e7 S$ M; ~8 p
- HIGH_level_read_time = micros();# R I) P* Y# M: J6 S
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
" \; F7 R/ d5 z - 4 o) v( x0 A7 h& k$ n
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out) ^7 b4 _4 a+ O1 n& C1 a5 t
- {
8 Z1 j3 F5 T( m; h2 u& [ - return 0xff;
: |2 S% [" W4 U+ W - }
0 n/ ]* ~5 n. K& X) ^7 e4 T
* S- T7 a+ X. M2 `9 F& b7 U- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
: e" A2 E, B6 d I( r3 h' r+ Y) m - {% D# P) H, c/ q: l8 @7 Z1 U
- return 0xff;
+ c# s7 w* c% Q- _$ L - }
! E( o4 B% s, G2 _. ]
* ]( A1 L' O$ h+ ^. F5 g- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
6 H' L# I& S. @3 } i5 M* p) M8 Y$ {* n - LOW_level_read_time = micros();) \# z# n- k# @3 h6 C# Q
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level# G5 N6 R. W. ^8 c% V3 A
& {, x) z' j$ e5 c- r; \# J- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out n1 d% t, ~* n$ l* \$ ]/ N1 E& G
- {( v' _8 I; @2 r6 H8 M- ]/ G
- return 0xff;; m! a$ G% e1 P6 A2 N
- }
" V. z0 f* S7 Y6 ?
$ y: w" h2 V, ]; ^9 r" c- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))! B+ `: Z6 x& X; v, O/ T. i
- {
# a: P1 U" P+ R - return 0xff;0 v" }4 V- P5 ]( }/ T7 \
- } @& p8 z7 {3 l
! U: N! h* p' Y4 G6 p3 R- for(uint8_t k=0; k<3; k++)4 g" Q; s1 m9 y, G: C0 C H, k: L
- {
6 @8 Q$ G- G1 L/ B: F - Sensor_Data[k] = 0x00;
/ e9 N8 B* Z$ Z- o
6 @: S! W# m" _: d! l0 p6 K. A5 ?- for(uint8_t i=0;i<8;i++)* E$ _2 M( g* }! w$ P+ G9 p: J5 J
- {/ E" ^9 B# D G( i+ W
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level; h B7 m+ U0 H6 w7 ?; M
- HIGH_level_read_time = micros();
0 R k6 T0 x" l) Y* X, s - LOW_level_read_time = micros() - LOW_level_read_time;" a' t% l6 c8 V; r6 I- s0 P
+ h- Y& M% w% ^* ]' o: X- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )% _% p5 l/ b% d! M; ]/ n; \
- {
# @/ G6 G" a% O# i2 D6 a - return 0xff;
9 e8 e* j: V% D h5 y - }
' Z" h p* L. G Z s2 U - " E. |; {7 |4 f: \/ H4 p
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );5 [. s$ u% j' x- l' n
- LOW_level_read_time = micros();
. e. J X1 ]$ k4 Z1 } - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
9 {/ T( K6 F+ `6 P# a7 {
4 a: j% I& d" ~5 b- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 10 a9 u$ O+ {$ q* G x% D4 O3 H
- {( L! l% [" [/ M @+ \: ?: s8 o
- Sensor_Data[k] |= (0x80 >> i);8 ~1 J! ? v6 g) F( U
- }
) W8 s2 H5 Q# |) b6 {. L - else if(HIGH_level_read_time >= 100). s# i2 H* u5 R6 Z) m0 C: ^, b
- {! L6 m x' x# m7 {: h! n
- return 0xff;
2 g6 P; S4 Q9 m! y - }
3 {- F4 F. k3 x - $ \- T+ b% U' ?$ K" r8 a/ f: c% K0 O
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)1 b- g Q& m+ v r3 w, m
- { V, p0 v% C# h
- return 0xff;
0 V9 d: r7 c3 }$ I - }5 w! c$ d6 h4 w3 Z( w* X
- }& W4 U" }' R. |8 }' w
- }
( y% v+ G: e; z( I- A9 E1 _
4 ?- C3 W, g1 Y+ k' W4 [- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level% W' r: D( r4 x4 R S' L3 L; I
- HIGH_level_read_time = micros();
- j' Q7 a( J3 |% ?+ X - LOW_level_read_time = micros() - LOW_level_read_time;& _0 h5 H' n: s6 ^
- q% z; u2 b2 Y% m( ]# |, |- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
2 b) U- y4 {2 r, v7 A - {) |5 K& }* L( P5 Y5 S2 r; h( x0 U! E
- return 0xff;& t6 S3 y4 C9 k) }
- }, r. g3 p# F q
- ' K- G; q: D/ P s
- pinMode(_DataPin, OUTPUT);
$ D8 L z! b; E" E' Q - digitalWrite(_DataPin, HIGH);
* b( P) F) Y1 T4 B) w/ e - ) e ?' n7 n0 z" v# {/ y- x
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
+ U; K- P* p; V) k - {
1 q" L" [* d7 O" {5 B - old_data = Sensor_Data[0];1 J& C2 ^ p% r2 E, Q3 w! q3 E
- return Sensor_Data[0];
8 L0 ^( E+ {% e" h1 W% Z - }" o+ v1 \! D" g: l5 M, ]6 u
- else
5 d; X0 ~( U4 T0 V - {- P) ?3 G3 ~1 ], ^, [
- return old_data;
3 ~5 D h9 H0 I# Y( j5 N - }
5 o- S1 d. C1 ` - }
% r) e! d p( A- Y
複製代碼
' ?8 v) ~+ y3 JMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
9 s' p5 i# b, R& z& ], {) O# f
0 G) d5 B- t! P- n" i |
|