|
|
" w& i" x# n& p5 }* a* L
- #include <Arduino.h>- j h: y' `% [- C& K
- #include <MeAuriga.h>
& k8 y" Y; i4 ^2 W - #include "MeLineFollowerArray.h"
: N6 d2 b) h9 O3 i F+ P
1 u/ \5 w% E: U. e- MeEncoderOnBoard Encoder_1(SLOT1);
3 J& Q% `; @ h- t: z( b - MeEncoderOnBoard Encoder_2(SLOT2);
; e) L: h* Z- N6 {6 h - MeLightSensor lightsensor_1(12);
) `2 y( o, y9 w5 ~3 i% f" f+ ~9 M$ h - MeLightSensor lightsensor_2(11);' \( T' } n* f# W& i- e6 B( Y- L
- MeBuzzer buzzer;
7 a3 F8 v, ~& G# _$ S$ J. I - MeLineFollowerArray linefollower(PORT_6);
( z/ Y! g! b5 b! g* H
V; X# I0 `1 ?: I" M- #define Error1 1
/ ]' e* {1 X; K0 ` - #define Error2 27 C7 r7 B$ f! ]/ f, M, s
- #define Error3 3! \0 k! A) S7 C3 A
- # I+ u8 [" o9 d4 h! C& r
- #define Kp 15
: `% K* s0 T" ^ C! k8 U, Z, A - #define Ki 0.158 C( r- Z1 G( h" d7 W& j, c7 F9 q
- #define Kd 0.03
7 t/ c* t! J; d# F - % M& R( E4 i8 k) V
- uint8_t sData;/ r; l# J2 W; t9 K
- uint8_t D1;4 p A' T. k1 I" q4 D8 J4 S
- uint8_t D2;1 N* H2 v2 F5 Q2 u
- uint8_t D3;1 Z4 K- N9 s# e* ?
- uint8_t D4;. B5 X: `1 R# z9 ?( e/ a
- uint8_t D5;
, i$ ?6 Y, W, d* C - uint8_t D6;
7 d* i& _. s, [ Z9 _ - % A3 X5 M% v9 ?4 V
- float previous_error = 0;
7 J0 D& w% P- E9 F$ p) \ - float integral = 0;* g) y) [8 r4 V
- float derivative = 0;5 V% b! T, y- s9 y" q
- int Speed = 160;
; X( q0 l% O9 {+ F2 p - float output;* f1 {8 y, ]/ h3 Z T5 U
- / Y2 X9 r# e4 w1 I0 A& y
- byte Left;
# G% C8 {: p# j
, q& m9 J& d. a8 U" [0 s; H3 N+ Y ^# ~- void setup() . Z. h2 a& p3 e% c/ j
- {' g2 N6 f! Q/ B3 g% @
- //Set PWM 8KHz& w7 u* y, T$ b+ u
- TCCR1A = _BV(WGM10);
: Z& X( D2 b% i" A# k% ~ - TCCR1B = _BV(CS11) | _BV(WGM12);( A! T8 [; g, |
- TCCR2A = _BV(WGM21) | _BV(WGM20);
* H7 u/ x3 _9 h( R$ A& y% x/ v/ Q- ]1 a - TCCR2B = _BV(CS21);4 w1 E1 Q+ D5 K4 l
- Serial.begin(9600);
( a6 Z8 j- O! ]' }' ?, U - buzzer.setpin(45);9 } d5 M/ P+ @ u+ t4 m
- }
- V5 z, Y2 X/ T4 o
5 J# T' Z% A5 g. F6 ~- void loop()
8 G" l- K- n8 U - {
/ O* U9 S0 n: U1 a - while(!((lightsensor_1.read()) < (10)));
5 [* T" g# K: G' ^4 t5 J8 D# X. _ - buzzer.tone(1047, 500);
u: e" B, j7 B: K# | - delay(500);
_' a5 S/ j/ J - do
3 S [2 F. m% p+ B+ t - {
! f7 U+ |, M, Z o+ I - int Err = getErr();0 T% e1 g; |. N8 p
- if(D1 == 1)! Q. W! u% T# X* l. O7 o
- {
! l! z; c) d$ R& X - Left = 1;5 O7 O3 {& e a5 U: T4 c; j/ ?- Q
- }6 D/ p: S$ q8 u* Y7 b
- if(D6 == 1)) J0 O O4 D% ]' }2 x
- {
, | i* n( F2 d5 {. F - Left = 0;, ~6 c B! r Q7 c
- }, d8 v- J" d% u2 ]$ |' v
- if(Err == 99)% R) H5 W9 l- B+ X0 s" u. h: \& z
- {
& F. I3 i+ r d: z - if(Left == 1)/ j7 m7 R2 a( y8 {# L* B4 s& t
- {
$ V" \+ r9 k5 Q$ s5 L) C - Speed -= 5;" }0 {+ i* O) y# B4 C* i
- moto(0,Speed);; j4 l: _; o2 n3 H
- do# v: b9 X" E9 y; z( O% Q; Q$ b3 q
- {
0 Q% @ K' I9 e& m+ G# | - Err = getErr();) H# E1 J- Y. [" o/ U
- }while((D1+D6) == 0);% @+ L3 F B4 r. I" m( N( Z
- }
0 `9 ]9 H# D' z; B - else4 o, Q) z1 ^* p# @& {
- {9 E/ d, ~# _, ~4 F+ Q
- Speed -= 5;
- A, w# u# {2 y. k( }% O+ ^ - moto(Speed,0); / g. l" w/ [" k' g+ E2 y
- do8 K: `7 ?* ?. e$ Q
- {
! B3 Z5 b: r f, B" p4 o1 y - Err = getErr();
' l! l3 n, T( P - }while((D1+D6) == 0);- e0 Z! [$ h6 R7 Q$ u: U) a
- }
, G; J" b {9 d; t - }
5 g! ]. k1 q' `/ b9 ? - else
Q4 [2 r% A0 h: h& T0 r( z - {# B5 ]# z9 F: |0 j9 u
- if((Speed < 160) & (Err < 2)) Speed+=1;
5 o. o. }0 z7 R+ F5 ~* |5 _ - if((Speed > 100) & (Err > 2)) Speed-=2;
# _) p6 K+ ^" l- Q' U& G - integral = integral + Err;/ p7 f9 G( Y s' E, C7 k# e7 @% X
- derivative = Err - previous_error;
% a8 g4 }1 _. Y8 n8 ^9 a0 P1 V7 O - output = Kp*Err + Ki*integral + Kd*derivative;- D2 ` z4 K% U
- moto(int(Speed-output),int(Speed+output));
" {3 b" t l7 u- L1 X- m) r9 s - previous_error = Err;, v5 b I- r# q1 Z$ J
- }
3 l: n9 U F1 W0 R& n* u - }while(!((lightsensor_2.read()) < (10)));3 G: S1 z" C9 u
- moto(0,0);) b3 m; j6 P' _" p9 }
- delay(500);
$ l- F0 Z/ G. t6 S6 f0 _' b - buzzer.tone(262, 500);
/ A0 g" ]" b8 B0 a, Y4 N3 ~9 E9 L" T - }! \8 N8 n( e0 v$ w; _
& F9 c+ _; n2 G, I6 c- int getErr()4 X& ~" M/ S5 G) Q$ |$ p! I( p' @
- { : F, O4 A- C6 @, F3 d
- sData = linefollower.getValue();, U* ]7 U6 k4 V6 {6 M9 h
- D1 = ~(sData>>0)&1;
9 B d# `/ k6 T4 V; E& h4 |7 f+ S - D2 = ~(sData>>1)&1;% A$ V% q! w2 r3 s- o9 _
- D3 = ~(sData>>2)&1;
1 I, ?0 P, ?3 ? - D4 = ~(sData>>3)&1;6 L0 r8 Z( U! t" h- u) i3 @2 b3 Z
- D5 = ~(sData>>4)&1;
# L3 {9 q! e! f9 b5 ? - D6 = ~(sData>>5)&1;4 E O% C* e% c: T8 t
- int downD = D1+D2+D3+D4+D5+D6;+ h+ L' H) a6 g$ X2 d1 U5 ]0 ] i: q
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);0 ~/ I' N: X+ i+ Z2 M4 m( e
- if(downD == 0)
9 s4 Y9 b# I1 x6 ?; J/ C2 K [1 s - {
' L4 e1 i/ S0 |& j - return 99;$ H! R; w; h7 \! X
- }
* D$ j% O6 ?+ x! F8 G) s% n - else4 B7 y: @# [( A! a
- {2 j6 r g0 s* f$ k H( b( E+ Y
- return upD/downD;6 }, G8 r9 K& h: i. K
- }
3 h( ^4 b' U' R+ j5 H - }
8 E: M; e) T2 s* g0 @3 Q - 5 M: k" z b0 m
- void moto(int Speed_L,int Speed_R)
. t- S0 {5 W) v9 C4 V - {9 ?4 J j* J: | E6 z0 s( r
- Encoder_2.setMotorPwm(Speed_L);) v( n4 O1 E) B- A
- Encoder_1.setMotorPwm(-Speed_R);6 d# l& G7 }8 v: w. `9 D
- }
複製代碼
: P4 v; z% Z$ IMeLineFollowerArray.cpp* v7 L4 ?1 u) Q. x, V( L6 H
- #include "MeLineFollowerArray.h"9 H( v1 i# ]% I! ?# v: y' @! j1 z, V
0 P6 U5 |0 w+ ^7 H+ }- #ifdef ME_PORT_DEFINED, |6 ^7 R" M; P& } W* E& ]6 V. d
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
) ?. w* v( f" u" Z& I$ I$ T |$ w - {4 {4 U1 G; \( t \/ P0 ~ K
- 8 l3 F1 L4 t* q$ C7 D
- }
5 x" F8 ~9 z+ I0 O. x - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)' |) J! x1 p* o! n0 J
- {
# ]' N7 m/ X0 }8 d: c( I& a - _DataPin = mePort[port].s2;. r0 a* C- X# O! w( R6 ~
- pinMode(_DataPin, OUTPUT);
; U; [' e, e+ c* S* t$ k5 A7 H - digitalWrite(_DataPin, HIGH);
/ p* ^. f1 a4 N: f4 z - }
% t/ c4 }* f0 _! n6 V* G, X2 ^ - #else // ME_PORT_DEFINED
: ~2 k) n' P0 ]" w+ O9 R; R - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin) G) v" l1 f' F
- {5 e- ] L, o$ m+ ~
- _DataPin = pin;6 O3 ?. P& D2 a, q/ W
- pinMode(_DataPin, OUTPUT);
% X1 l7 F7 D" M- y1 T - digitalWrite(_DataPin, HIGH);
$ x$ \/ x% X/ l - }* G$ Q1 q7 G) j
- #endif // ME_PORT_DEFINED$ J1 h/ ~$ B$ k# l7 {8 r
* e2 Q/ J0 R& ?- / R0 {, A4 r. k6 j
- void MeLineFollowerArray::setpin(uint8_t pin)4 b- y1 V1 k( R' q( O! |( \
- {6 j/ t% n& F9 o; H
- _DataPin = pin;
9 d4 `* c6 y7 Q( n4 [$ b - pinMode(_DataPin, OUTPUT);
+ T y- r* U6 ~. S - digitalWrite(_DataPin, HIGH);; d0 o! b% D* l+ X
- + r- E3 k2 I& v1 h+ O3 ^
- #ifdef ME_PORT_DEFINED4 e4 r. ^: D+ t0 H) u) i
- s2 = pin;
# h7 |% c( `: h( H - #endif; p4 b# Q9 i# v* f* {1 Q
- }
, Q3 r! G! c. D# t3 R/ P - ( P, f! m7 z4 o* Y3 {8 Q, o
- uint8_t MeLineFollowerArray::getValue()
! F7 Q, v+ D2 t$ p' G& t" o - {7 j: v" _7 u- W5 S! Q" y; h
- uint32_t LOW_level_read_time;
* y) m$ ]' f3 y, b0 Q - uint32_t HIGH_level_read_time;4 c, }" \3 W+ q
- uint32_t time_out_flag;7 y0 R# `2 y3 z+ V3 M0 f6 C
- uint8_t Sensor_Data[3];
% }- u( h, d/ t- t$ y! j# p - static uint8_t old_data = 0xff;
/ D0 T6 B0 T; e0 _2 K8 g
; H8 Y6 L) V ?- o, M' K! |- pinMode(_DataPin, OUTPUT);2 c+ r8 w$ l! z( r# Z5 z
- digitalWrite(_DataPin, LOW);5 M% |) z9 u( Z, T% f
- delayMicroseconds(980);
! ^+ D) E4 _1 C; ]! v% @% K1 V, R - digitalWrite(_DataPin, HIGH);( m- `7 Y% l7 x1 g- d
7 l. {" E5 w. N+ `( @- pinMode(_DataPin, INPUT_PULLUP);1 `" X+ k, t: S/ |/ R9 K
- delayMicroseconds(10);! `3 N( r: e6 `0 A, f0 @ G
/ E% Y a& B4 H' F6 M- time_out_flag = millis();" e* V7 j* w: @% y, |0 J
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
" o7 _, d) B5 w: f8 I; y
1 ^+ M |7 U1 C8 v! d- LOW_level_read_time = micros();
# d- k$ {! F4 {& S* k' k! @ - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
% K9 [3 ^2 o/ a - {- q- n, x) l. K; q
- return 0xff;
, y! A4 M0 Z+ l. O5 ?4 ^& m - }( i2 R" e% h. ~ J3 P
- / I+ `& u; z& c# ^1 e! L
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
0 B6 m, p" v: n( t+ r
$ c" I* D3 a/ J3 D8 ^8 U1 t. i- HIGH_level_read_time = micros();
. r+ t) J! _9 z- X6 A) Z - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
! S& M5 Y2 d/ L! Z- p" E
7 Y) O& O: _! U p0 c U" J- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out# |! h) M! ~+ ]* V- @
- {: Z8 P* w& f2 I4 X- r
- return 0xff;+ e+ N2 ~( A1 M& W6 b+ f/ K
- }6 C: a2 m$ n$ W$ e: Y: W' X8 `
- 0 l$ y- n H! y( U- ]
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
/ }+ X% q3 d! R6 r' Y. U - {# @1 E, S. N9 {2 V5 l0 R* y
- return 0xff;
2 E/ s3 X9 L' Q1 C4 d3 d: v - }
. m" h: S/ a& ?: e+ P8 c. P
8 {5 w) F0 e( l) }- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 P5 [; W# U- J v0 t( x
- LOW_level_read_time = micros();+ V6 \7 s+ m$ X! n, s+ t
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level/ t# W# Q* E! u( n, q
# b0 n! V8 e- Y1 R; B; H- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
: r/ _" Y& j$ v5 B; A - {+ N; |3 S' B+ |5 j9 a9 ~( K# T$ @
- return 0xff;
2 E9 b1 s- A6 C, ~ - }
6 k/ x$ Z+ @" E, B D - 4 i8 Z x" Q2 F& r4 l
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))# w( h7 T( f" V6 ^
- {
) `% `5 P' Y/ b7 S2 S3 E$ z - return 0xff;
" w) v( P2 ^9 q9 r, M4 l2 } - }) E# F; |4 q- w' M6 c
- 8 X1 w1 `# i/ ~8 \" v
- for(uint8_t k=0; k<3; k++)
1 Y0 {' L' F0 R/ K8 | - {
0 b" }+ t$ D# I+ l - Sensor_Data[k] = 0x00;) ?/ i8 O- D1 h9 K) _5 k
- $ I' f. f6 a% r
- for(uint8_t i=0;i<8;i++)
" W- D6 I) Y! g3 G - {4 [5 h' o% @6 [; h. N; M& c
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
( o0 @4 ]7 C! Y5 Z# K - HIGH_level_read_time = micros();
, ]. A) i/ e: `& H- R) ? - LOW_level_read_time = micros() - LOW_level_read_time;3 o6 `+ n7 h1 |: P
) q/ F; e$ q( Z- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
; O" i3 v" X% T+ U - {
; k/ E# l0 f3 c! g" o4 [ - return 0xff; w, I. Q/ ~; r8 B; Q: G& c; f) T
- }6 ~4 m. k- M0 l# i8 A/ u( [
- ; u& ?" `: N0 @/ x
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );! n" q+ y" l' K( Z" K
- LOW_level_read_time = micros();. R& f$ K" K1 t# f3 H9 P
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level u h1 q7 H( z, }6 F. T. v9 H3 k
- ( z0 H8 n& ]4 Y; J
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
: K, U5 D0 r9 E! W- L$ k! j! k" N - {
8 s4 ~$ C3 J# Z! @3 q6 H: K, } - Sensor_Data[k] |= (0x80 >> i);
9 D# M' K3 z) R9 j - }! q5 g% q6 B$ @2 Y: j7 l
- else if(HIGH_level_read_time >= 100)
$ C1 r- U) H; l - {5 ]' l& u, K# v; _9 m
- return 0xff;9 Q) J1 d/ E7 A+ e3 B2 n0 r
- }; j3 D7 K% A5 F, X5 `/ F3 ]4 K
/ N0 `' j: @+ }8 J+ V. Z V$ V- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)2 c: {$ l: H6 v* B( c* R* ?: r
- {
! ]+ W, P7 j/ _+ h U - return 0xff;3 |; l- G. E9 g: G( V7 _* g4 A
- }
) [! S4 ]+ @4 z u) F" | - }
; w0 ~3 y- j2 r: u S$ h2 L - }
6 E1 ~5 m$ |3 g- K9 k3 r; a, V) `5 J S
% w: a) s$ v9 p8 [6 m- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level4 d q/ ~" K8 F- c6 q, t* S
- HIGH_level_read_time = micros();
% H7 x3 E/ k: I+ x$ W* T - LOW_level_read_time = micros() - LOW_level_read_time;! @; q3 Z4 a, H, ? Z4 [/ _8 U
% {! B, y T- k0 n. }1 S* Z/ S- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
" r& A& z1 ?3 D i) W - {: B5 y: s/ ?; h' W
- return 0xff;
4 y" q6 f- J% Y# N) f - }
6 q' c: e: r! u6 I& p - # L2 N+ [2 ]' L6 d4 @" P# Y E" {
- pinMode(_DataPin, OUTPUT);4 c' G }9 w/ F0 z3 A( Z: A
- digitalWrite(_DataPin, HIGH);
1 A( P- Y$ y/ x( V
( u2 E. T0 y! u; X* L7 F9 I" m5 j- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
$ j, f6 S0 o. ]% I- @2 o( @3 H* r8 E - {
z' Y+ E* z9 `4 p$ D, l - old_data = Sensor_Data[0];$ G B6 t% x V4 z! \
- return Sensor_Data[0];& J5 L, ?' H! Z/ u& z; D
- }
- n5 `" M* L- H! y - else6 V1 w9 u1 i+ h$ {, {( N3 L
- {0 x0 f7 c$ t5 R2 k+ [
- return old_data;
l! R, N7 X# a; x& ]0 u; ^0 r0 y' h - }
/ ~/ T2 k% ?5 S3 `$ `9 ~ - }4 G& {4 P9 x7 \- l9 ^! e
複製代碼 & w, q$ g0 V& K5 u& C0 l
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
* R) P8 ^& p- |/ M- [+ y
- W2 |) w- Z; L# [5 G |
|