|
|
! t" z" i$ W4 X' Z: Q, q: S* T- #include <Arduino.h>2 f! e' u' @! c; C# e
- #include <MeAuriga.h>: _: K; ]! ^2 c1 T) Y
- #include "MeLineFollowerArray.h" \# Z2 K: k$ t
) g, Z$ w$ C) M, A, {. Y% \- MeEncoderOnBoard Encoder_1(SLOT1);5 [$ z! Y" v6 D U
- MeEncoderOnBoard Encoder_2(SLOT2);& C2 l3 }2 o" ~8 N7 f3 F# j
- MeLightSensor lightsensor_1(12);) q# r* p/ S( u0 V- }
- MeLightSensor lightsensor_2(11);
7 z' y3 }0 i2 ]+ C/ _ - MeBuzzer buzzer;
7 {9 e/ f4 K* |# O6 t! Y - MeLineFollowerArray linefollower(PORT_6);, c) U0 z) q; R% a
' n, V% j4 y$ @- #define Error1 16 z) I( A; z! K- ?
- #define Error2 2
0 w4 M( O0 Z* s& L - #define Error3 3
5 i9 V: C: ?8 Y5 F0 s. o) a
/ }' v; `7 O2 J9 q$ W5 |6 ~ y- #define Kp 15
' ^ a( ~- O. ~ - #define Ki 0.157 [* I9 b* c$ q4 }
- #define Kd 0.03
: E0 Z1 d" H" j - $ E' X3 E' _2 k: G2 f* @
- uint8_t sData;
: P& ~, P, x6 K) |. ] - uint8_t D1;4 q7 A% X! G" }" E& Z
- uint8_t D2;
s U6 g2 ]* P' y4 S6 W - uint8_t D3;4 a4 ~& ~7 ~5 i
- uint8_t D4;
9 V+ V6 z1 Z: W! t, k- l0 a1 } - uint8_t D5;
( r! ` r8 p3 _. Y, ]$ D - uint8_t D6;; {7 x" ^ H/ |: a9 |+ P+ k" c2 [
4 q2 C6 e( z, \2 o$ r. A& ?. R- float previous_error = 0;
/ }8 r6 r! }9 P3 ?' o# J+ A - float integral = 0;
; g$ _! M" ?3 Y, q0 x3 ?: k. k - float derivative = 0;, `; B1 L) u6 q5 q
- int Speed = 160;
- W9 y) v8 o" P+ {* n7 a* f7 ]( H1 } - float output;
3 l. _. \( S+ d B9 X2 R& q/ R
* E' @+ y2 [+ m3 h% }- byte Left;! ^. Q5 a9 g) @ j5 I8 C
- ' v# k' t0 s X' L& h N7 k( q
- void setup() ! [* D( l8 H$ z
- { j* @' z! j" |9 I5 d; k
- //Set PWM 8KHz. Z$ K( m4 T# y
- TCCR1A = _BV(WGM10);( y W* v& p$ r, T
- TCCR1B = _BV(CS11) | _BV(WGM12);
# V& L( Z0 G0 w' i6 L, L - TCCR2A = _BV(WGM21) | _BV(WGM20);
2 k8 I( S0 V: u - TCCR2B = _BV(CS21);
# U( C' e8 [ f7 ? - Serial.begin(9600);4 x. ]" E; [6 o7 N7 F+ A
- buzzer.setpin(45);
: v% p/ o, t. {6 t* x) B9 y - }
; @" w! {: ~" V3 |1 R1 n - + x; k% Z( n/ l. N5 e0 v9 @6 N
- void loop()
: `2 C' `) R( t9 | - {
7 R* ?1 ]% O7 L. P" n' e4 D - while(!((lightsensor_1.read()) < (10)));
7 U2 v* A7 ]' S6 S& z$ l - buzzer.tone(1047, 500);
|* {7 Y& b; q5 ~4 S J - delay(500);, C3 v3 a6 \' m$ w% S5 H% z
- do" m) m, x) i7 d
- {
( w+ ^; o2 V! U! s9 ~ - int Err = getErr();
+ C: i) b4 c9 z# [. R8 m - if(D1 == 1)
3 C# u: H1 h, I: ]/ R9 a' P1 q - {
+ N, O& P: R1 y" |7 W% ] - Left = 1;/ L* b; b( ]4 T) T8 c" Y
- }& d' i1 m, j; h3 P
- if(D6 == 1)
2 b0 D2 s9 `$ Z" {) ^ - {3 N4 [3 Y; ^/ ~; q9 F* r
- Left = 0;2 y9 ]) E; o- Q/ K
- }! f5 {. O2 q) b, J' E0 C
- if(Err == 99)
, N3 ~9 r6 k* i - {
9 ^1 i. N, Z+ G$ j* w - if(Left == 1)
) I- D1 y7 O. P" G% h- v - {
* f0 f- A8 [! g4 w4 R - Speed -= 5;
% O2 L7 ~5 ^9 N* { - moto(0,Speed);
+ b/ q7 X( T5 o: Q8 }; |! [ - do d9 e6 a8 }7 V6 C$ G0 L
- {# T4 Z: }9 J9 N- M$ q0 |0 C5 B1 Q+ P7 R
- Err = getErr(); `! U# t- p1 [# O4 H S
- }while((D1+D6) == 0);
! z. d) S* R s - }
! Z8 m% Q+ o& p! c - else
$ g0 t6 s0 \5 c: k5 G" Q' C - {
( h( m) A9 n* E' q: j - Speed -= 5;' C S& F S& |6 ]
- moto(Speed,0);
; V* [' v5 O0 h1 s% | - do
# U7 e) b; W) G& j- A9 o - {
, D. q: g' E* j2 S: L - Err = getErr();& a6 H" a1 D# W( J9 [* P' ~
- }while((D1+D6) == 0);
* o1 f, H: q) _( r F( k" L - }
2 E1 O$ Q1 O H7 H: [' ^- E - }+ o3 _) J `3 @+ ~4 b
- else
# [( T0 e Q- O* p2 U0 {9 ? - {% ^. _7 b9 U. K6 |# r% `; W
- if((Speed < 160) & (Err < 2)) Speed+=1; 4 C) z* c/ J& ?" C& i* d
- if((Speed > 100) & (Err > 2)) Speed-=2;
- K5 V) B. ?* V - integral = integral + Err;
& }/ R# U% N( {( R, \3 }% ^3 v1 q - derivative = Err - previous_error; `- f7 X' z. R/ b
- output = Kp*Err + Ki*integral + Kd*derivative;9 y( x3 d' T6 K# a7 l) E
- moto(int(Speed-output),int(Speed+output));0 \, ^. ^- ^% L: g# F: _. n
- previous_error = Err;+ F2 [! p/ U) s" o
- }
7 v C/ k4 _ x. ~1 Z3 B9 D - }while(!((lightsensor_2.read()) < (10)));
3 d' @1 I2 w- N9 @& L( G z - moto(0,0);
, }# a$ ?" i# y$ g; f - delay(500);4 m! p# G) x8 k& C
- buzzer.tone(262, 500);
9 I7 X. D: |8 v0 G5 F9 w) l6 Z - }" W# l. h( j9 P* ]- E
- 9 B$ B5 D$ X- l# k$ b% h# J) e [
- int getErr()
0 L7 y0 t" y1 u* e - { : u" G! S0 {9 f0 X: \
- sData = linefollower.getValue();! r, Z: O% j; Q8 w& m
- D1 = ~(sData>>0)&1;$ w! Z/ ]5 B* B- J
- D2 = ~(sData>>1)&1;0 f& @2 T% D5 h% f! k
- D3 = ~(sData>>2)&1;8 B6 S8 N2 W/ k) P
- D4 = ~(sData>>3)&1;7 R4 W; B1 H# q7 U) V9 [3 m* h
- D5 = ~(sData>>4)&1;) r; ~5 G, O* u3 u3 r' j
- D6 = ~(sData>>5)&1;
( C4 c/ E' E7 {/ H f: N) } - int downD = D1+D2+D3+D4+D5+D6;
9 U+ q! D+ J0 U x6 {3 G1 p/ O1 O - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);/ T1 g/ f y1 s9 {
- if(downD == 0)- J9 a9 _ K6 O6 h
- {
1 ^5 K8 c+ R( m! ] - return 99;
8 @4 _. h0 _1 t - }5 u2 Q2 n4 a9 C+ s9 T
- else7 k# \' E7 c! g4 _+ S* B
- {: B6 K0 R' j' w
- return upD/downD;4 c4 U1 I T" E: P# V, w( J6 p
- }
# j+ L& Y3 K5 _! T' k; K W, | - }! h2 V0 I, J7 W* g+ R
. o1 u6 y" U) ^" j' p, Y( Y) E- void moto(int Speed_L,int Speed_R)
6 b8 `! F7 z' s5 J; h5 n3 n - {
" `8 T9 g% K7 I( c7 g& ?8 B. ~8 z - Encoder_2.setMotorPwm(Speed_L);
$ `7 f" t g# n8 E - Encoder_1.setMotorPwm(-Speed_R);* ^# |, y4 l5 r; |
- }
複製代碼 & N6 g2 e0 r% l- l r/ X6 w* P
MeLineFollowerArray.cpp
7 O! T# k& a2 l& K$ a- #include "MeLineFollowerArray.h"
2 n" w6 T/ C4 P/ N8 w& P+ }. k/ I, ?
9 k& F5 _3 |2 L# n- #ifdef ME_PORT_DEFINED) H' f* {7 {- B: M, O
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0). S1 r) D' x' X! H
- {
/ D) F6 r4 a+ }
6 Y" j0 m7 [! ]2 ~% S- }! u/ y3 K# }& N) l% s* u% ]: l: I
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)" L: T0 ^7 }! ~) Z
- {
& m, V" o! e4 S' q- i& {9 l - _DataPin = mePort[port].s2;
) k) [+ j( X- \8 R - pinMode(_DataPin, OUTPUT); # }* `! r* ^ ]& X& v0 D+ D- A
- digitalWrite(_DataPin, HIGH);0 X/ q8 y$ {9 _8 B9 {5 {6 I
- }# c2 S9 S; g9 h1 R* v- z& \% r
- #else // ME_PORT_DEFINED
- P0 k- i4 l. ]% w: t. K - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
: M9 y1 g" W0 L - {
6 z# _' z8 ~- f# i7 c& e - _DataPin = pin;
/ \6 Y; h5 j% q - pinMode(_DataPin, OUTPUT); ) E& H4 c* J, D) ^: p% w9 c
- digitalWrite(_DataPin, HIGH);
! @& }6 v9 T4 C5 ^9 K1 |2 N. g - }6 {% }5 s* V! ? Q
- #endif // ME_PORT_DEFINED
7 o/ V6 p2 g \% H& |% [
- J5 V+ l9 l( q2 Q
: j8 E$ V# U2 x+ e( H: M/ c- void MeLineFollowerArray::setpin(uint8_t pin)
& Q( y) h7 t E - {
5 s6 v6 ~7 Z: Z/ Q4 c1 ~1 s3 o+ p; p - _DataPin = pin;
3 K- s) t0 a y. z# R; a( Z - pinMode(_DataPin, OUTPUT); ! k4 j6 Z6 @* ]5 `
- digitalWrite(_DataPin, HIGH);
; u- S/ m5 X8 ~2 p; a* f t - 9 f: s- E1 V' b8 o Y
- #ifdef ME_PORT_DEFINED
- e& d/ I/ J( r5 _! P - s2 = pin;7 p: d" @& [) H: d1 ^$ P2 O; Y
- #endif, a2 ^; q3 o# s: L) Q
- }7 {8 w' h5 v; `* ~3 D( p
! @* a+ D: K) l0 X5 V% Q- uint8_t MeLineFollowerArray::getValue()
6 L: u$ k; S" u: U - {
$ i' E6 Q) f+ i- u0 Z5 k - uint32_t LOW_level_read_time;2 Q/ y3 K7 i3 G7 ]0 A
- uint32_t HIGH_level_read_time;/ G' B- B; v" K+ E* k% b8 _2 z* ~
- uint32_t time_out_flag;6 t3 Z9 Y. I$ a8 d% ^0 P( C6 G# @! U
- uint8_t Sensor_Data[3];
8 B `8 Z9 P$ z. }% a2 {8 P - static uint8_t old_data = 0xff;
$ _$ i4 k K7 {, m$ k
1 G5 |' W8 M6 j" q- T) _- pinMode(_DataPin, OUTPUT);
2 b$ x( W0 H6 x) i+ o+ G5 u- I - digitalWrite(_DataPin, LOW);+ P4 f' Z4 }* V) P+ L6 @
- delayMicroseconds(980);2 K( {; C" k7 N* ]& F% x
- digitalWrite(_DataPin, HIGH);
# h! V3 ^8 L. D% X - , R' h' @7 ?7 ]% D$ a
- pinMode(_DataPin, INPUT_PULLUP);, k; `5 S# p( r. F
- delayMicroseconds(10);9 k7 ?6 B2 q$ M- `- I& @6 r' h9 ~; [7 ?
- 9 D& V. y- ~9 j. ]- l/ U! a0 B
- time_out_flag = millis();
: B) V& I' g2 i. S' U* j* v Q) \ - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );+ s# C; F. k, N+ K
- ; Y |: F3 w% V& m8 Z
- LOW_level_read_time = micros();: x8 e1 D. r: `- |/ I
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
4 _3 c1 k* I: X; d- ]8 x - {- X/ q6 ^- A) K
- return 0xff;7 h( p* k& z. o k' l
- }+ r! q& w& \- n* b
n; w+ v) z: d; V/ G- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
) y. v* K) }3 P, R% `$ R2 U
: q* s, Y1 ` `: B& W- HIGH_level_read_time = micros();# T' _0 F3 }$ M, f* d* r
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level2 ^" a9 [+ M* V, C2 X2 q& j+ J
- " ^! W# x" H8 s8 O1 Q, Z2 \
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out4 K0 G0 S K/ o; A2 N2 q7 w: P
- {/ S* W7 ~- x- ?( j
- return 0xff;
/ ~0 V% o' @( @) } - }
/ u B" r i% _" N! A5 K4 B - * _- q% A) ?; ~) `( ^0 L
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))# f! ]& ? ~( A! H6 b# w
- {) Q. W* x* g5 e% Y7 X u
- return 0xff;
+ D' e+ R1 Q3 M3 Y - }+ J$ H- L7 j+ m$ k
- 6 j. ]+ D D2 Z+ H( v3 M. y
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( I2 [6 Q5 o: A; w - LOW_level_read_time = micros();
- L, A/ n( O B1 n - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
}3 P" N5 D% A4 c' k
. m% o7 v% w! S) n- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ x; r6 w7 p5 X0 T/ U& L - {2 T% Z" C6 g$ @& w1 }9 B! K
- return 0xff;3 u0 E9 h& ^! z/ x3 D
- }
9 s2 A) t( D, U# G
# ^! |# F; x+ k. M9 T' U: P& ~- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))% T' R. j$ p* s/ m" F% y
- {# b2 v- D8 L5 y6 [& h
- return 0xff;# D5 S; x" i) M1 S: Q" a; R# A7 U$ E
- }
( {6 a% R# s: [: @, x+ k9 J" E8 k. b
& Q' B# r. `* U1 X- for(uint8_t k=0; k<3; k++)* I, }8 Z: E' j, Q' ?
- {
: T: ^1 ?( A5 P - Sensor_Data[k] = 0x00;, ]7 }$ I3 d, r2 @5 ~- R. G
' Q: G+ A" x0 ?7 q/ q" B; F- for(uint8_t i=0;i<8;i++)8 F* v! } V7 V" |
- {
. p( {/ F$ f9 a, R H' l; l - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
" x! w' j# K% A4 X - HIGH_level_read_time = micros();
; x9 c: e) V1 O* o$ K - LOW_level_read_time = micros() - LOW_level_read_time;
+ i4 i+ n; `, `! i
9 [) |5 y8 n: G1 G- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
( Y8 F1 Z& w1 L - {9 d: H. @1 P$ r6 G. h
- return 0xff;
. X2 L1 Z% J0 e - }. _+ ]: E7 i. a* Z1 t+ m5 B, ?9 M: l
5 u m' h' ]/ I: U- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );2 y$ g* U- Q0 i& k0 _% G0 k
- LOW_level_read_time = micros();
# l E" Q7 m+ n0 t4 s - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level1 a" n" F/ P P g
0 y$ a, h) W1 _6 Z- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1' n D7 b1 `/ I
- {0 g' I/ U9 u3 i+ n h j
- Sensor_Data[k] |= (0x80 >> i);
. ~% i3 h ?5 ^; n - }/ e: s8 N) ~4 v7 H( ?
- else if(HIGH_level_read_time >= 100)! V' V. E8 S( `' @6 R
- {; W$ F4 t; L& y% a, A
- return 0xff;
* g5 z6 m! A6 r, T% ~7 b/ f - }
2 \" w9 g$ Q; G7 Z1 w! |9 [' d - - @8 [6 ]: ]" K; d$ @ ?3 s0 @
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
+ e2 h! |6 g7 [8 q# ?3 m1 L$ Z; r - {1 `! T' Z" q8 v+ g% X2 b
- return 0xff;
. y: r2 }7 P: K+ G. m - }
3 h& ?- h" ]8 a - }
$ C6 V+ I$ |- z4 Z" m+ W1 T- l - }
/ v1 W$ {4 I0 N% m/ k1 a/ Q- l
$ r' M3 y t/ ]+ Q3 Y( d- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
2 u* b2 U% l: |, z& V, e - HIGH_level_read_time = micros();" }/ q' E Y3 }1 n4 Y& o/ ^" l
- LOW_level_read_time = micros() - LOW_level_read_time;- G" W0 r3 B/ d; `4 l! u$ f2 r/ E
# }2 p( U* X( p7 M* c+ j6 w- F' M- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
+ P/ ~2 m( y& {2 c: C - {
6 R: f# P7 C1 b! J7 P" G - return 0xff;7 \; y" l/ p, W8 r
- }+ W4 O/ {5 x }) Z. }3 [, M' ?$ @
" Z# a5 F, c# d( d, }- pinMode(_DataPin, OUTPUT); X# k m. ` u5 Y: Y4 G
- digitalWrite(_DataPin, HIGH);* f+ L1 H# g& k M6 ]8 I
- % H) E4 f3 r0 B5 P4 a+ c
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
3 U2 A7 G- l, _) j# \. q' A - {
: _1 d& P3 t4 w+ Y" G+ c - old_data = Sensor_Data[0];3 [' I* @0 p! p" ]( ]2 T
- return Sensor_Data[0];
2 x/ |/ d" Z! {' l& e - }
! e4 i! [( _' B - else
- U. E( w8 ~; G) n - {* P7 j2 \. h2 v4 `$ h: Z, @2 W: C
- return old_data;
" n$ p. O& S* E2 K) G- A - }; K7 i+ Q- l k9 U! }
- }3 g% K0 ?. ?, [/ w$ |5 b/ |
複製代碼
8 q9 w& x) i- i( w) f& UMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
# E, t0 q, `) \. T8 p: ?9 S7 U! {3 |( h( R) H0 U6 \3 }( t S
|
|