|
|
) b/ t+ c( t4 _8 I3 X$ b- #include <Arduino.h>! P! ` \* _8 B9 w5 t% T6 G" b
- #include <MeAuriga.h>( n; Q( o; h. t" `' D0 O' c2 v) {
- #include "MeLineFollowerArray.h"
7 P' X9 |, H2 K, d7 ]
% K+ F9 ~. F. m$ u' \8 A8 \- MeEncoderOnBoard Encoder_1(SLOT1);
* |6 K- y% E. U" A$ J3 I I - MeEncoderOnBoard Encoder_2(SLOT2);
( {! r: R+ X$ E5 {6 o/ X - MeLightSensor lightsensor_1(12);
1 c& L* f3 P; v+ {6 |7 p6 `; w - MeLightSensor lightsensor_2(11);
u. I; \5 H. p; v: s - MeBuzzer buzzer;% H9 O1 o, s2 q
- MeLineFollowerArray linefollower(PORT_6);. e$ Q7 Q% h2 C. g8 G; r: w( |5 h
+ F* V% Z5 R: S- #define Error1 1
( J4 Z; h; w6 t+ M6 `2 j4 F - #define Error2 2
. n' [- j& {5 q( ?" J - #define Error3 3# `$ E& G& b7 Q5 g+ p' J/ i3 w
- & E5 k1 r4 T# p! {
- #define Kp 15) P9 e/ A4 l- V, f
- #define Ki 0.15
( e8 P6 w5 p# I5 W Z# |' T$ ` - #define Kd 0.03
7 |6 a7 E& R5 a9 ^8 h! V - 0 }5 p1 X; d( h
- uint8_t sData;% D9 h: x. j$ p
- uint8_t D1;) `' K5 j5 ]0 o% ]/ c
- uint8_t D2;
, j& \: C5 M: h: x1 p! x - uint8_t D3;
: b1 v3 f! Y4 t. Z% `# I - uint8_t D4;
/ F* A, _& x" p5 C7 u$ ] - uint8_t D5;
( F5 Z# {5 T q2 W4 W& C" J4 e/ K - uint8_t D6;
# e* a- _9 k$ @) @" Z' b - 3 i6 q9 S8 ~& o# t8 l* @6 d4 w
- float previous_error = 0;* X$ W; _0 n2 a
- float integral = 0;
8 {7 o$ p1 u, |4 k/ h, Z6 V - float derivative = 0;
/ n7 V2 e5 [* c5 Z- h3 h - int Speed = 160;: m- K/ G" i" o. G
- float output;" u. d8 V, N7 @7 j5 |# Y0 _
- $ u7 ~+ h8 P1 G2 k- L( ~# Q
- byte Left;
6 y; S8 ?1 T8 I5 i7 ?
( t/ T/ P. k' u- L% E6 V1 W- void setup() ( k/ j" [' O2 w7 v) ~7 g5 i
- {
9 I; E: u6 T) M& p; |, k - //Set PWM 8KHz
( L, l e* d3 u3 W0 k* Q; b - TCCR1A = _BV(WGM10);4 W9 e. W0 u3 n$ x3 n
- TCCR1B = _BV(CS11) | _BV(WGM12);6 l6 l& Z, T5 s6 ^
- TCCR2A = _BV(WGM21) | _BV(WGM20);
1 u1 n2 W" ~/ N0 H( B$ t' v) _ - TCCR2B = _BV(CS21);5 i: M. C% u s. N) c$ E
- Serial.begin(9600);
; ?6 M+ |; W( J% l! {) i* U; Y - buzzer.setpin(45);9 n) H% ?& t) E, E
- }, g- y# h' ^8 E3 J( U8 o* \
- 4 `5 v' y% t8 `4 Z
- void loop()* @* C" ]$ c, j/ B1 [. z2 U! R& E
- {3 E8 n ]7 P- P& P
- while(!((lightsensor_1.read()) < (10)));
' Z+ ?0 h$ P& e. K - buzzer.tone(1047, 500);' w6 U6 N7 f) L6 m
- delay(500);
0 v5 P* }0 @: s* l - do, b; R9 Z+ Q- _) b8 f/ B9 b0 k2 c
- {
% K) b7 r1 F. D9 Q% o - int Err = getErr();
: `4 l7 d8 B- t- P; a0 ?! z - if(D1 == 1)
; M) U6 V) c# w% p - {
* E, A$ ?: W$ b' X8 V+ X u6 Q3 z - Left = 1;
; V, e" P$ \$ A - }7 a, k: z3 Z$ ]( a' E- u
- if(D6 == 1)
/ A% ~, {# H' g' z* r% z - {: ]! _! {9 X5 W& ]! b: e
- Left = 0;2 V* T# e t/ a3 I, n( s0 h) Y
- }
* t6 Q" s: u) f - if(Err == 99)% \( {' n* F8 d# V4 e; Q: n5 K
- {
# ^1 B9 x3 ?) y& `4 P - if(Left == 1)& O9 V! m6 z3 N- ~& b( }5 K
- {
- V( L5 \ I( L" W2 q! \ - Speed -= 5;
! `$ m/ L1 g# l# d U - moto(0,Speed);
. [( ]1 h! ~; B% V - do
. R* s( \+ ]$ h - {
2 n0 N; H7 m! N4 p) Q - Err = getErr();
9 |; @, Z' B8 s) U& I: k4 W z+ B - }while((D1+D6) == 0);9 y5 C& L% j) {, g
- }; w0 t, A! n) e3 B7 d& q/ N* ]
- else
! Q: O+ S- @2 U - {7 W4 s. i D* m# S$ x
- Speed -= 5;
5 D$ C' p7 q: l @# Q# p. | - moto(Speed,0);
3 E* j. H: z3 d2 y9 Y2 m - do
$ ~2 N1 J* z) `9 U5 y - {
3 V) l1 g; C3 Z8 A - Err = getErr();
$ d5 L* L9 ~% ~; A* d# ] - }while((D1+D6) == 0);
: L% o o W- I2 \8 X" l - }
5 M ^0 Y: i2 Z4 X7 m - }1 |* ~. s/ O) t& {$ ^
- else
3 v6 C4 s6 g1 O+ G" _' X _, V7 E - {
% I' P! B7 N9 `6 J - if((Speed < 160) & (Err < 2)) Speed+=1; / U" C3 {9 Q* b( v# B
- if((Speed > 100) & (Err > 2)) Speed-=2; . V; ?$ x/ J# R% h# ~
- integral = integral + Err;
. P3 L) W" l$ X - derivative = Err - previous_error;
& {' Q! l1 E# U; N - output = Kp*Err + Ki*integral + Kd*derivative;" Y0 t/ }. s# P, @& ]
- moto(int(Speed-output),int(Speed+output));
6 f* l' P4 I8 M5 j/ Q4 U/ t7 q$ ` - previous_error = Err;
5 S/ B) F: E, d( E/ u - }1 M6 p9 w$ U6 M5 P* e5 Y: M% @
- }while(!((lightsensor_2.read()) < (10)));
1 n- p9 V# i6 |1 H. F; m - moto(0,0);& Z( t" r9 W$ m( e- z
- delay(500);
1 R+ L2 Q: g& t3 ]- N& } - buzzer.tone(262, 500);
* i1 ?3 G$ m. j5 c0 ?! H3 H2 s - }8 P1 b- \* j/ Y# ^. P$ E% T* x
- $ x3 w+ E" y- b3 R* P; ]1 x4 o& h
- int getErr()
- _' W4 o6 n/ A1 M - {
( _6 E' G ?$ w' p! \! _: \ - sData = linefollower.getValue();
& |; \! T2 W! m. B! Q - D1 = ~(sData>>0)&1;. o$ m% l+ |5 D! b! G3 ^8 w Q
- D2 = ~(sData>>1)&1;" S8 q) D5 v: u7 v( m
- D3 = ~(sData>>2)&1;
& O" d# P* R$ I - D4 = ~(sData>>3)&1;% X2 ~' F& d6 P+ z
- D5 = ~(sData>>4)&1;
0 u. a. i6 U |% a. K- ^ @0 E - D6 = ~(sData>>5)&1;8 _; k# |% o( O/ F9 P
- int downD = D1+D2+D3+D4+D5+D6;
+ q4 w' ]5 ~/ q( r! X: f% d% X - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
% Q# Q' w1 G/ [: d* l - if(downD == 0)" r9 ~5 M. e$ I2 [/ T3 b
- {
' a8 I9 i" T) p; b - return 99;
6 |# o x' ?/ j( S- m4 m: ~ - }
O# F! c$ c3 { g. u# v - else
) |; N* C% [ n1 F5 p' k - {
) ?9 R; R# V( C( @ - return upD/downD;
+ L: t. T6 p, n3 w - }
& k% \% L2 w2 T2 v+ M. A; [ - }
. n g _6 ~# }+ O% [; r" J$ P, c - + x& }9 t% _/ e" k# w8 p7 Y
- void moto(int Speed_L,int Speed_R)
3 F, L4 q+ H6 B$ q. _, D- u) U - {
. L/ t/ f# M2 Y+ X: u - Encoder_2.setMotorPwm(Speed_L);
% o: ], c4 E& S) y& ?1 U/ |0 a$ s - Encoder_1.setMotorPwm(-Speed_R);& B1 v" j. e# [" N" S+ G0 r+ y
- }
複製代碼
6 b! C' {" p KMeLineFollowerArray.cpp, q6 p! a7 s8 _4 Z/ K. _/ ]' N
- #include "MeLineFollowerArray.h"
' u# F0 ?# _" J% x+ z, t - 2 P7 C' _ ]; _2 X$ a6 ~
- #ifdef ME_PORT_DEFINED
' y8 u3 \; ~. o5 C" ?$ Q- O4 s - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
! T: S8 h j @6 N - {1 G# z; n# ?$ J( N5 ^- \0 a
- + S1 ?3 o9 G, F) `
- }
$ a+ R/ W9 ^1 _! G) O - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
8 g: `- h! n3 R - {
& r' p; f b1 ~ - _DataPin = mePort[port].s2;
/ d9 {6 Y9 D! s4 J) z/ a - pinMode(_DataPin, OUTPUT); & l7 ^, w0 s- o' W( F! x2 x: Y
- digitalWrite(_DataPin, HIGH);
3 ]' J) k4 @) n: k - }& S. G" R1 e' j% S" E
- #else // ME_PORT_DEFINED% M+ K' z: B; g* B$ l3 `& K, q
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin) ^4 @# E: W. k& ?# W9 F
- {/ ~) {5 I1 G- R
- _DataPin = pin;6 B# E/ `; h& \9 C, t
- pinMode(_DataPin, OUTPUT); 6 V$ v/ h& X& g
- digitalWrite(_DataPin, HIGH);" d) K6 d0 l: p6 ^3 U% \( s; x9 X8 u v# j
- }
6 D V" ~' A2 q - #endif // ME_PORT_DEFINED* H: N! P& P! ]1 o0 w8 A% R8 N
: `( \: O. \* |+ G# }$ @* Q; |
- W. s7 z9 w" y* z2 J" t$ J' Y- void MeLineFollowerArray::setpin(uint8_t pin): q% G3 X# Q3 F1 L6 c% p
- {
" s3 p# T& d( g - _DataPin = pin;
- {4 G( {5 [$ u0 s - pinMode(_DataPin, OUTPUT); 5 Y& R. z# q0 h! l$ c1 j
- digitalWrite(_DataPin, HIGH);
" ~1 q0 ?. x! I* b
& N! N' O- _( l( `- #ifdef ME_PORT_DEFINED
4 C* E$ d; @. F3 ^3 q5 p. o - s2 = pin;
7 {$ K& ]0 K d$ |- i' p @ - #endif
, K+ y: i) {' A- F8 n8 y! d - }
" O9 Q/ a# y# d$ Z" I2 _' g
+ U5 I8 @7 T" |' Q- uint8_t MeLineFollowerArray::getValue()
: |6 \0 q; W! Q2 [; ? - {
+ e& v: t9 X6 y g - uint32_t LOW_level_read_time;
1 T d. {, H6 m8 c b) k) g - uint32_t HIGH_level_read_time;
1 p) D% E k. G2 d, z" O - uint32_t time_out_flag;
% F9 n h `7 ?4 x - uint8_t Sensor_Data[3];3 m& t' S" g5 a. O( ^9 S5 N3 i- J
- static uint8_t old_data = 0xff;$ o O; u8 U* R5 q4 k0 j" R' x
- 7 x8 x% p; j f0 }3 M
- pinMode(_DataPin, OUTPUT);4 b1 r! h$ R$ F4 L% r
- digitalWrite(_DataPin, LOW);1 S/ n5 @2 z7 g
- delayMicroseconds(980);
2 F0 R0 `/ g( d$ o1 y3 A - digitalWrite(_DataPin, HIGH);
' F6 G$ o- U9 z6 p - 0 _9 I+ w9 j' z$ y
- pinMode(_DataPin, INPUT_PULLUP);
8 k6 L8 C3 M+ P6 N' T - delayMicroseconds(10);
" i/ ~/ Z5 `8 |4 x( ^! A
: P; _) `0 ^2 o0 _- time_out_flag = millis();
( ~7 G0 r# m0 @! v2 F* a - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );$ O% M" C( o- ^3 c) j! n7 w
) b; u0 H) ?3 u" ^. C V, x- LOW_level_read_time = micros();$ O9 W2 ~* t. ^% {7 R
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out: \, j: O& C) c2 u
- {2 v8 E8 m6 q* U( z$ ]4 h; }
- return 0xff;( J+ o! W2 ^* u7 m5 [
- }$ \5 {+ l; W3 P+ p
- ; C7 A( Y' _7 [5 O5 E* V
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
; v) G3 [: l0 Q6 ` - 1 U4 i- |4 F0 E
- HIGH_level_read_time = micros();% Y8 ?2 o1 w- k, W9 u
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level2 v- L j! @& Q
- ( Y" q0 L2 f7 z, g, D. y
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
' t" b2 z, x: F2 n1 z - {
" `+ w* i% \. z6 G4 i - return 0xff;
- F' L3 L, L# Q. o- s% R7 [ - }% ]0 w' s" j8 T1 K) f
- 8 B/ |* B2 D' Q9 _) V( Z
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
2 {$ t# ^! P3 v% d( w1 V - {: [& V. F! \6 E) a3 E. I" _
- return 0xff;: |. `8 V- V& [' o
- }
1 \! g, E I5 ^& m- w
4 s+ y6 ?; u' N7 g- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
5 U: _! O1 D, K5 d5 n. [ - LOW_level_read_time = micros();
# j e4 x3 M7 ?/ n+ }7 b1 e6 @ - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level7 ^! S! D: b: q5 ?4 `* U0 V5 p
, @3 \1 `- |2 `0 n8 Q1 N4 C: d# T- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ f! K/ I- ]5 e% Q* `' B! Y - {
$ ~4 Q d7 M$ u* A - return 0xff;
# Q1 {; t0 g9 ]% \; \ - }
. F0 k( C/ G3 q" S6 H" u! j - & X4 G* e/ p# L" }& W
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))9 x5 E2 w, j, H; @
- {( p: J) h5 t- v3 U r3 f) y6 e) i
- return 0xff;
! L" R5 ]0 F* O: i - }. H3 X7 W' w) O
- " [1 i W' O: M4 C( v. T! J! e4 D
- for(uint8_t k=0; k<3; k++)+ V, V! f3 R' M, ^$ I/ m8 t
- {; F5 d& Q9 K; H( d% y' i6 a8 T
- Sensor_Data[k] = 0x00; t% R( Q( r3 t& n! U
- ; m& e0 l5 f4 r# i
- for(uint8_t i=0;i<8;i++)
`+ S! S( t9 [( m& G# m - {( J9 O" d( G0 H4 B& n
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
6 I# p/ ^9 V& E7 o* V; R: l0 W - HIGH_level_read_time = micros();
% P o: x9 W, J; j6 I. V( k! N& c6 S - LOW_level_read_time = micros() - LOW_level_read_time;2 \) L9 \6 \ |7 {+ J# K* [
n; t. [! `1 |/ B- d6 T. {7 }7 F- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
$ y5 x8 V* b u3 `5 g, D' H - {
8 u; [# I1 E3 B. u& d, ? - return 0xff;
- }* T9 S6 R. M8 A - }
" D! Q8 [/ i7 ~$ l* t* }
3 C% g+ A* K; g- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ _5 d# u( h+ E1 G - LOW_level_read_time = micros();
% k! z9 a' y1 F/ P6 ]! S4 b - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
7 Q; O9 b8 A+ z0 o" _% g, w
3 Y& {% ^! v% I, o; F/ Q# D7 x- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1/ {5 b1 a) s/ u3 D
- {
3 U7 u5 `2 j' Z! ]- [ - Sensor_Data[k] |= (0x80 >> i);
. h( c( V8 M8 ?0 A4 z4 K m - }
* w7 c9 m; t+ i$ ? - else if(HIGH_level_read_time >= 100)) v# N5 L5 a" Z( M! M9 _
- {1 x% ?. {8 o, c
- return 0xff;
* s5 z9 ~, t0 H$ `! d8 c - }4 \4 n( }+ \! ]
# C# ~2 i/ a3 n& N K6 ~* b- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
* S3 y( K$ \" g3 t - {
, x( U# C" e- @- b - return 0xff;, T o% j. A& P# u$ S3 K
- }5 \2 i& l, H, |, E) c9 z8 u9 c* D
- }6 f) |! a5 `, `
- }
3 n1 _% L ]9 O* E4 m! M0 u, y7 Z - $ t* L7 |. Q. X8 L5 u
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
0 I- r! z4 |2 C7 b: \+ c - HIGH_level_read_time = micros();
4 t( Z, Z0 x; C! S5 L+ x$ a$ ` - LOW_level_read_time = micros() - LOW_level_read_time;: S( _) o% }7 o8 H" Y( F# q
- ' g1 d2 U% s7 m9 G+ L
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
' [4 N% ~: Z8 F% i0 h* ^2 G - {
+ [( u# q2 J& y% I" t6 U - return 0xff;- V( a3 g. {$ D. o3 t9 Q" |% q
- }
1 ~2 u' b$ Z* S S* A - 9 n5 i( b. t5 V# E8 ?' T. H( U
- pinMode(_DataPin, OUTPUT);) s: ?7 A# N4 T1 o* L6 N* I7 h
- digitalWrite(_DataPin, HIGH);
& O' T7 t1 ^7 Z1 n( x
' v$ ], b- K0 w* B# W. {- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
! ? C2 q, i2 X+ \! q# V% C$ F - {
% U* ^; k6 V* e - old_data = Sensor_Data[0];
$ k7 X" ?+ c% N: B - return Sensor_Data[0];# ?9 o4 u$ ^. w+ r$ V$ Z& M& E
- }/ s. U J( p" X% x) n+ P% f$ M" z
- else- `% X T# R1 D7 \7 o
- {
' X ~ I4 v) u7 D - return old_data;" o& j, ~- w; H3 \( q
- }
6 o" K* Q1 Z7 M/ k* _ - }; l3 z& _7 c* `3 y7 H
複製代碼
0 C5 k& U" b" |- q# N' M9 M4 X( q% ^MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
* z# U0 i* T; o; A4 U
+ N- T% d- `* e2 S+ b
|
|