|
|
1 |& _2 X- H, H. t# V; Y
- #include <Arduino.h>
. J/ ]: q0 |6 X) _- m' u - #include <MeAuriga.h>
5 _5 E' w2 Q# Z$ ?) L, U - #include "MeLineFollowerArray.h"
$ i. t4 b; ~' c3 ^! \1 i, P- j. P
3 M9 Z( W G8 y8 o# ^/ A' `5 r- MeEncoderOnBoard Encoder_1(SLOT1);
p" h7 e0 U; S# w& }4 k - MeEncoderOnBoard Encoder_2(SLOT2);
3 s# R& f! h( O" I' R - MeLightSensor lightsensor_1(12);# ~8 @( P9 b% B. U9 T
- MeLightSensor lightsensor_2(11);1 m& |& l$ M, Q' O- z4 b
- MeBuzzer buzzer;' q9 s8 B( y' a! e
- MeLineFollowerArray linefollower(PORT_6);' X1 h. i7 k, c2 f ?4 p
- 7 |& F- x" ^, Y, E$ j
- #define Error1 14 Q9 M+ i8 M& ~% V' F6 u' g
- #define Error2 27 p5 { a) |3 W7 F
- #define Error3 3$ ^: G2 b# N- A+ a5 ^
- - A9 p3 ]; k* Q5 _: {4 h
- #define Kp 15
& R% K9 `" g4 f K# x/ l2 \. n - #define Ki 0.15
, K/ d9 b8 Q9 ?+ B; I5 {4 d/ {# G - #define Kd 0.036 S s# x- O1 s& ~
- 5 B# W4 o1 u* Q( a7 g; N
- uint8_t sData;
: r% B+ h: T. [1 f - uint8_t D1;5 j, b/ n5 l& k( }7 d9 q0 n2 Q
- uint8_t D2;
( }: G+ J' S. p4 l/ ^- \ - uint8_t D3;
# h% O, R% k. B H - uint8_t D4;7 U: u- c( }, I. r
- uint8_t D5;
9 m* d1 r4 W& @& m4 g8 L1 b) o N( R - uint8_t D6;$ T& y8 j9 Y$ P: T: Y
- 3 y9 j7 h# w& z" L& V
- float previous_error = 0;( u w/ Z$ X. [6 I3 l+ e7 v
- float integral = 0;
% V4 x- \, q7 Z7 \. l - float derivative = 0;: {' P4 [$ d: Y" z. J! @; |5 r9 v
- int Speed = 160;
$ `! h. |( N, |% F - float output;- j& v; z# W/ y4 B$ \# G6 h
1 v) p+ t- f8 {& c4 ~- D9 F8 z- Q- byte Left;0 n) {5 o% s6 z
# X9 T2 g& b; m" I- void setup()
/ N2 y) A0 p( k0 Z - {0 W1 |0 k/ m5 V- L
- //Set PWM 8KHz
8 U" e7 D1 b9 f2 ]$ ]6 J - TCCR1A = _BV(WGM10);1 A1 v7 v' @# G' f; M8 `5 f
- TCCR1B = _BV(CS11) | _BV(WGM12);8 x! y+ C Y7 `! \
- TCCR2A = _BV(WGM21) | _BV(WGM20);1 u& y8 q. I: \) n# C! ?
- TCCR2B = _BV(CS21);, a Z: s3 V8 n
- Serial.begin(9600);
4 H) k4 N- D `' v+ J - buzzer.setpin(45);9 Y9 c- M) k. a) D1 H
- }* ]- U, t5 g* A+ {/ X
- ; ~4 L& V/ Q: Q$ ?9 n
- void loop(). a( Z* Z4 l ?4 E+ X4 d
- {
. `6 `4 R8 K' _/ G - while(!((lightsensor_1.read()) < (10)));- v( ?/ O- D: H c7 Z f
- buzzer.tone(1047, 500);, r9 t7 h, }9 q( T n9 ^" t$ Y
- delay(500); u9 c7 o/ ^( y4 v% {2 q
- do
# k( ]) j( j( I' V' p - {
+ C4 R* ^5 P/ D - int Err = getErr();
4 H: g6 u3 N+ n \! U) k - if(D1 == 1)$ w- q8 i: z1 x; l7 p0 K" Z9 n: [
- {; K# g# i1 t4 B5 G" ?! P; g( |! ?
- Left = 1;
5 N0 ~0 O: S' R - }+ h; X- L3 v' h% _: K( A
- if(D6 == 1): y b) z% V p" d/ s1 F) {
- {5 t, Z7 ?: c# Z5 W3 h0 }9 b4 _
- Left = 0;
E- X+ }3 z% S" d - }
" c7 h, }8 I7 g1 t - if(Err == 99)' U7 O. n) I7 S
- {
0 l; \0 E' Q: T7 a& T+ { - if(Left == 1)# K/ V" {. e6 @. L( w8 }6 ?
- {
& m/ T5 N. b. i6 `. S* U - Speed -= 5;
5 A9 a0 j! U; S. [& t; a - moto(0,Speed);
$ c0 h1 _7 a6 D; B! @$ L - do) ~8 @. N, Y* ^6 N; r. `; a, }5 f
- {
$ i, U; B' h6 C s+ ? - Err = getErr();
9 q; t k/ ~; l( G) S( Z - }while((D1+D6) == 0);- H1 T( R& F! |& g4 C, ], b7 F
- }1 N4 M0 j3 J: `: F) H/ c
- else
: A5 P# g7 ?1 |# F" P/ z" K3 u - {
" H3 ~: I+ c4 Q2 w3 M - Speed -= 5;4 T4 j; D% u) M% a' {
- moto(Speed,0); . C- v! j( U1 Q% V- C
- do
' G. [% w4 o" Q% T9 f0 f6 O. a- l - {
7 Q4 a3 K) i, |, e' R! m - Err = getErr();
+ z& Y4 b+ @0 [% _% j/ F! z - }while((D1+D6) == 0);# [% c5 f4 g7 w0 _# E4 X/ x- Z) B/ t
- } g% j9 d) T% L& w
- }$ j. ]3 q7 O) b. f
- else
) b/ I5 E4 c9 U1 ? - {
~! \& o! E* { t) X - if((Speed < 160) & (Err < 2)) Speed+=1;
% O9 b- I6 N. G8 e w - if((Speed > 100) & (Err > 2)) Speed-=2; 6 F/ q7 q* |% N3 _
- integral = integral + Err;
' V) D/ b% ]9 x# z/ A - derivative = Err - previous_error;' U0 B0 b/ X0 L$ X6 \
- output = Kp*Err + Ki*integral + Kd*derivative;
: K' `5 b3 N, U - moto(int(Speed-output),int(Speed+output));( D( C( Z0 i6 h8 t3 ^
- previous_error = Err;
8 Z+ G3 N% `. C! l5 K! s. V& b - }; p, _* o6 r4 Y
- }while(!((lightsensor_2.read()) < (10)));! e+ S) _5 j F- X) U
- moto(0,0); z8 M# t0 h( r+ P
- delay(500);! J( b" n4 ^* n( D' Z' Q
- buzzer.tone(262, 500);3 q; O; q" c$ p8 j# G4 ~8 P, K$ _
- }0 a$ K' P& e( w' Z) L8 Y5 C
- ( X6 ]1 N( U* {7 ^: [
- int getErr()9 T4 e" f* b9 x5 x5 T2 I- z
- {
8 M9 t; S" Q- p - sData = linefollower.getValue();+ G' b5 _" {' L" k
- D1 = ~(sData>>0)&1;1 w+ Q) m0 m; G* ~4 [
- D2 = ~(sData>>1)&1;
# {) _$ i6 s% }& q, [ - D3 = ~(sData>>2)&1;
[7 Y. b1 Y2 N1 S2 e% J - D4 = ~(sData>>3)&1;
) u$ I; @: {: y$ {% a& t - D5 = ~(sData>>4)&1;. J/ M3 Z8 f4 B! h7 ?) s1 F: I
- D6 = ~(sData>>5)&1;6 t$ X6 A/ Z! K" W$ E; y% S
- int downD = D1+D2+D3+D4+D5+D6;3 W! \$ O m7 E/ b1 G- b3 ^4 j
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);/ B7 Q# j! x, o( K- Z" ]
- if(downD == 0)2 g& a+ q- a; [, w8 f
- {2 {# j, F* O; S! @3 D& A( b
- return 99;% A1 G: c1 O0 O; Z
- }- [: {# K- p& { d
- else
) D a) }5 w) F, b1 d/ p - {
' o- x, A5 X2 c$ Z$ ] - return upD/downD;2 i3 l/ z: R6 j; n% C
- }
* o3 T" t( R! D+ d) c7 `$ ] - }
! D0 `0 k; d: D1 k9 P - 6 \% \2 _9 g/ h" A; I
- void moto(int Speed_L,int Speed_R)
1 [% B# X$ L7 H9 Q6 R - {
6 Y6 ^( w4 d2 p( l* q - Encoder_2.setMotorPwm(Speed_L);
% i6 A" B9 `4 d) b! x2 K- ? - Encoder_1.setMotorPwm(-Speed_R);
! B8 O k: {4 O: [4 k: _ - }
複製代碼 4 D5 j1 l/ a7 B3 ~2 r% T: l- a
MeLineFollowerArray.cpp- W9 b0 A' y5 E# f! A
- #include "MeLineFollowerArray.h"( M! _! c) y* y8 y c+ m0 ~
$ Z- a! Z7 a: h0 [5 l- #ifdef ME_PORT_DEFINED
1 l/ S, ~) J* h% |& _ - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)3 o% Y+ l9 ]* W* s5 Q
- {
5 \2 g7 W) `8 o& r5 B4 G
8 k2 N. r* \* m3 {9 B, a- }$ s4 L: b5 g5 v: v- \; M5 s
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)7 S* Y- f/ m! }1 c7 P; X+ C
- {5 S8 F P) ^" Z: s3 q
- _DataPin = mePort[port].s2;# _6 S% l+ m1 g2 w/ _$ R8 ^
- pinMode(_DataPin, OUTPUT); : W4 }7 z* T2 Y9 L
- digitalWrite(_DataPin, HIGH);
6 g/ w) i! S$ e9 P - }' K8 n0 C5 f" u6 G0 o! Q* ^6 q
- #else // ME_PORT_DEFINED
8 I6 r' c3 ]% ]+ U6 s# r - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
# u7 t R! D' m4 n3 A5 ] - {
* c3 {# H9 ~5 ^2 e+ }) D - _DataPin = pin;; c6 _2 l3 q4 a8 Q3 U
- pinMode(_DataPin, OUTPUT); , Q, g! W f; R u9 Z$ ]
- digitalWrite(_DataPin, HIGH);# d8 [$ \+ _4 a
- }7 S; w6 \6 V" m9 ]+ n
- #endif // ME_PORT_DEFINED
. ~7 ?2 ?! E0 y4 h& u - 2 c" _/ h p( c& ^- I
, {0 u. P/ u$ V- void MeLineFollowerArray::setpin(uint8_t pin)
6 ^ K0 w' A8 ]) d# x. c5 |5 c - {, \# I# ^; S& k3 i: S
- _DataPin = pin;
6 d( ^0 M. [. x. k$ q1 l1 F - pinMode(_DataPin, OUTPUT); 0 k1 C( t; u0 E7 W; C
- digitalWrite(_DataPin, HIGH);
% x( Y4 N5 K: F% K! c0 { - 5 m& v0 [" K N; @* T7 x4 a5 u5 `5 ]& u, E
- #ifdef ME_PORT_DEFINED
) B [' z/ g% V* ]/ |( Z - s2 = pin;/ [: X9 I. a3 J, ?3 p& x
- #endif
3 _3 }- n9 p3 n/ u - }. y, h4 K4 I3 Q* d9 e! @$ E
- 9 w, W Y7 t2 x7 ~: D- X
- uint8_t MeLineFollowerArray::getValue()2 @! A' K* `. N% n/ M6 j$ J# r
- {
$ k; _, m; z; e& _+ M - uint32_t LOW_level_read_time;# R, B" _/ e3 b+ n0 \6 H
- uint32_t HIGH_level_read_time;
- z" z8 ~. n# D' [ - uint32_t time_out_flag;" l1 d9 [" x- M) @+ U+ j
- uint8_t Sensor_Data[3];- F- x- n5 ^) P$ |
- static uint8_t old_data = 0xff;
- f1 ?, g u7 } - ! N2 @* E5 o* z& n$ t. [0 ~" }
- pinMode(_DataPin, OUTPUT);+ X9 x# K. B: L- V2 g- G
- digitalWrite(_DataPin, LOW);, b2 J- h# T5 N1 O l
- delayMicroseconds(980);
! {+ ~2 P, H, |. z( W% A - digitalWrite(_DataPin, HIGH);* M7 e5 Y Y9 o) A" c! T
- 1 P- o: R' u. `& V, |' o0 w1 O
- pinMode(_DataPin, INPUT_PULLUP);
3 G% g4 a4 L! N( I0 ~; L: M - delayMicroseconds(10);
1 p5 ]9 `# |/ P& g - - R+ P9 X! H- p% B7 O- y# N3 b
- time_out_flag = millis();, X ~; |% A1 e% a/ R+ e/ l
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );" a) P7 T1 v$ q8 I6 f/ T$ _ M9 H
/ G2 n! E9 G, a# f( N. N7 d- LOW_level_read_time = micros();
! y! m, {; c u5 [7 U - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
& x0 A7 e9 x* c% }' f7 \ - {0 B- B' y! }6 L' j5 w7 ^
- return 0xff;
6 R. p/ z! \$ a N1 B - }
5 s% a$ o9 |# e: ^
: J1 B- u! t2 @4 b( q" [3 C$ i- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( L# t2 T" [/ l3 N0 o
5 l1 u2 T' @0 P' K- HIGH_level_read_time = micros();
# j0 T/ I7 k! A* s+ T8 I - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
& b; v& I0 v! j, E ?4 }- j - * N) v6 _1 W- C2 p0 ]# {+ E) B1 @
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- p d" I. V( a# Z
- {$ o' T! ]# z( x e6 d& R% I) E
- return 0xff;5 }# D( H0 f% Y. H/ t4 ~% r
- }
; K# g6 y4 {' E+ o2 ~6 Y
5 O) m: }: B0 y; j4 X" _ I) x# t6 a- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))' l D, e& K! I/ ?4 n7 j
- {8 h8 R' v, j/ r1 n& e
- return 0xff;4 q+ m1 w! w- K/ e
- }6 ~- v0 P! D. X, s( t
0 z2 I- ^& \4 u3 d% n- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
" s( Q2 @5 B7 ~8 Q. k - LOW_level_read_time = micros();
* d* n( B) B( k/ E2 g - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
5 h( A# [9 x6 P# b: D7 T+ W
: [" s* r- n* E9 e a( E- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
2 ?' N r9 W3 R6 K - {
, @- c/ V5 p/ Z% T- r9 B" o; Y - return 0xff;
. B, S+ c8 @; ] - }
% \/ T3 N& I4 m( w& t$ W - ( q6 S% x; j" O1 H
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
8 j9 p: L9 R4 N) w! f - {8 B% y% n& E* @1 C5 G
- return 0xff;
, I2 ^4 T5 M' p - }
+ a5 g1 o6 ]2 f, J* L - * z, W6 ^: z& l* P/ u6 s# r: o z
- for(uint8_t k=0; k<3; k++)
6 a3 E" q' Z! ]& Z6 L8 }. m; d - {
- f1 T* [& d" c1 P4 C - Sensor_Data[k] = 0x00;
( M4 _# q. A: h |4 k - n4 D5 H3 ^) L* o2 g5 X0 U
- for(uint8_t i=0;i<8;i++); B6 ^* m2 [; }+ S/ |! w& R
- {
! s) f7 S# z {/ o, @ - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level! |/ R2 ~6 W6 M3 ~3 Z( @+ O8 R+ m
- HIGH_level_read_time = micros();, c( ?, Y, F5 f1 Q. _, n+ l6 z
- LOW_level_read_time = micros() - LOW_level_read_time;
5 {) A4 y: b) [& R0 ^7 g - + ]. v6 @2 a' W' x: n" ?9 |/ n9 J
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ) M, X4 C& s4 f |
- {
/ I5 v" R- K1 I+ A; x - return 0xff;# s! d& W3 e, A5 h, `" z
- }: R. b& ~2 T# [" ^7 V2 x; s: N1 \
" Z/ b; Q1 n' K; b& K0 V- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 N9 d% a# E4 \" K# D - LOW_level_read_time = micros();
; T3 K; E! ^1 n* P( w4 { - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
; S0 f) ?- V7 ^2 k( A* H4 l4 S - 0 o) W! `" }6 k8 z/ Q' y4 V7 V) s
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 15 `. N- S6 y! C6 e% m( i
- {
% ]! a6 f# u/ _! Y4 Q# g4 z - Sensor_Data[k] |= (0x80 >> i);
' O) Q# h: f1 n - }
/ u. z/ V8 n% j# `8 a7 T - else if(HIGH_level_read_time >= 100)
( Z" H+ v$ H+ a1 R - {
" S5 Z9 W# h* G7 I& ^' I+ e1 F7 H - return 0xff;
+ l4 x! c- j8 r/ } - }
. t- ]/ H1 b" n. T' N7 R6 H7 E3 g
% k: \9 A0 F7 H, B7 a7 a- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
% L- n4 C2 K( a6 M. z - {
% D( ~/ g* a* U8 D% w, R - return 0xff;
& n; \0 A* @" W - }
1 O. n$ k4 r# c& B/ V - }
/ c% s/ X5 x8 a - }
7 K5 K1 `# K9 M4 b: b9 |+ M
) |$ i& z1 k* Q D/ D% e/ a- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
+ N/ v) m7 e q! O1 ?. l0 c; t0 E - HIGH_level_read_time = micros();. O2 Z8 q$ t) l& H
- LOW_level_read_time = micros() - LOW_level_read_time;* I! b( U5 h) F
- 4 P0 g3 P4 n) P1 T, K) U/ @
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ). H2 L3 d% I3 ^& D; `0 g
- {
$ l, S7 {# H! ^! w* R4 P/ ~* ?3 d7 e2 m - return 0xff;
5 J) W/ v0 C2 K4 w" H - }
4 \% |: H% s( D2 L& L( t
1 Z2 W+ ?9 F: q9 W- pinMode(_DataPin, OUTPUT);
$ X1 m0 H9 }$ d6 H3 L d6 h - digitalWrite(_DataPin, HIGH);# o7 {" v0 O9 j' n. P9 f
% I& U8 K1 |! P4 l: Q- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))* V6 J) P$ I9 z0 @9 v" R6 ^
- {
4 A: @" b4 c* T/ | - old_data = Sensor_Data[0];
0 @! a* u A! }+ r, k# \$ ` - return Sensor_Data[0];8 q' i4 a7 R* F
- }3 X! x; r2 x( A' Y6 j
- else
! K1 S& J) N! F% I - {( Q6 |+ _* A5 B6 E, ^+ v) ~
- return old_data;
U4 F R% ?$ U3 `1 ~ - }* S# ^0 n8 E7 z
- }
7 t3 e0 p# y" W. d2 ?
複製代碼 $ J; V, A2 D0 F3 `' Q9 s
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
0 R b( q% e' q4 X: A8 u8 u" w6 }! ]& c3 Y
|
|