|
|
) `; b+ w7 z' T
- #include <Arduino.h>* L, e2 ?" Y* m" G; m9 O' |
- #include <MeAuriga.h>- @2 v. U+ b9 W. u$ M" d
- #include "MeLineFollowerArray.h"
, D$ R/ A& ~% [3 |0 C
1 Y1 Y$ g. n& W2 h4 f* O- MeEncoderOnBoard Encoder_1(SLOT1);
, p$ W6 \: O- c+ x/ N# q" S - MeEncoderOnBoard Encoder_2(SLOT2);$ ~* u1 P6 G$ T/ V. S3 j1 A6 F% P
- MeLightSensor lightsensor_1(12); d) u; ]! a# N/ Z k8 \3 j" Y
- MeLightSensor lightsensor_2(11);2 a, t* j3 {6 S# K
- MeBuzzer buzzer;( L; j2 W8 ]$ W I
- MeLineFollowerArray linefollower(PORT_6);
% V" ^1 R8 I& ^/ b - , S1 A& I+ P7 @
- #define Error1 1
& k/ |4 m# h4 X' f, k7 M" J" c - #define Error2 22 p+ x" @$ r3 Z/ f
- #define Error3 3
0 |1 ^3 P( ~# }' J ^
! h3 d: k5 s! w4 g+ v* }2 J$ _- #define Kp 15* [6 F6 n& z% E# y
- #define Ki 0.155 h% Z, _; I3 K
- #define Kd 0.03* J7 Q3 j5 \8 Y
% Q1 q3 z3 `$ e2 F- uint8_t sData;3 f( e1 N l5 ^0 N8 n
- uint8_t D1;4 ]" \% w B; l6 k( k0 q# {- X& {
- uint8_t D2;5 O n l, X, r
- uint8_t D3;
' A' y* ^9 z. L/ b - uint8_t D4;
+ @8 R# D" C; [ o" @ - uint8_t D5;
2 `& R. o+ g6 C9 y4 [9 ]4 d0 H - uint8_t D6;
' z* {' X0 S4 G+ M! }+ D+ X6 P - 0 G. K: F6 O6 V! E4 d: B
- float previous_error = 0;
0 X9 N) u! X6 s- j. X4 P - float integral = 0;
/ E: v5 X' x* b( [ ^3 u2 k - float derivative = 0;
1 v w Q" Z. B4 T - int Speed = 160;
+ ]! ]2 F" z' G! y! c - float output;
9 d* m! L' h% |
. j& Z: u% H( U' r5 w$ G# C( z& N( k- byte Left;$ ~1 \' Q& j7 c' c' u7 y
- v( T Z1 h: n. L8 n: K3 ?' H1 \
- void setup()
1 e) T0 G1 S5 g0 a - {
# E/ s' U- }3 N+ z8 D - //Set PWM 8KHz# K# @2 K! t; h% F& Y4 D, t
- TCCR1A = _BV(WGM10);& M' ` A& p' |- m, X
- TCCR1B = _BV(CS11) | _BV(WGM12);, N; v0 G8 y, z" @' B
- TCCR2A = _BV(WGM21) | _BV(WGM20);
. E. [) A# `2 Q! ^# D - TCCR2B = _BV(CS21);: u0 g P4 z2 _1 q' m+ |1 l
- Serial.begin(9600);+ }+ x* I4 z; F5 i5 j1 j5 l
- buzzer.setpin(45);
% y+ Z$ L# ~- ^; H - }- X3 e" d0 c- }; ?/ v
- $ M' W) U# b P+ W: j# P2 c
- void loop()- Q* V7 [9 u6 O8 i) |" Z. t
- {
! }& e0 N) J* k# { - while(!((lightsensor_1.read()) < (10)));# v8 v4 s0 x( j: B, x
- buzzer.tone(1047, 500);9 k6 b% O: G, ^% X5 [/ U: ?
- delay(500);/ f8 U) o+ ~" n. z6 l) h
- do. n$ }# K) k7 \" g) M9 l3 ]: c
- {
! n- l5 X% I" L. S! L - int Err = getErr();
1 N9 w% T6 u( w( H - if(D1 == 1) n! l' N+ P, p
- {3 {. I( _: C' p) W( E
- Left = 1;
3 G1 L5 b0 u5 t' F, Z - }
( u/ V3 i2 t" _+ W& n - if(D6 == 1)" d( f& t% w- R7 G5 v; ?* `
- {
. F% {$ T+ N, u# w - Left = 0;
/ ~4 l( p8 T Y1 w* D, a' z - }& e3 _$ `: ?- p/ j. P* s/ @" x/ `
- if(Err == 99)4 A/ v6 i# J( _. x
- {' @1 m# l/ h0 J/ l }' a2 \- @3 H
- if(Left == 1)1 y: o8 @- @6 X, L
- {3 T6 W$ \. p2 ^8 S0 I7 c, w0 P: K
- Speed -= 5;
9 p4 \( Y6 K0 U- Z- v5 L- J - moto(0,Speed);
e3 ^/ W$ b. Y - do
" l2 D% k& a2 x F - {
( {8 ] ^6 A& R* {' u2 y/ A - Err = getErr();
3 X- S# D4 K1 v! p6 [9 y* h' m - }while((D1+D6) == 0);; |0 h; w- p+ x
- }
. P: ?5 i0 t8 W# R3 t% ` - else7 u N7 _- C5 i! }
- {
; d# |2 B. z s. H6 n) L - Speed -= 5;
* x8 k5 H- I! D - moto(Speed,0);
+ D. F& _5 J5 E- i - do$ \' A( @5 o" ]2 x. o3 P6 T
- {! _1 K& u. Y1 m2 u I
- Err = getErr();' Q) I* X5 y# i# k
- }while((D1+D6) == 0);
# [( e, \3 n0 V% V - }
9 c; r# D* t/ }" S- ?9 \8 S5 g - }
: w5 p% l6 Y' E8 M - else' J& e- Y1 i C( W
- {
2 O; I; f& n! ~4 F" I S8 {% Q9 d - if((Speed < 160) & (Err < 2)) Speed+=1;
$ \2 h9 ]% v. b0 J - if((Speed > 100) & (Err > 2)) Speed-=2;
, q5 } g4 G& S. _ - integral = integral + Err;
& I' C- H' j" T; \$ o - derivative = Err - previous_error;4 v5 C4 \$ v% y8 i: V2 a* _
- output = Kp*Err + Ki*integral + Kd*derivative;
* B# H, u( c1 h3 f; \6 a - moto(int(Speed-output),int(Speed+output));$ @2 n" P5 @+ M7 u
- previous_error = Err;
. x! O( l) K! A$ ^$ |1 a, @ - }
3 A. D B0 I: g, y R - }while(!((lightsensor_2.read()) < (10)));, b2 A) } Q% k' o! q( ]* d6 C3 w
- moto(0,0);
5 x9 h( g7 V" i1 i0 V- ?4 a - delay(500);
- c6 B1 o! U( r+ d7 Q7 x D! v* d - buzzer.tone(262, 500);; u" J, Y- P8 R. H+ g
- }
( {8 P! Q! s8 q9 {3 E( ~8 [ - + x7 ]2 C {) Z& w9 }# u5 n
- int getErr()
7 A7 C& F# l! m5 z: X' F& u - { $ e3 h5 G' I) q- o
- sData = linefollower.getValue();' U# }: G- ~" w* k
- D1 = ~(sData>>0)&1;, u$ @2 s7 A4 H$ G1 z6 ]
- D2 = ~(sData>>1)&1;7 u( M: `1 J% m2 x: ]
- D3 = ~(sData>>2)&1;: Q: a% r! g" j% T x- _3 d
- D4 = ~(sData>>3)&1;" ^% p( V- ?1 J5 `
- D5 = ~(sData>>4)&1;
. [ Z0 G2 Z; x: `( _ - D6 = ~(sData>>5)&1;# V, _( k$ I, Z+ v0 f
- int downD = D1+D2+D3+D4+D5+D6;
) H2 Y0 I& s$ S$ g# V* M, a. l7 J0 Y - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
9 a& O. }* `$ f1 {2 K% O - if(downD == 0)
9 N; P4 B, f+ z; q - {- w- e6 K3 R9 S5 `
- return 99;3 F9 Z- J" Y) k; ]
- }* _& g9 d o w2 J& h
- else8 n: {+ ]5 I q2 V$ V! _
- {7 i) T$ A s, `9 Q6 [
- return upD/downD;1 W+ H \; Z$ v% m! ^ b" f$ u
- }
: T; f7 T8 d+ ^1 ?" Y! n - }7 B. i" N# j# s% b; f5 `( _
: _8 g" I4 R6 x' m9 J) U; i( C- void moto(int Speed_L,int Speed_R)
- w1 a0 U+ [8 k# D - {
6 [# u. u: r u8 y% Z# t - Encoder_2.setMotorPwm(Speed_L);8 z% J3 D- h j9 ~9 S5 T# Y- |
- Encoder_1.setMotorPwm(-Speed_R);/ ]. c& y s, [! a
- }
複製代碼
: p( l) w! z- p+ X8 JMeLineFollowerArray.cpp/ n7 L8 ~& ]0 W. A' S
- #include "MeLineFollowerArray.h"( e+ r; K. }3 X3 r) U) A
% Y- O, \7 \$ J& K' _7 W0 [- #ifdef ME_PORT_DEFINED
* D& a1 n9 j4 f - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
/ U' P/ j3 g" _" ~# Y; m i7 c- J - {
( O7 o& g! [6 ^ O( _
2 F/ w: K; @4 F. S8 L! q* I- }
Y1 M! Y0 o9 A! m+ z3 e1 S - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)* ?& s) B' J+ M% O- o! P ^5 X
- {2 i. j0 U4 O- e; Q& O; b6 {
- _DataPin = mePort[port].s2;$ O5 l$ p. P" W( A# K0 ?
- pinMode(_DataPin, OUTPUT);
6 i$ l3 E! J2 G% \5 A - digitalWrite(_DataPin, HIGH);( L3 a r$ T0 I U, ~+ M
- }
# `, { {# h @) p - #else // ME_PORT_DEFINED
* @5 W9 a5 H( {7 W - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
1 M: J0 K4 U' Q3 D( ]! { - {. l& h1 }" ]- l6 P1 @, w# _* _5 X
- _DataPin = pin;
8 r/ j6 e( v! G% F - pinMode(_DataPin, OUTPUT);
0 C7 V0 D3 n& ~+ | |6 I! c9 [ - digitalWrite(_DataPin, HIGH);
2 V6 y& E8 G y# X6 p/ f - }$ ^" b1 {$ f9 o; C
- #endif // ME_PORT_DEFINED/ _2 q+ _0 h# h' _2 ^# Q
! u5 I. h( r) {" l9 s. R
7 l8 l/ Z$ h# u9 K: W' g- void MeLineFollowerArray::setpin(uint8_t pin)' a: E* J6 y, k- n
- {
; g2 y4 M. b, J0 k* U: S - _DataPin = pin;
6 ?, f! T/ k8 t7 } - pinMode(_DataPin, OUTPUT); 0 q, R! L4 s8 a% z% A. R
- digitalWrite(_DataPin, HIGH);
5 `; A/ s; ]% ^% m+ I3 P7 | - 8 b# @9 D. e. x8 t2 @
- #ifdef ME_PORT_DEFINED( ?2 C0 g% F9 ~. M( {
- s2 = pin;/ a/ d0 ?6 B$ H6 G
- #endif
3 t3 i. |5 p/ S5 G - }
/ R0 S0 }8 W- w3 y9 g4 A
9 O, r8 ]& F8 Y5 }' u- uint8_t MeLineFollowerArray::getValue()& A! o/ q5 g( |1 O. k
- {$ k. j& o3 F) }2 W; k5 P! b& @4 ?
- uint32_t LOW_level_read_time;8 ?, j* f$ l( M2 T
- uint32_t HIGH_level_read_time;
/ x( T5 Q ]9 i+ B - uint32_t time_out_flag;
p, @6 N! ?; {- F - uint8_t Sensor_Data[3]; k: E9 W& |) [+ i9 x, w8 L( M
- static uint8_t old_data = 0xff;
4 T! X$ T: Q) u' e! B0 ~ - 3 d/ A1 _( L! e/ n1 `/ }
- pinMode(_DataPin, OUTPUT);
" ]: s/ Y5 c0 c5 U2 } - digitalWrite(_DataPin, LOW);
4 \8 C8 ~5 r5 D- A! ~ - delayMicroseconds(980);
" ]8 y" I* Z4 D5 M# H- z4 o8 U - digitalWrite(_DataPin, HIGH);, G" h4 _* e4 e
& u5 W2 x( f" Q- pinMode(_DataPin, INPUT_PULLUP);# v* I* ~/ C# o+ Z, J& V. |* X
- delayMicroseconds(10);
1 r& I1 d8 o9 M
* ?3 l6 Z! }; m3 }2 a9 W0 _8 C- time_out_flag = millis();1 V1 [) y/ o, u# l, Z W5 f
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
8 F! I8 x3 A H/ V5 ?3 H# {/ q
: N4 X) J2 W1 L6 k- LOW_level_read_time = micros();
/ E7 ]- d) }4 k3 B4 S - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
6 L& I4 e) p. D2 S$ j# ^8 Z, k - {
' O$ s* f( [* X. s$ q: e2 h$ K% ~ - return 0xff;
. i1 a* q# E$ M - }
3 s) L4 Y0 x- ~9 b6 G8 |0 \: R - 1 F; t: B% J9 `4 f `( B9 y
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );% B) _/ _1 ^% q
- ; b2 S: f8 H7 ~- u# q
- HIGH_level_read_time = micros();
/ E7 W9 }8 p0 L. |! K - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
4 m0 f4 E( n p _3 f( l( I - & ?+ }3 t6 `) R2 U
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out2 @9 J+ h( {4 K5 {
- {
4 d8 X; q% y3 [" G% N- f - return 0xff;
! d0 u# p& c0 [/ @. |9 h; O - }1 s" T* L' E3 z+ b4 Y5 [
- % Z( C; p$ P1 u
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))5 P& M- @; u m* @2 d, U
- {
! a# e* u1 g% t$ O) H% f - return 0xff;% ?; l* q5 [0 \+ C1 m( ]
- }2 x& I. r2 u, p4 Z( l9 X/ g
- . ]0 C7 z' ~9 W
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );0 V3 S: ^, ]" b+ u: X
- LOW_level_read_time = micros();
: B% q H4 r# l* F9 }" l& }7 w - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level$ k2 g. u! f& ]' F- B/ ^/ m
% G$ D3 u+ E' Q- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out' p) a1 x `( Z9 t4 E/ S
- { i5 B+ V: d# Q4 `) P$ R) f
- return 0xff;. I) |- `" g1 d/ T
- }- }, g v# m! z$ T3 j; h( k
% N; J8 N0 v! @; W3 T" ?- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))3 _5 f) m( H% Z$ u& k
- {9 e( W6 p5 W: a/ _: E' S8 N! N2 g+ ^
- return 0xff;
) C! a) C+ F! @( |3 z b - }
! d& e( i, K- v2 L) O, q' }7 }& Y
- e+ e7 `4 @! t3 U1 g- for(uint8_t k=0; k<3; k++)( V( g' U( l( z$ Z- ?" J
- {* U# Z+ M. X( W/ R
- Sensor_Data[k] = 0x00;
) q- R- r0 ]! D2 X6 Y - ; ]( m+ E* L1 k, z
- for(uint8_t i=0;i<8;i++)0 b3 |& L; X8 R9 q
- {
$ K' E$ ]/ p9 R, P' ]3 U U, @6 F - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
g* n) x" J! i O& C) G - HIGH_level_read_time = micros();' ]) c$ e0 \; }8 a
- LOW_level_read_time = micros() - LOW_level_read_time;8 e' b! ~0 s; r! T; s, s G
- # {2 ^1 G$ j$ {* I3 \
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )$ d5 ~/ `- [0 W! \
- {& A1 ~6 C, v1 D' I& ~
- return 0xff;
' d* r7 B6 Q: k, P% c' }& r+ U% ] - }
, H- Y8 q& ?% n* J& |
# t5 u" `8 n' O, n- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 ]# s4 D& m# m) ^( k9 T1 j
- LOW_level_read_time = micros();# n, z3 D+ j) ]& H
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level- o* [1 c# l2 E! P- D m1 t
3 o) H2 W% r- L4 b- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
, K3 \7 V, |- K( i$ | - {
% h- C2 O; R4 P h - Sensor_Data[k] |= (0x80 >> i); ~- t, x( Q: \+ r) u! _
- }
' b. X: ]+ c* i" }5 |+ T3 z - else if(HIGH_level_read_time >= 100)8 x& J/ P9 f* F
- {1 [/ a9 _9 j8 b) o0 q% g
- return 0xff;
1 z, {/ t" c# l4 j6 \# k5 e - }9 l6 D/ s2 ], x' c0 ~0 K$ U/ j
( ]2 _6 I% U Q6 Q- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
2 u2 q9 `- [$ I8 O% U( O - {
1 P$ M0 C! o0 a a - return 0xff;
7 ^: i- B5 ^. q; F6 ^3 Q- k( d - }4 o8 P0 t2 d% o( i. x
- }
/ ^& r5 Q* n+ g9 r# Y - }
) j. Z1 ^) U. V, K4 M - 0 M. a. o6 |' g3 J$ t+ _
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level+ C' \ v0 }2 b" ~, ]7 {
- HIGH_level_read_time = micros();
; ` ]: y+ ]3 a$ }' Y; V) h. M; t4 } - LOW_level_read_time = micros() - LOW_level_read_time;
; n1 P+ O& s9 T, ~" T; |
5 k" L9 `2 P" o S3 J9 c+ a. \$ l- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )2 G4 _6 H4 V4 p7 u
- {! H/ @% I- r& W9 i" v
- return 0xff;4 O$ ~( n/ f g* K+ W' o
- }% J# F' v/ h d
" a5 `! k- i- \. s- pinMode(_DataPin, OUTPUT);
" b; v; ~+ w4 i5 n( i% i( k, N - digitalWrite(_DataPin, HIGH);
$ ^3 R* R$ v d' U4 j - 7 T7 Q# _4 b7 g4 I& S3 H
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0]))) p3 E+ m" @9 I7 ^# ^3 M ^" w
- {3 X8 |3 |1 r8 L r- G$ x! t, J
- old_data = Sensor_Data[0];
$ W+ x; g% Z2 \% y- c) @ - return Sensor_Data[0];
, f7 }$ `3 U$ q. h4 t - }
E, a# ~( J# n! O, a - else/ o$ `# S8 w! P
- {
; b. H4 }$ G8 l3 n7 N - return old_data;& R1 g' D! m) y2 t! E
- }+ J0 O/ U. v8 D! [1 ?
- }# }0 \4 `9 u) x9 Q, W% N( H$ ]( |7 j
複製代碼
" p' [1 O! O3 y6 M' T9 QMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
/ \% S$ p% ]& U, P) u0 Z r/ K. h
- F* B7 z% y. M* k9 K
|
|