|
|
+ s* c3 ^2 q$ x' v
- #include <Arduino.h> ] Q$ d# j9 `+ Z3 r' h
- #include <MeAuriga.h>
/ b1 E2 ]; y9 C, ^ - #include "MeLineFollowerArray.h"
' H% K4 u6 S/ j+ X4 j! O, S
$ V8 l- V1 q! n% C( P( D- MeEncoderOnBoard Encoder_1(SLOT1);
, ?: u7 L! ]4 C, A q7 S. B6 r$ P% Q - MeEncoderOnBoard Encoder_2(SLOT2);, @; k0 P1 w* n, Z* D- a
- MeLightSensor lightsensor_1(12);
{; \/ k1 H0 `; C3 n! `9 D$ ] - MeLightSensor lightsensor_2(11);( c: i0 u# l! X9 k( |2 q9 \: d+ H
- MeBuzzer buzzer;6 C' S* n% o5 g* G6 A5 x
- MeLineFollowerArray linefollower(PORT_6);
0 s- b' f7 i* E5 J - : L* _" X+ k+ o% g5 }
- #define Error1 1
' y/ r( f! K1 d, e6 B1 z7 o - #define Error2 2
/ R: H- s5 [7 U; K9 N+ d7 _ - #define Error3 3
K( v. Q) Q0 }
6 E; k3 E" n) X" _! M4 C8 i- S& f- #define Kp 15
] n b- Z! _* o' _ - #define Ki 0.15& f- E) N4 n: N% \
- #define Kd 0.03. F/ a) D/ G9 m' S; U# T+ Z4 F
- ( F& e" \5 i/ w- }6 x
- uint8_t sData;" ~/ z; b! I. h& a+ n" l, K' q3 z
- uint8_t D1;
2 h9 G. W+ b2 X+ l+ p5 a/ L - uint8_t D2;2 y) h# o+ N% ^, m+ b4 ]# ?$ o
- uint8_t D3;
5 c H2 P6 n8 X2 K - uint8_t D4; n4 g X) A/ x; T9 y
- uint8_t D5;
5 R, [. n4 r; @; o3 }! r N6 E3 E9 Z - uint8_t D6;
$ n1 ]) n7 K* I% U
# K2 {1 ~0 U5 l: y0 Q& q2 u) r- float previous_error = 0;
+ o, b* i: U% N5 z5 X! v0 {; } - float integral = 0;$ t }! M' W4 I2 Z& C# a
- float derivative = 0;* j% w. F- k2 r
- int Speed = 160;. M2 X1 S0 r! C) {3 m I! U
- float output;( J: Y2 x( R P* ]7 L5 i1 l
* D; x4 K( p' \; c- byte Left;
! g, e* V, f2 t* t- P$ u
+ a" H3 i; l: Y. u- void setup() 3 q$ a- x( e9 ^2 x; R& W' U& y
- {
3 i1 W- g5 X3 b% X# M) Q# |# u - //Set PWM 8KHz/ k4 u+ |) D2 \! O" E9 Q( _2 C
- TCCR1A = _BV(WGM10);
$ z4 x$ T$ Z+ m! c' b) w - TCCR1B = _BV(CS11) | _BV(WGM12);& C: I) l; g2 R
- TCCR2A = _BV(WGM21) | _BV(WGM20);* O- H4 h; [2 m
- TCCR2B = _BV(CS21);
" S8 |, [+ g d, M+ S; n6 I - Serial.begin(9600);$ V3 }4 Y5 R% r( S4 e6 N6 d1 n
- buzzer.setpin(45);: D! m) K4 n) @
- }7 q+ P) k3 L3 z- [ W2 `* v1 _
$ _3 B z5 Q2 n- c+ j, f- void loop()9 V, x0 A8 c1 M" h
- {
. X+ J- \" D1 R0 m0 K0 R* z - while(!((lightsensor_1.read()) < (10)));
# h: X* ~6 y5 ?7 E - buzzer.tone(1047, 500);
# n0 Z2 U* l( s/ [$ _ - delay(500); A" R4 u# ^9 H! m, K0 x$ k+ |
- do* q D; Y/ s& t; }2 a
- {
* q% m# v, C: P - int Err = getErr();+ K% t, l, u& _- Q6 a& Y5 b
- if(D1 == 1)
" `+ j9 A3 l2 {, T# S2 } - {; w; C: f$ _1 N E. {
- Left = 1;
3 X m- D1 r1 x# J! {4 L - }1 w* b9 h) p& u$ y" a* J
- if(D6 == 1)
o9 t0 f, D! o* g* j - {
2 W2 B6 X3 k! X3 J/ u. s/ n - Left = 0;
9 ~" p) b E3 T" L' V - }
' r+ I# V, Y, L8 g# G) h, Z - if(Err == 99)
. d {, d0 M, a* ^% E - {
9 Z% D% o( j; z, J7 I9 ] - if(Left == 1)1 G" A6 r2 l6 \
- {3 L3 s- H3 W0 ~% c# R& u5 g
- Speed -= 5;
4 x- Q8 Y B# U7 e3 P - moto(0,Speed);! ^3 y, T) E( [# \3 [
- do
' G+ m5 h" l, k! h) i% n - {9 Q: t1 R$ Y f1 n# p* S
- Err = getErr();: H" W8 z8 A% y' Y- k( |; {0 Y
- }while((D1+D6) == 0);4 |4 p, P5 O5 _3 s
- }
6 M: {$ T9 Q! s# R( w8 ? - else
% U( [* }2 c: Q" i5 \$ N - {6 H0 ^7 ? ^& f: o
- Speed -= 5;4 B* J* L) P8 a! z \
- moto(Speed,0); 2 a, N0 Q9 |9 `) [
- do$ ?; {- |+ d* x7 e& d( H8 J
- {
" `. O, i4 M% a - Err = getErr();( K$ S5 C* W. ^4 E" b
- }while((D1+D6) == 0);
9 K* k5 l* T5 |' W2 |2 c - }6 I! J$ A9 k' `, k3 i
- }% o4 ?, e& C( G% R: A% b
- else
2 T' D" r8 m( U& u8 u - {+ h# R4 N. X% ^
- if((Speed < 160) & (Err < 2)) Speed+=1;
& Q( K% z/ M/ @8 @ W5 ~6 w, i - if((Speed > 100) & (Err > 2)) Speed-=2;
! B: S2 y9 r4 t- O9 c' a6 C6 p - integral = integral + Err;
2 W, Z5 }- h1 `1 f- e - derivative = Err - previous_error;
& B2 z& i9 M8 i! y7 _ - output = Kp*Err + Ki*integral + Kd*derivative;6 O% z: j( A" J8 m
- moto(int(Speed-output),int(Speed+output));2 o/ {9 U" Q M5 u( g
- previous_error = Err;
( z, S/ j8 W/ y' ] - }
! D5 J4 W: |+ _) u9 B$ I - }while(!((lightsensor_2.read()) < (10)));
3 h: a" B+ [: z! [4 |$ C1 t - moto(0,0);+ }4 P, d( z7 U. |
- delay(500);! T2 B9 t( E' ?2 n0 M7 i5 b
- buzzer.tone(262, 500);
2 b0 l! r( S0 c8 `1 E& h8 R% V0 M - }
* V' T0 o: \. I* }: l/ W# x+ t
0 h( O* r$ w( U1 z! ]# L. @- @- N+ s- int getErr()
* V% S+ ~! y9 L# P5 q, O5 ^# r ^/ v3 m - {
. U6 o, l" V9 \% I! w- y - sData = linefollower.getValue();
* _5 X5 y+ z7 g5 J - D1 = ~(sData>>0)&1;
$ f; Q" R, c8 Y - D2 = ~(sData>>1)&1;
& E: \; L# M$ m/ ` - D3 = ~(sData>>2)&1;1 x% d1 Q$ i' W2 H. r
- D4 = ~(sData>>3)&1;" a4 A9 j& ^; ~' n% a; E& A) T1 `
- D5 = ~(sData>>4)&1;
. p& [9 @! r7 x% P4 a( w& H9 {4 X2 x8 A - D6 = ~(sData>>5)&1;, W( V \+ r1 D2 W7 s
- int downD = D1+D2+D3+D4+D5+D6;7 Y" ^% g, U5 @
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
) q3 n: K% f' D! [9 m - if(downD == 0)1 a/ o j% k. p9 ?* F
- {
0 d3 {, ~: e) Y2 L- h' D - return 99;6 S: L4 l8 A5 B+ X1 J7 {; S* R! X
- }
2 b1 V7 f* Z+ \2 N1 V' k# Y - else
# M6 Q! M8 u# J9 a/ b, T8 T0 z - {
! w3 Y, D) y' h" B - return upD/downD;/ L% z/ Z+ F; A
- }- k" `. o/ D+ ]4 a' {" }! ^" B4 _
- }- B$ f. K: L5 f" W" A O
- % L5 K- V; o2 G* p
- void moto(int Speed_L,int Speed_R)
: |! x& T% C# j/ ~1 [4 { - {
- I% q* j0 [5 [6 V5 w0 N# T - Encoder_2.setMotorPwm(Speed_L);
2 f& h% {- r6 g& p1 }6 ` - Encoder_1.setMotorPwm(-Speed_R);+ v9 w( d# t5 I0 g3 Y
- }
複製代碼
" J& a* U' m5 W% z4 f$ ]MeLineFollowerArray.cpp8 Q* `5 x# U/ D: w f
- #include "MeLineFollowerArray.h"
3 x3 q# A8 \" o6 C
2 y0 K9 {# @) p3 w- #ifdef ME_PORT_DEFINED, u5 o$ J8 T5 Q. j( {
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
- r' z1 L- D% Z/ J" M, Y - {
: C& b5 u: ~9 C. A, a: ~2 j - + n1 f; c c" Z }! m
- }# |/ G1 K& t# Z$ H1 m- A l
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port) H; _0 l9 p; e3 [4 w
- {5 s$ v1 t0 n) D* f1 p M0 B4 v
- _DataPin = mePort[port].s2;3 c4 z5 c8 W2 q; t8 W
- pinMode(_DataPin, OUTPUT);
. \( y2 @ U! L - digitalWrite(_DataPin, HIGH);+ k! B" n: \. ^* a# N
- }
. ~6 ~/ E5 B0 l. z2 u8 Q8 s - #else // ME_PORT_DEFINED$ C' ~' b1 r! x5 s
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)9 T! c6 P" S) F8 V' O" d: n
- {
. n* k7 x" O* e - _DataPin = pin;7 c( x; l, f" G
- pinMode(_DataPin, OUTPUT);
\- j4 \5 ^4 p1 F( _8 R- \ - digitalWrite(_DataPin, HIGH);9 Z, N% n) t+ ]% l5 e
- }; @6 l' {& `. O t; o+ J/ G& L6 V
- #endif // ME_PORT_DEFINED" h0 I5 |- r3 Y; c* h% v: R' Z5 k
6 S, O/ @2 v8 t" ]- - }5 Y0 {$ x. V) J- L0 B
- void MeLineFollowerArray::setpin(uint8_t pin)
: }4 t7 J& l6 h - {, I" m2 E+ u5 U# _
- _DataPin = pin;
) p1 e8 b5 U7 J$ u6 x" |& c - pinMode(_DataPin, OUTPUT); & N9 a& q# V5 Y7 D. n+ ?/ C
- digitalWrite(_DataPin, HIGH);
! c, O3 {8 H4 U) Z - . k) u8 [ x( b( Y9 F) R
- #ifdef ME_PORT_DEFINED- ^. Q5 K4 y- V# B- ~
- s2 = pin;# I* g2 s. b' I+ H8 V
- #endif7 G) o3 [3 d- a w+ u
- }! y9 y6 b' K, z9 ]; i- b# `) S
* o1 E. A6 {/ v7 C' s6 v- uint8_t MeLineFollowerArray::getValue()
& m# R: i, N Z% @' p) r - {
" D0 Z- W/ Z. |) ? - uint32_t LOW_level_read_time;6 Z5 e' r% O! _! C; Q, y
- uint32_t HIGH_level_read_time;: n+ g4 } I# N& [# J' V
- uint32_t time_out_flag;/ k5 f' F f' {" A8 x
- uint8_t Sensor_Data[3];1 p: V& t' P- V1 `8 s
- static uint8_t old_data = 0xff;
7 ~; U6 a# a+ S0 ?$ |) j6 Y - ( F6 ~& u# z/ i- `1 o+ j# A: j
- pinMode(_DataPin, OUTPUT);
* q. B8 y% R1 r: K - digitalWrite(_DataPin, LOW); v8 Y% D- x0 p& [( Y
- delayMicroseconds(980);
{& m! Y' U z# V) A) n+ x - digitalWrite(_DataPin, HIGH);$ w1 o/ w" Z) k2 C+ T; s
1 d0 k* ~5 l! G. N- pinMode(_DataPin, INPUT_PULLUP);, |* d( r% u; V8 i: i+ d
- delayMicroseconds(10);' o" {% [* F( Q$ a4 v
6 O6 t/ F) g6 y% q0 ^+ S# X/ i( P8 w- time_out_flag = millis();
, q5 v# @5 L2 s; C - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
* {! |$ ]/ R, z' E; Q
) Z; [/ N, H$ Z3 o7 a- a- LOW_level_read_time = micros(); I1 j; g" X$ M6 z
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out T! C; x& D; ?6 g3 ` R
- {
8 t% s( h4 `: L5 f% \# h" F7 P - return 0xff;
2 V# ^0 }% n' A( S; I - }
! \* a2 e' V; W- G. _# H
: q; u+ W! i+ C1 O4 _0 `. F9 J o8 _- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 y( n. g$ ^7 e2 W+ Z
3 G2 O5 D/ ?( V {- f P; c- HIGH_level_read_time = micros();
4 L: ^0 E2 o. T' L - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
9 M% V; S% f6 `$ p. ?( u; y
4 {" i# `6 S# a: j- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out! n& K6 J3 \7 r" @1 o5 U; F8 ?: d5 c
- {
: |4 d/ a( `( b! [( Z2 J. v - return 0xff;3 K+ M! e3 ^) v/ \, |. |$ t6 P0 y2 q
- }
# S6 E \# O0 y; | - & F1 N7 \# e& I+ P$ m
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110)). x6 D1 W0 c/ i2 V3 N- k; f. ^2 t
- {8 _: O! R2 {, K: B2 Z H! [5 v: u
- return 0xff;
0 m% v- N5 K/ J8 ~9 O9 c! N: D! | - }3 N, a5 ^6 {0 ?' S6 {6 s
- ( L1 J" v+ C7 ?7 k
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );; ]1 b4 g* l: D; Q
- LOW_level_read_time = micros();
) a1 Y) c/ d2 _$ C% I: n6 m - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level# ~% N, P3 O4 G6 n, g2 g# b8 E
- , H6 {2 y. b6 i& M" ^- ~
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out) u) H2 P5 R P! @* L
- {" Y2 g5 z; }' k) E
- return 0xff;0 W r: S% U0 h/ Z$ w9 g% s
- }7 D* X9 B1 G1 x
- : S6 E5 u+ z {6 B
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))9 V- v: A" W6 f. W l
- {
. _' L! p0 F/ M- z5 v - return 0xff;; y4 n2 ~; B$ z- T! {, p, k
- }
" n0 `0 B) h- \ t4 {/ M
. b' [% F; s8 D3 u- for(uint8_t k=0; k<3; k++)3 {* @1 k* }9 R; D
- {) P. R" J* c6 z
- Sensor_Data[k] = 0x00;; a/ H+ T- i& k4 j
- f N7 z9 t! J
- for(uint8_t i=0;i<8;i++), s7 q% Z- M U! j5 Y6 J
- {. L; L. `' s W+ q$ P: [+ c
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level O& |3 o- C. G. p
- HIGH_level_read_time = micros();* T- {8 y) H) u" K3 p7 C
- LOW_level_read_time = micros() - LOW_level_read_time;$ Y: _0 z9 U( k
% i! ~0 r8 r6 x- Q- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
6 j3 z% V' x/ H X - {; j* i. T+ [4 K& c6 G' z& n7 Q1 |! j
- return 0xff;
$ n b) b: j3 ?; z - }% d3 I* N4 w O* s. a
- ) z3 Z+ J2 z5 r, ?# ]# u
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );; E }3 c5 }1 w7 q$ S; s9 ^) B( u j
- LOW_level_read_time = micros();
8 I% ?; o; L. q t - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
8 F2 S! n; u- j% C" r2 _ - ; }' l3 P' M/ E2 z' s) D
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1; c: c- w" ~) X& J/ y$ z
- {0 k0 I. r; s& O* V/ [& N
- Sensor_Data[k] |= (0x80 >> i);* S+ u' \' X5 C" }! K) j0 \! i/ z8 b
- }
( U8 G9 }' \% ^ - else if(HIGH_level_read_time >= 100)
7 z( ]$ ?" W H/ ^( u1 g - {
% V! h" W4 ?; |+ g - return 0xff;3 a) d) ^7 H# K: W- F
- }
7 Y( `; f: q* J# g0 @' h- h3 l
! A" D3 f$ m$ o' T1 Z- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)+ k) v" t, \5 B* H
- {
) I. h/ P! G5 }& B4 I! o9 c - return 0xff;( H; J, G1 w I# u0 X' X1 ]( O/ M1 p
- }+ T0 B" A. t- B- o$ S
- }" Z1 O( M* ]" p+ U- W8 j( }
- }
! D( x6 \2 N* _+ C# w9 I6 Q- @. N
" _6 `5 s. O5 J- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level6 i F! G5 ^9 ^: P
- HIGH_level_read_time = micros();# _& h: c* b; w' S) O/ f
- LOW_level_read_time = micros() - LOW_level_read_time;
+ J/ B. R6 `, V0 Q8 t! t: u
5 U# K+ N% B4 R) G& I- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
F. E$ q' D8 J- U+ A8 z( _ - {
/ ^9 Z3 |5 j5 V. t - return 0xff;
, m# i p! s( }: n5 b( `0 ]+ j - }
* K( g, ]7 B4 F( C% T - . B3 y' Q% A l% s# s
- pinMode(_DataPin, OUTPUT);9 M" Q% s- Q5 J v
- digitalWrite(_DataPin, HIGH);
' ^+ Y9 q& `9 P - ( U( X5 `& s2 i k- V% l7 O
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
: N$ {. Q) Y; c: ~! L+ B8 W! U9 E - {' S# o, Q) m# W7 F' ]
- old_data = Sensor_Data[0];
5 y2 K1 `' f9 T- m3 F& p, g - return Sensor_Data[0];
1 Z* u* _+ M0 o) n% i - }! _1 Z5 l! _9 b7 Y; `
- else1 z; ~" V8 E+ d! {, @3 B
- {' I$ r6 N/ I1 y& f% z! @2 v/ R% T+ X
- return old_data;! Q; Y+ y8 l* Z
- }
7 H; U" l m8 q& ?# l { - }
8 B9 [) v: y" A5 y" _
複製代碼
8 L3 ?6 u, M/ N2 z$ L& C1 pMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
, A6 Y! N# Y# f9 i5 u" ^
- R- B4 L5 ]$ P& D7 ]# j |
|