|
|
! \4 [; T! \# I% Q. [- #include <Arduino.h>2 w3 q) V/ r( ~% w0 M( t3 w& O( M
- #include <MeAuriga.h>; Y0 u& \: C) w) e5 E
- #include "MeLineFollowerArray.h"
+ A+ B( R# G2 \# t s
- N( u- z- I' g( E- w- MeEncoderOnBoard Encoder_1(SLOT1);0 F# F! r/ i0 J* y
- MeEncoderOnBoard Encoder_2(SLOT2);1 z, K [( l2 ~% W8 ^2 a
- MeLightSensor lightsensor_1(12);4 u8 f% m# G: i6 p
- MeLightSensor lightsensor_2(11);
. \( `) P" L2 r# y - MeBuzzer buzzer;# M- A: i* U' ~+ I9 c$ n) r
- MeLineFollowerArray linefollower(PORT_6);; D8 c6 T) W: B
) X {. w/ ~9 d: f/ {/ F0 d% }6 C- #define Error1 1
; {' k2 o! g! z2 c+ ?7 W - #define Error2 2) h4 l0 P2 Q7 i3 e. s
- #define Error3 3
' u9 z$ U2 A9 C; e, D5 p" S
0 T& _8 P0 [+ r( B/ M y3 Z2 g- #define Kp 153 U( B u# Z8 I0 R. |8 V: d/ `2 P
- #define Ki 0.15; x9 Z M' I5 J `- y3 _
- #define Kd 0.03% x7 E3 q9 ?9 x0 y0 L f/ Z6 I! ?- J5 f+ ^
- 8 l7 a* M* o& S$ P
- uint8_t sData;2 t4 s) ~: r! u' y) c& g$ U' j
- uint8_t D1;& z: a$ z# G8 S1 |
- uint8_t D2;+ E; v) V d7 d) o5 H3 I8 V
- uint8_t D3;2 W I0 O% I2 U3 P# O2 C
- uint8_t D4;- {5 w' [0 l& a$ A! y
- uint8_t D5;) \* W# s# g8 h
- uint8_t D6;
, r: `* c2 C5 @' z7 B& J, B - }) c. G0 E- L' h. ~
- float previous_error = 0;
; f0 T- |6 |4 y- s9 g6 ~ - float integral = 0;
- T1 {/ a2 V9 y3 i4 h r - float derivative = 0;
9 \/ L* q2 H$ P" C" f% y! y - int Speed = 160;0 F" }5 H6 b& d
- float output;
2 ]: e4 W5 y; n) T - ! P) }7 ^- R- K! w
- byte Left;! J$ P0 T! b0 e
- 1 I7 r4 x- x2 e
- void setup()
, A0 U/ x+ v5 K- W; {+ D - {* R% l1 j4 H0 }2 t$ d5 o$ s# u
- //Set PWM 8KHz
- T/ Z! \6 J" f0 F, T8 e - TCCR1A = _BV(WGM10);( \, ?8 h5 ^. W- @. q
- TCCR1B = _BV(CS11) | _BV(WGM12); ~6 O; ]* R9 Y
- TCCR2A = _BV(WGM21) | _BV(WGM20);' K) L: h. i4 ~
- TCCR2B = _BV(CS21);
* v2 ^8 d" ?* B# J: k3 J# b6 D1 t - Serial.begin(9600);- q' l( L' c3 d
- buzzer.setpin(45); [3 m6 K- p3 w$ M. T; E6 N& q! s
- }3 m' s3 o% f1 p& }1 s9 A* Z6 N
/ [* @, b8 b( u" |# w- void loop()
; R$ e2 ^3 t6 i8 C& V, S$ G - {
& ~$ O. ?- e1 _" r# I+ I- L - while(!((lightsensor_1.read()) < (10)));* f( h- ?$ N% M7 d. g; b, z c& v
- buzzer.tone(1047, 500);0 e, L2 ]& K6 K* V" s3 v s
- delay(500);
/ W, X- O* N) O$ M3 M0 w4 b6 L - do6 {: a2 W' X9 Z' }, W. z+ ^. Y
- {1 s% J. Z& w% A7 E" P. _! @
- int Err = getErr();
7 K, Y3 w; {- r8 `/ _ - if(D1 == 1)
! S7 {. h3 h; e8 z- M6 n6 L7 O: j - {6 m4 G% i ^5 ~3 G1 H1 `
- Left = 1;
% ^4 m9 Y9 A7 R; d$ I - }
/ Z' I, E" L6 ]; j s& p - if(D6 == 1)
! U1 C: v) \ }7 h; A( b# C - {
3 m: _+ a. R u* w9 j - Left = 0;# v: M1 }- O1 Q6 W& S( s* x
- }
) R/ {/ S/ c+ a0 B9 u - if(Err == 99)
7 G" O* ]1 o+ _) | - {% k; }+ [( r+ E- N
- if(Left == 1). F/ P% u" d; \
- {; X8 o8 j3 I/ R9 ?" B" l0 \7 a! D
- Speed -= 5;
/ C0 k" t# T# M5 i - moto(0,Speed);
; d) {% a% p- {0 L, o2 I - do
& O8 ~& A9 I% Y Z) o$ N - {/ u: o' N5 z `1 C+ K4 u7 Y
- Err = getErr();3 w( Z6 B" D1 p9 c7 _2 Z
- }while((D1+D6) == 0);
; P: F3 d9 J+ z+ C2 ] - }
" j7 t- T: }" X" @ - else$ e. F; U0 u( z% q/ \: G( U
- {% k7 e0 {; H3 Z
- Speed -= 5;" C: s4 N+ y! C2 ]" s# q
- moto(Speed,0);
/ {: X) R4 O% k4 ~ - do
8 F: q" _2 i) H) v4 n, K - {* i7 x$ e+ r- C% ]- F5 e& Y2 n) {
- Err = getErr();
5 h/ @% S" ?2 h - }while((D1+D6) == 0);
6 r! `% Z- n4 g( n - }
$ U* f! e1 b5 ~ - }
# u+ o2 B# _( F2 r# x) j - else
& R& Y- b f2 G9 ] p3 M* z# A& @0 `. U - {
% d! k8 X6 w9 K- f* Z5 ? - if((Speed < 160) & (Err < 2)) Speed+=1;
( s. _5 ?4 h& t$ j - if((Speed > 100) & (Err > 2)) Speed-=2;
" _2 B( h `3 I+ k& y1 d6 O/ N" b - integral = integral + Err;6 q& M. i- M/ H" m4 X: x$ j& P2 s
- derivative = Err - previous_error;
9 d! S1 k3 N: {) G3 R - output = Kp*Err + Ki*integral + Kd*derivative;; x) o! a; R3 q9 U5 A
- moto(int(Speed-output),int(Speed+output));
: a* y( L7 E) V+ A8 c+ S6 A: h - previous_error = Err;
$ q" b: K, S0 F) g - }
4 Z+ v% h2 y; R* G4 X: _ - }while(!((lightsensor_2.read()) < (10)));
& k |! M/ m. Q" |7 I - moto(0,0);
2 }% D4 |# \# N, ?! j - delay(500);
+ l7 o. J& \6 k/ {- q8 @ - buzzer.tone(262, 500);, j% r' h" t8 E9 a- d/ x+ `: [4 h- E
- }( r _; @2 W! _1 T D. J( ]6 `6 _
" D' y1 D* r) D3 I9 i- int getErr()
- y" b9 I5 D# Q& w$ f- s - {
% [4 \+ Q U$ X- ~2 } - sData = linefollower.getValue();
/ q& f) ~: N- I* s( l - D1 = ~(sData>>0)&1;
9 C/ B* E1 ?2 q8 D - D2 = ~(sData>>1)&1;; [# N, P- J$ J) v) V O4 v% s7 a
- D3 = ~(sData>>2)&1;
" W, |6 Z. f* _& s* @ - D4 = ~(sData>>3)&1;& S" w4 b1 A+ [. F: u" Q
- D5 = ~(sData>>4)&1;
5 _* d% M! s# |7 m, {. d - D6 = ~(sData>>5)&1;
! ?+ n& P3 z' s' B" j: {' ?+ x - int downD = D1+D2+D3+D4+D5+D6;
0 V L2 V1 s, u* _ - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);; K: j( @$ n. n
- if(downD == 0)
2 Y7 s/ C* O; S" G+ R - {# G$ S* N4 K7 A; Q5 c8 X9 S, c. r
- return 99;8 N7 {) j7 {" P' Q9 u& ~
- }
8 V1 M+ @5 C4 e" c" i; }: L' L - else% U8 \1 ^5 k0 ]( Y; n6 n" v
- {2 }0 |1 d. X& P6 q7 v# _* J8 s
- return upD/downD;
: p- g9 E- [5 e7 T$ k7 d - }
" E% X( T$ p) n& `2 J. C - }
9 z2 t7 _4 X* v, G
5 |5 Q1 W* ]' G& P" o& [- void moto(int Speed_L,int Speed_R)
& R- b" G5 P" S- w- N - {
3 {1 @) T7 w$ U9 U) S. {/ M0 l. K - Encoder_2.setMotorPwm(Speed_L);
$ e! z$ v- p% r - Encoder_1.setMotorPwm(-Speed_R);
* B1 H: U3 n6 `6 c$ @+ f+ \; M' D - }
複製代碼
( t, O2 _+ ]2 D/ {8 D% a. hMeLineFollowerArray.cpp& v8 o, @" P( ^
- #include "MeLineFollowerArray.h"
9 q$ X/ j0 m% Y - 7 x( g' P# s- ]$ T5 S
- #ifdef ME_PORT_DEFINED
$ R: m3 X& q: k: c - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
6 O7 K9 N# f/ n- h$ t# I: \% Y - {) A( d0 g! i% P2 A, m# y
6 ^. H8 i! |" r& _8 V$ w% [. w# g; q- }
4 I% l) B# h. D - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)6 A5 M. k) `/ u6 \- B# V6 S) |7 [
- {
4 y1 z+ i5 @0 r/ e- m) @ - _DataPin = mePort[port].s2;
& h, g: o# `% s# E+ T1 U - pinMode(_DataPin, OUTPUT); 1 ]8 H8 Z v. u3 O7 l6 y; J
- digitalWrite(_DataPin, HIGH);$ d& Q2 ?" N3 a) n
- }8 a' S6 |# s s
- #else // ME_PORT_DEFINED
# ?1 e' k0 F4 c7 j+ e8 }* ~ - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
/ F1 j& N0 i8 [- a - {; b: [7 Q+ h' Y
- _DataPin = pin;
; r. d- J9 U+ {: B! M4 i - pinMode(_DataPin, OUTPUT); - O3 F/ v ~" L
- digitalWrite(_DataPin, HIGH); _2 U4 _6 P! T/ o4 {) l3 `
- }3 q6 M8 K! O5 c9 w6 T8 k$ _+ i
- #endif // ME_PORT_DEFINED, Y) B0 K% T) a1 i4 N9 P
/ r1 a3 { J, K0 j6 d
: x3 |/ l, f+ y2 \% a; ^0 G- void MeLineFollowerArray::setpin(uint8_t pin), c; @: m6 ]0 n1 `9 ?/ R) l% I
- {# f: L1 X; @; P, ] E) z- x3 Q# A
- _DataPin = pin;! r# _# [7 w- q' m5 k2 d
- pinMode(_DataPin, OUTPUT); : p5 x% t+ p+ P6 R7 |/ V8 }
- digitalWrite(_DataPin, HIGH);
. h, c! O4 i& J+ }, r, Z2 V
1 y. O" x# J- T1 O) Y$ S0 o- #ifdef ME_PORT_DEFINED
" ?* O# M( `; K; b5 _8 ]' r - s2 = pin;
7 i+ b; T3 D1 { - #endif+ W6 R3 y7 W( f9 ~, }# {
- }" J2 D' T$ H; i6 I, { @: h! b
- , G2 W+ |, R% E+ C* S
- uint8_t MeLineFollowerArray::getValue()
, V+ `6 w4 z3 a - {
8 R9 z0 I- m* h' w - uint32_t LOW_level_read_time;0 y5 M- ^7 P3 s6 F+ ~& Q
- uint32_t HIGH_level_read_time;
) I5 v, d% H& T - uint32_t time_out_flag;# m- P( B5 f6 H8 ^8 b0 }2 \$ E
- uint8_t Sensor_Data[3];
1 C1 W# i8 B2 K - static uint8_t old_data = 0xff;
- M5 J, [4 p( B* S. s( W1 Z+ O: r - . @$ A2 D4 m$ c% [3 j# C
- pinMode(_DataPin, OUTPUT);
6 E7 Y6 V" H! i. { - digitalWrite(_DataPin, LOW);3 s- G; u; [5 S/ l6 ~
- delayMicroseconds(980);
, h# a3 U h8 j0 {8 e8 O) V$ h - digitalWrite(_DataPin, HIGH);
2 u, V1 A3 C2 e3 Y* B# B4 k! f
9 Z! [" o! M" j) u' `- pinMode(_DataPin, INPUT_PULLUP);# i) n% @* |/ i: G S" T/ o7 }% R
- delayMicroseconds(10);8 N; H4 {- k) i
- : U& O* i# }) O/ M7 t K4 Y/ f
- time_out_flag = millis();
$ J* ~- v) K1 L - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );& K- [9 o& n6 ~0 d/ F4 p6 Z* A2 ?' z
& o5 l2 V8 R3 b! q- LOW_level_read_time = micros();" L2 t; t0 a$ G6 m
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- p7 ~# \7 V! X
- {: M% g' H( z* T1 ]
- return 0xff;& z$ G' }- r0 G, k% G# z5 Q! c% g. T- d
- }
4 N- x7 y) E2 ?8 F1 N2 ]2 v
4 K/ q, K4 D$ O" h' p; V- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
5 L2 T9 G0 G0 C; \( G. F
8 V/ }2 U0 @4 U2 f/ {& K/ r& p- HIGH_level_read_time = micros(); |$ ^) Z6 ]5 O. O% h
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
- X# t, [" b- @: i
3 T% K2 N1 N/ z$ Q- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
: P, p, y" z, W% D; D3 @1 P7 m - {: T8 N6 j* ~5 |2 w0 O6 r3 f b
- return 0xff;7 [% N. M2 F7 i* _$ Y
- }+ j( f2 I5 ?5 E3 ]( P( X
- % z* x2 P0 ^. N/ @
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
& r+ p" W' Q9 p' l: x9 T+ |8 U$ V - {
; x$ ^) g: T" s - return 0xff;
: g0 B0 u: y7 D. {) o8 G: D - }- b8 P7 Q+ Z5 b: r1 B+ X/ M4 \
- 2 i' M4 ^7 `' l: R
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );- _2 d# n, y. _9 Y5 ]
- LOW_level_read_time = micros();1 Z5 I' N" u/ S+ P" G! _) G
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level* M+ b6 ]) q; _: D+ W& J( Y$ P
- + w( a$ o+ c4 a4 H0 T& e/ h4 O) N
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out, Y0 [6 U- x# H. C, u9 j5 f% T: F4 ]
- {# n7 ], t( t T* Y; h
- return 0xff;
6 }9 N- l: G0 f4 h - }9 Z! L$ O. F+ z# }' k# N6 k
- & I( h4 Y/ k) ^. N. T
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
: p( j- i: z6 J; @7 _( N; j' | - {7 p1 n) N; D" H. a/ r
- return 0xff;
# r/ J3 n% z6 Y1 J - }! B0 g2 _( D% l4 i
- w& m# e% F1 i' | X1 L- for(uint8_t k=0; k<3; k++)" G6 D' z6 H9 J' ? `+ U. T
- {
1 z9 I/ v, ^4 t1 X1 a8 J - Sensor_Data[k] = 0x00;
% P& h" P9 {+ o1 h" t - * R! T% ~7 R& Z" H
- for(uint8_t i=0;i<8;i++)
$ @$ Z9 O) H& `4 P% P - {
+ m4 W' Y- k$ m, O5 N8 M) j1 q - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level) C, v+ `2 E6 W0 j! p/ d' c
- HIGH_level_read_time = micros();5 ^1 g) t; u8 A
- LOW_level_read_time = micros() - LOW_level_read_time;
8 G) e& D* f' `! q8 ~- |7 w6 ? G
. e* I% V3 N8 T p- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
# r- S( U5 J5 w. y$ f$ q4 q1 x - {! S& K) S& i. p# ~. x6 O% @
- return 0xff;- R5 @' h7 C H$ |6 n) @
- }! ]( r- g5 F7 U+ \" p! E2 X
- : m8 f) l3 {( k" R* C
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );, B* @0 p5 Z- o7 m
- LOW_level_read_time = micros();7 ]" P) E: h! r& A5 g
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
& D; X$ d3 ]" ?- u5 B
9 \) D% u i; G6 A0 F2 b- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
" B1 _( q# F6 G2 A: y" e4 w - {' o: @9 j/ p7 {0 p* j& e! j
- Sensor_Data[k] |= (0x80 >> i);* r+ c D% B0 U9 b% Q8 u
- } E: a+ j% p O, t" |% S
- else if(HIGH_level_read_time >= 100)
0 b9 ?2 {9 I" g8 ~* F) i/ q - {
; z) d, e5 a& O5 V1 u% V; L$ u - return 0xff;' r+ M2 }4 s9 G8 Y+ F
- }/ \: [% @7 l/ ^
- 4 b3 H1 r: f* W* q
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)" l, R/ y# W* S1 e5 E; I
- {
" s5 v1 [+ V7 H: o2 _) [' a& v" ^6 t - return 0xff;
+ N1 n; `0 X a - }
9 }$ Y& U" u9 c* { r! ` - }
" N, d8 w3 z: s2 S7 O - }
4 b; `4 [* S# @; G7 a) Z. Z
* ~3 E3 [( M6 X9 I0 P8 m- J; c; A- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level' O7 ^% s; b% y* ]
- HIGH_level_read_time = micros();3 W/ u) e; ?2 Y1 |2 h$ h
- LOW_level_read_time = micros() - LOW_level_read_time;. a' v$ S/ p4 ~7 n: U6 f* r
; C0 W1 F0 ~, Z ?- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
' P, N% g3 R2 [* b3 ?. M - {2 U; ?1 g" {6 r$ _- K+ K
- return 0xff;# j; ~" i# i0 c0 V {) n
- }
# w0 h9 [# l/ A
& u5 q5 I% g( J2 r- pinMode(_DataPin, OUTPUT);
/ v5 v! {; r* C2 [/ E- A7 i& [. a - digitalWrite(_DataPin, HIGH);6 u$ ]) H' k& N, O% S
- B' s' J5 E( l1 g
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))% i8 l) \% F+ `4 |
- {( I( {1 }2 @6 ]& {$ |5 |
- old_data = Sensor_Data[0];6 w0 `$ W; O4 z' g w, W: d
- return Sensor_Data[0];
5 n! D. ?; K+ L - }) c% o C. Y% i
- else) I4 j J6 i* {6 b' D
- {
S7 o5 R) j1 J! K! o - return old_data;
, O* `& ~+ v4 L) V' h, E" [ - }3 ]2 _. ?# e) k. K, F
- }- N/ ^# k0 C! s+ X" e
複製代碼
( n; }) k p# Q% r6 s' jMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
5 L; a ~8 R7 s
3 i9 C0 G5 A2 Y7 g; B4 v |
|