|
|
; H0 ^5 V( ?' H( c8 d- #include <Arduino.h>
* N1 R3 |' P( p2 _. a& y; w% ? - #include <MeAuriga.h>
6 u& c& X) g) P5 g7 Q9 ] - #include "MeLineFollowerArray.h"
# ^$ I& C' [; K2 ?$ `4 o
1 L3 ?; y( V5 O: A- X: K) z; |- MeEncoderOnBoard Encoder_1(SLOT1);
- G+ H: J( J- d9 n$ i+ N; ^$ J) O( X+ z - MeEncoderOnBoard Encoder_2(SLOT2);: a/ ?0 y! {: F2 {0 b# |
- MeLightSensor lightsensor_1(12);* |# T \, B x: w% O( T. a+ |
- MeLightSensor lightsensor_2(11);2 L2 w5 z5 h7 Z! `+ R
- MeBuzzer buzzer;% f3 m+ q) j+ Y; {. w+ @
- MeLineFollowerArray linefollower(PORT_6);) S5 s( W' S- m, ?1 b% |
- . A, i4 F* Z& k# q
- #define Error1 1" \. R+ Y* L4 h3 G$ Y) M1 T
- #define Error2 2' Y) u. A7 M/ z) C
- #define Error3 32 N( h5 Y7 E1 U% }5 K* Y% b( V
- 5 `- C$ L7 C' y/ T' V, r! n
- #define Kp 15
; B" g% ^, M; D: \; s- L. Z - #define Ki 0.15
3 M% Y% u+ p% z5 Z/ N - #define Kd 0.03
: @. Z* E& j9 o w- c. \* a
/ C. i2 T0 x! J* ?, P* l- uint8_t sData;( R+ b8 T' k% s9 ?3 ]
- uint8_t D1;
# t1 F* u' A/ }4 P; F7 M - uint8_t D2;
$ E3 m. b+ ?) _% y: o8 b9 q7 h9 o - uint8_t D3;
) u) I `* v6 ~& i - uint8_t D4;9 r/ p( \7 {" [& @5 P% D
- uint8_t D5;
$ n! w6 S% T( s - uint8_t D6;+ h' s: c' |$ d# S2 C4 ?2 a& r
) t9 k2 [5 R! q5 x' o: w& G- float previous_error = 0;0 C0 ^4 a: L' l( Z( ]2 _/ ?
- float integral = 0;
/ z6 x. W O. S% v# z2 v" y - float derivative = 0;
& K6 Q2 s' ~% M+ P( M) r6 E - int Speed = 160;
) W0 p6 F0 X: d B# l - float output;% J) J3 `0 h) C! m+ b* N7 k
" c# p9 n; F1 Q$ T- r8 c- byte Left;
1 x% \* \' T1 _. f' l - 5 x5 E; [5 N/ [
- void setup() $ _9 E" o) l* ~) y8 K8 d) w
- {
# Q, I$ S' T' v% C - //Set PWM 8KHz Q- k. i" k# s5 g
- TCCR1A = _BV(WGM10);
9 H, w9 Y- E" A$ _ M$ h9 F: I - TCCR1B = _BV(CS11) | _BV(WGM12);
* q F* [; J; z - TCCR2A = _BV(WGM21) | _BV(WGM20);
u0 z. t, r0 o$ I/ M1 F- C - TCCR2B = _BV(CS21);
+ f3 ]$ K6 j# B& g% K+ G - Serial.begin(9600);
( U3 i; V# {# g6 | - buzzer.setpin(45);/ n4 Y( J7 Y) ]3 S' k# Y2 k0 w
- }. [: }- \' b/ h% K5 K7 C
- ' t* Q9 e Y; @5 b4 k
- void loop()
) ]7 s. w' V! y" ?/ S+ E9 [9 _ - {% f* a1 c$ n8 k+ u+ S3 b8 J
- while(!((lightsensor_1.read()) < (10)));
. g6 F. L2 g, { J - buzzer.tone(1047, 500);4 |! P b' D& }
- delay(500);
4 q! i% ^, l! S2 T* v - do y$ g6 T7 U# V% V3 L0 w8 c" d) ^
- {
- f" X" C4 q0 ~; @& ? - int Err = getErr();& f4 Y0 `* F- B$ B' h- J
- if(D1 == 1)* W r$ E7 a9 n1 x% T9 h
- {
5 _9 h4 ?( @6 x3 o( _1 h+ C L - Left = 1;
0 }4 Q I: N, n - }9 t" I" M( e" u+ ?# R" {, w& ~
- if(D6 == 1)' \& Z) ]( c ?& V# g z
- {
/ u- ~# L1 e# ]9 ~" z w - Left = 0;
! J( o' Q" B$ g: {$ h9 E - }4 p E4 J$ B& Q" W6 M
- if(Err == 99)+ g$ }( N& d2 e# J [2 w! C
- {% ^2 Q4 P; x! J. _* n
- if(Left == 1)6 E- H* c: a" I2 v
- {
' s5 _; ]: d% A* x2 F, M0 u - Speed -= 5;
. V; Q1 p! A% [- n8 S: P - moto(0,Speed);
$ U1 Z7 l# u/ c3 g6 r# p& b - do
- K: S$ v& M% O$ W - {! U$ f: O$ }$ v
- Err = getErr();
4 y2 |/ c& ]- w0 g: F6 [ - }while((D1+D6) == 0);2 Y% Y6 }: `% m
- }
% a( l5 G G1 Y) [+ a% a - else' L* o ]* ?" i1 Q8 I* U, B
- {9 H# M3 w8 E5 H/ D5 Y- z8 _6 I
- Speed -= 5;
) j- u( _' s# D. ` - moto(Speed,0);
* z1 L7 G. K) v0 t1 g - do5 y9 l9 h/ X9 Y2 R5 c& U
- {* g* k7 ~' Q! u1 r
- Err = getErr();
, [+ `/ ]) h$ U/ A9 D - }while((D1+D6) == 0);
% \ x8 t0 J5 W1 h& U. x - }
7 O: t2 ?7 i+ N) }: c! P: \9 \5 u - }
7 X6 O8 S5 x% F2 R - else
3 @7 E* H; N! ` - {
6 h- m% w3 T0 ~' a8 M9 K. Z - if((Speed < 160) & (Err < 2)) Speed+=1; ' e% q4 B' o% d" i
- if((Speed > 100) & (Err > 2)) Speed-=2;
- K$ c5 C W0 _2 D - integral = integral + Err;
+ I6 T3 z, k* G, n9 G) z& I% D& V E - derivative = Err - previous_error;
3 L& p% {; O9 i1 T% u d - output = Kp*Err + Ki*integral + Kd*derivative;4 `% W8 r0 y" T j* c* {5 a% \
- moto(int(Speed-output),int(Speed+output));
0 i" S% K+ w: W0 _. Z - previous_error = Err;
1 e/ _$ d# g6 z4 e5 I - }; X# m4 K& L- ^3 g0 x
- }while(!((lightsensor_2.read()) < (10)));
' P8 t$ }) t5 T" G9 B# ] - moto(0,0);, U0 {9 ^% Z; N2 U0 J- J j
- delay(500);
, C9 R1 ?# s5 {0 x8 f - buzzer.tone(262, 500);
) o+ T. |" k; `& v7 J - }4 {) }4 _: t. r8 H, ^, N8 j- d
- 0 J! S* m! M# n1 s7 x. G) `
- int getErr()4 F5 i6 k% F: b% J8 a+ p9 m) |
- { ! J! r4 i& @1 o$ \6 v
- sData = linefollower.getValue();
0 w4 ]. y2 [- | - D1 = ~(sData>>0)&1;. s( j3 \ B6 x/ w" g
- D2 = ~(sData>>1)&1;
/ ]! l; y9 x. _/ @ - D3 = ~(sData>>2)&1;
2 T* @) m. B8 l0 Y - D4 = ~(sData>>3)&1;
/ f' d# E d7 F8 q2 |4 w$ ^, } - D5 = ~(sData>>4)&1;
" `7 u8 b; w% l. c6 r2 X - D6 = ~(sData>>5)&1;
0 ^. q" T- U/ ? C6 G8 } - int downD = D1+D2+D3+D4+D5+D6;
* A* ]$ I _2 k& u1 M* m- v0 b - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
0 ^* i+ y* n; `) M# f0 ~ - if(downD == 0)" n/ ?8 X% J, x+ L+ U
- {
6 y' l6 b5 r0 S) w2 Y( ~' g& b - return 99;
: o. i ]& t" Y$ M - }) K4 e9 _7 ^3 A3 j" Z
- else) h; M, l# f; o4 y
- {# \; P* }5 ?: e$ n
- return upD/downD;
% B3 X" O5 Q+ a0 G, S* F - }9 L: W2 z6 d5 E+ v" x8 x
- }
7 i3 e, F; }. n+ K( j - # G( v' O- o+ Q
- void moto(int Speed_L,int Speed_R)3 @5 R+ C' V. X6 t9 J5 Y+ U' X
- {! g1 ]9 x! B; h6 `, [" [3 t* J
- Encoder_2.setMotorPwm(Speed_L);; |' V& x3 f; ^4 f* L4 c$ c
- Encoder_1.setMotorPwm(-Speed_R);
7 V( O1 n m2 F0 n z - }
複製代碼 - y4 |7 X% N, R9 H, c1 O/ r" D
MeLineFollowerArray.cpp& j0 r' O- O/ u/ B% d# I: E
- #include "MeLineFollowerArray.h"
9 E0 V- y5 p/ [9 w3 z O; B8 n( p - # s, T/ V4 m- l4 K
- #ifdef ME_PORT_DEFINED
% }1 ~8 B! p: X" U- p8 o) g* T - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
$ |# }6 T2 t8 m6 l8 U - {
' b- r9 M" u+ i7 T - $ c" |5 {+ v" p6 _' W$ P
- }# ^" Z9 f$ z% D0 l6 @) u
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
% J7 y0 x2 c. u - {
4 W. v7 }8 k+ `% K" } - _DataPin = mePort[port].s2;; _' A8 U( j& F( I5 b
- pinMode(_DataPin, OUTPUT); # t7 G2 Q# Q5 f6 ~# B* q+ x
- digitalWrite(_DataPin, HIGH);
! w9 P" `# i* m+ u7 n$ D7 S* Q - }; q3 Q( r- h$ d1 D
- #else // ME_PORT_DEFINED
( [, U: c n8 R$ { - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
& B9 a; |6 x! M( \: e( }9 p- \ h - {2 m+ D0 k: Y& b+ ^0 K
- _DataPin = pin;
( L7 Y' E8 X. s5 n g# U3 e! k - pinMode(_DataPin, OUTPUT);
; `4 u5 g/ r7 v( ] - digitalWrite(_DataPin, HIGH);( }$ z$ Z, u3 z) Z
- }# [0 `: a; X/ b/ y- b
- #endif // ME_PORT_DEFINED4 Y+ K' ^- H2 B0 [& r
- . g2 \) |' Z6 m! z' l' x# C/ K
N- D* w4 |+ @. h( _+ {$ V/ e1 e- void MeLineFollowerArray::setpin(uint8_t pin)# f6 C/ l& a- E c+ l" H$ M
- {* K r' Q8 [- x( W6 H! Y& y0 J
- _DataPin = pin;
; ^+ J* D+ z* k2 Q6 l - pinMode(_DataPin, OUTPUT); # z1 G8 P' X7 F2 q X9 a; Z
- digitalWrite(_DataPin, HIGH);
" V e! d4 I6 h5 n& e5 P/ t - ) _- c* f7 }: m# z; k
- #ifdef ME_PORT_DEFINED
& [; g4 ~6 ^( z+ q7 U3 E, Y+ W* K6 e - s2 = pin;" k# d% k: s! E" t3 _5 @
- #endif
, k0 g; s& N$ B* X2 `. {' ~ - }
. n9 q5 P4 w- G3 K - , ]: t3 i/ Y9 c! m( y
- uint8_t MeLineFollowerArray::getValue()
4 S- u. T1 `, E& ?6 S! P - {
5 Y( X" R% H. X% \8 w - uint32_t LOW_level_read_time;! j# P, k2 t; {4 X# |
- uint32_t HIGH_level_read_time;
6 Z$ e/ z; P+ b" j& I - uint32_t time_out_flag;
' M" w; p1 Q4 B# J& x - uint8_t Sensor_Data[3];# r$ E7 d# I7 r' M1 Z
- static uint8_t old_data = 0xff;& }$ L# R0 C" t0 \) z" C1 R7 P
# v: E) l$ z6 o2 q' x7 Y, F1 \- pinMode(_DataPin, OUTPUT);
4 Y! G/ }/ O( w& h$ J. e2 u - digitalWrite(_DataPin, LOW);; W3 V( w; @, k" q
- delayMicroseconds(980);
7 @: G# [. L; N) Q - digitalWrite(_DataPin, HIGH);
6 [' T8 Y" a3 ]: |
% e) f# C) F7 ?- pinMode(_DataPin, INPUT_PULLUP);( ?# t) n$ h E: \
- delayMicroseconds(10);
. ]$ M+ M# c* g/ h" M+ `2 v - : m, b+ Q' i2 Q& y" {' P8 E0 _
- time_out_flag = millis();- Y3 M) C9 H: U1 W5 s, H! N
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
& u$ ?: u5 Y$ b* ]: P) r" R - 1 Q. @3 Z8 B5 p% c; n
- LOW_level_read_time = micros();. v0 ~; a' ]- @8 p# _; q! D" g
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- o* K6 w! i; s
- {9 n/ x+ C' `" Q8 \ z$ a. Q3 C
- return 0xff;
8 v$ `+ V0 }6 c, \* t; I. g/ e - }
6 |- O( a+ S) o
0 B! D" B! Q- @* R- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );, }' M2 @9 T4 ?5 ~1 K
- 4 {$ {) h7 X7 d: y% w
- HIGH_level_read_time = micros();7 N) f# B( R, J
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level' `5 c+ w5 |' K' u& c6 t
- + Y8 t. _$ J j8 v# P: C. m* A
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
9 x1 N. U0 h" ^2 V' z9 B& M - {
* L6 v$ e3 {/ \ e I( u. p) z - return 0xff;! K& [0 t. ~+ U7 M! C
- } W8 s$ f g: v+ y, v' P' c
, ^5 Y# b2 H4 q+ `% R- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
7 K) J; B# o: {3 W ~9 ]+ w% m - {! ]& N2 F2 V4 I
- return 0xff;; e. K' _3 @1 Z, p: U4 m
- }
) S( ]) B9 \# F: C5 {
) [6 j; H. r" I- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
; E$ D; F7 s& G - LOW_level_read_time = micros();
, T3 X; L6 Z" G4 G7 X - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
# ^1 d( Z* t! Y" P
5 Z0 k3 K3 p! p7 q% f- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out6 c" |5 x* M$ p! {+ q+ U
- {7 T* e# D$ `6 l2 g1 g: i
- return 0xff;( z* z' z1 L% S; M; W
- }
# R' u5 h: p8 v - " w) b& E9 R4 I( ~) q4 u3 X6 w
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
' D3 E! z! I1 x4 e - {# g6 Y: O8 C: G( ~$ c% ^
- return 0xff;! F0 C% M& p! Y2 Z6 L9 O
- }
6 [- b. Z6 z K - . ?9 R6 b1 f' i+ n
- for(uint8_t k=0; k<3; k++)+ |5 A9 `3 i {" Y9 U6 @
- {
* y; e4 }: l/ _1 d( X/ O+ k, h - Sensor_Data[k] = 0x00;
6 g$ t% f$ I' O$ z+ f' ]
* Q J3 ^, f7 ~# j3 v) Y) j- for(uint8_t i=0;i<8;i++)8 t# J u+ a9 l4 x/ T2 t! y
- {
, K" W, f- |# w- }* \. O - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level2 h% b$ C) G. ?/ }
- HIGH_level_read_time = micros();+ `# a O. r0 {+ m
- LOW_level_read_time = micros() - LOW_level_read_time;; B+ Q M% t' V
- 5 ?7 T+ p' T" ]2 ^2 R! C3 m
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
& K6 Y% K) T- v$ d6 D. M K$ F - {+ W$ G; |' ~! ?6 B4 @, J
- return 0xff;0 K5 p7 D: T) ]. p+ U. r: `' I
- }
f5 H* k8 G5 W' C - 5 J7 |4 @/ a& z5 V& j: s! t# b
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); E9 j$ O Z3 G
- LOW_level_read_time = micros();; h+ l; Y" S2 l* z
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level9 M a2 T. q. ?* y+ ^. l3 L
- 5 O; t R# l- B1 k0 \8 p, g: X2 a
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
+ z) v! G- g5 J( i - {
2 T V7 K5 _' V - Sensor_Data[k] |= (0x80 >> i);
4 V# O4 k7 ]8 c$ w - }% C5 v7 [9 ? r6 F1 G
- else if(HIGH_level_read_time >= 100), n: w/ {2 H6 d; {5 L
- {+ V' g6 d2 ] q+ y: F7 `
- return 0xff;
/ s4 n' I) b) x/ J" M8 n+ v, q3 } - }
2 f- q* ]- H; S+ E M; `- ]
1 R! p* S$ L# K9 k0 z& R- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
* r; V6 D# H4 L/ k9 T: E. X9 m - {- I5 f6 t( w% V; q( e& ?
- return 0xff;
8 s9 e, f! h! ^5 c' Z \# p% s - }
# `. c: s0 m$ n9 w* c9 G - }# {) z0 T1 i+ u
- }
/ a; N9 y1 d* Y7 ]/ n4 Y - ) E3 k; R) H# c) t* @# R, i
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level- b: D0 z6 {1 r0 K% S
- HIGH_level_read_time = micros();2 \: V' |0 o& ~
- LOW_level_read_time = micros() - LOW_level_read_time;
" u( D! |4 G% F1 S, W
, y4 V+ H! O/ Q/ T- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
! b$ w1 D1 d6 E2 r3 _( r+ J - {
; l4 S' @5 m. W$ k3 ? - return 0xff;
4 p Z- d2 I% J - }: b# \/ b: c6 \9 p* A& _2 W( z U% A
- . J& O+ O+ E# E' Y& i/ W
- pinMode(_DataPin, OUTPUT);: l' [5 M( N( B7 k* F9 d( W
- digitalWrite(_DataPin, HIGH);+ g1 N, O+ W. s4 j4 Q) W$ u
5 t }+ {1 X: K" ]4 @ R$ y: o- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))* m/ i+ @* O: N, x0 y$ {
- {6 z5 {- ]$ |+ Y4 ?1 t
- old_data = Sensor_Data[0];
5 e* g% r+ w$ Z, R/ H - return Sensor_Data[0];
) t0 F* Z4 N5 T9 v/ h) o - }
. H1 C$ _! I. ~1 G, @' u K; _, @ - else
2 Z$ u( n" L7 k6 l, F1 x - {& q1 k4 i n/ S( M! q
- return old_data;$ s4 P4 n. v6 e9 ?. p r' x, w, B {
- }' r& ^1 A& @4 E! ^! S% ^
- }1 v4 a3 e& X- r# ]
複製代碼
8 |, Z5 k5 [* [; }MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
5 Z1 E6 z7 D1 G5 W1 ^$ m! z
1 i1 A2 h: W$ V* U1 [
|
|