|
|
3 `! B2 R8 A& W$ }0 z$ R7 x
- #include <Arduino.h>
" U4 X0 p! e/ q$ o" d2 Z9 j/ ] - #include <MeAuriga.h>
, }* D- d* {% z& F* C R2 ^ - #include "MeLineFollowerArray.h"' z3 h" |- w5 u. V
! ~, M( P7 [8 O# v, W; \- MeEncoderOnBoard Encoder_1(SLOT1);; P+ j; }4 }& z7 B- V( z& e
- MeEncoderOnBoard Encoder_2(SLOT2);% [5 Z* q; v- U# H9 R3 k( Y! `- r2 _4 T
- MeLightSensor lightsensor_1(12);
8 ~& o* z6 T. Z5 W: j& S6 |' U5 j( N - MeLightSensor lightsensor_2(11);4 M9 |1 I& Y4 p4 `9 B; A8 u1 z
- MeBuzzer buzzer;- Q7 H; i$ V3 ?/ X7 S( \" b
- MeLineFollowerArray linefollower(PORT_6);
Y1 ~5 @/ S5 w# X$ b, u
|* F# e1 U$ r# q- #define Error1 1) r: N, t6 A9 [
- #define Error2 2
1 K0 Q) c5 i: ]" c$ H2 q0 n - #define Error3 3
5 L9 }+ R& Z0 i- L, _, N J
! q9 E; T7 k, R/ ~: l; ~# c- #define Kp 159 \3 E5 n& ?/ Z$ O, x+ \- I# h
- #define Ki 0.15
6 \' K, u8 V. e7 m r, o% U9 A - #define Kd 0.03- t: c: d9 s7 O( i3 s6 G
) V- y! I% n' u$ |- uint8_t sData;
0 j. g( I; `% }7 ^* t) k - uint8_t D1;
2 {/ S* e5 e* u. C - uint8_t D2;2 G8 m t5 Y! Z& r: @! y) p
- uint8_t D3;
% b; L6 b; W1 q' T; J" N' z - uint8_t D4;
2 @( p! W1 I3 y" A/ [% w) j$ \ - uint8_t D5;
0 G: V+ k1 o+ r - uint8_t D6;
* m- G& z5 B2 e4 ~8 O5 W
/ \5 ^+ b) ^/ r1 j0 J: c- float previous_error = 0;
; v+ V/ h; y. {6 Q- T. q! C - float integral = 0;
; E# P \! C- Z3 _" Y4 [ - float derivative = 0;' f2 `% b F, Z9 {8 C, q
- int Speed = 160;& x! Z7 Y3 d4 Q" y$ C
- float output;+ C$ N4 [0 P1 P- \0 B% h8 c9 @- l
& e: R% H; J$ Q. w- byte Left;! W* y" p9 ^1 Y1 k4 V# w
5 W: b6 C/ I: H6 t k! s' B& ~- d- void setup() 5 X, K3 c; a) H R4 B
- {
; {" t" |- A7 m# ^" F$ { - //Set PWM 8KHz0 D) |1 ?3 W% `3 s. \! Q
- TCCR1A = _BV(WGM10);. W- T* y- w4 ]
- TCCR1B = _BV(CS11) | _BV(WGM12);5 X: L7 _5 Z9 u0 {
- TCCR2A = _BV(WGM21) | _BV(WGM20);* d/ y1 }0 A6 r0 n2 n- D) z
- TCCR2B = _BV(CS21);
) R: i8 H- q6 X, o7 h - Serial.begin(9600);
* i) P$ v& n9 e$ W - buzzer.setpin(45);
R% O- z. N/ k) E' w - }
8 W' \) O8 _6 e; I4 \
- m7 l9 O4 v5 m/ i7 [- void loop()3 n$ z& x4 m& V5 B' \5 I7 z
- {7 U* g. G- y$ U. F" w$ w* O- x0 T, z2 U
- while(!((lightsensor_1.read()) < (10)));
" ]4 f: U- U4 Q6 D - buzzer.tone(1047, 500);0 S; l8 M h4 M7 R
- delay(500);
! m) R- ?7 |% o - do u! x8 c" u* ]9 J
- {% l" F* b0 D$ s
- int Err = getErr(); u0 z2 l! Q; J7 W5 @$ T' g
- if(D1 == 1)# O2 _* c) [7 Z; N/ U. C, c8 J/ V. r
- {
7 l; @, i* N8 Y7 J - Left = 1;- C$ o3 {; u; y- s. \
- }
5 E% z1 p0 ~# p; d4 ~; c - if(D6 == 1)& ]: c3 C/ @2 Q8 o' D
- {( b3 U. B6 r' n' T5 m3 N
- Left = 0;5 a: I8 O3 b$ `( K9 ^
- }+ ~ ?9 a9 F! b- L" I$ y# U
- if(Err == 99)% h+ j3 L9 G! }2 N& j$ l
- {
% o$ Y4 V: B. z6 |) Z - if(Left == 1)
! D8 E3 y T# ] c5 t; p - {1 B6 E+ r4 S/ r; f; v5 P
- Speed -= 5;
# t1 `( O+ ]( l6 Y - moto(0,Speed);
# @% z# s! P- @8 p - do. m! f. e S/ e* N
- {, m. Q/ R: C3 }& y8 c
- Err = getErr();
- Z8 S5 `, N. g - }while((D1+D6) == 0);& [. j3 G5 K2 G" i7 B
- }
. L$ i6 ]% w z4 A - else
" k4 ]1 }/ l: R/ k5 g+ n - {
7 u& C0 E* ]$ g8 G. [8 n - Speed -= 5;3 Q0 @) `3 o9 \6 X: f- ?3 b2 o; N
- moto(Speed,0);
' h' D0 _+ @6 d& j0 x - do
; q9 C1 i f) l& t3 A9 M( E - {; ]/ M, C) { s, R! U, p
- Err = getErr();5 M: l$ Y& A/ Q
- }while((D1+D6) == 0);
2 ]) j, p! Y' L: V" L" ~" R! Q - }$ u7 f# v/ ?% d4 K6 v
- }
$ h- W% |4 }3 ^2 ~ - else
W) s8 O: h) Y3 X' v - {0 `* `/ [0 R. K; J; Z
- if((Speed < 160) & (Err < 2)) Speed+=1; % r: B( H* i" n7 O" @3 c! L
- if((Speed > 100) & (Err > 2)) Speed-=2; / V/ \3 m. g" D' { Q+ x- H6 d7 ^
- integral = integral + Err;
2 |1 }( |; F( c1 C* Z - derivative = Err - previous_error;
- n2 A* T3 V9 z - output = Kp*Err + Ki*integral + Kd*derivative;
7 W/ ^2 q* h0 x( S - moto(int(Speed-output),int(Speed+output));
1 G: u, b; x& ]) D" q, Z - previous_error = Err;* P3 ^& V( S2 d" C
- }
! [. U& E( d9 J8 B# w! r: B' l( V* I3 G: B - }while(!((lightsensor_2.read()) < (10)));
7 z5 l _( i7 j) j$ v - moto(0,0);
2 C7 ?0 h- A; {9 j4 f) Q - delay(500);
- v! }) s: E( A1 X7 S. T; Q0 t - buzzer.tone(262, 500);
: V8 c4 z( N9 m! u# K - }
7 ]/ i/ B' F1 S/ r% Q: p9 ~ - 2 S0 p" c z6 W+ Y- n- i
- int getErr()
. z) E5 n) F8 `- E% ? - {
: T/ k) i1 C) \4 [ - sData = linefollower.getValue();0 M1 }1 o2 s! D5 u9 A/ w. [8 r
- D1 = ~(sData>>0)&1;( S* @" o" t/ E- w$ ^
- D2 = ~(sData>>1)&1;
( W3 D3 Y8 n3 d; b7 X1 Q5 D - D3 = ~(sData>>2)&1;
( K8 N( d' v3 r# d2 _7 U$ W - D4 = ~(sData>>3)&1;
8 M |1 y+ e% ?8 r3 I - D5 = ~(sData>>4)&1;4 y0 b h0 x& n5 F6 a% t/ H
- D6 = ~(sData>>5)&1;
2 X! M" F8 f9 e& K9 ], a% m( V# z - int downD = D1+D2+D3+D4+D5+D6;
1 r3 h) w* Q- K+ W# ]& t+ q& y - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);" E% x3 v: g# k: y
- if(downD == 0)
$ [ g$ q; ]; N8 r - {
$ ~# J2 ?0 h( c6 e. \ - return 99;
/ w9 j+ ]$ {# ]6 |% e a5 ] N% | - }1 r- b6 b! d/ \! v: T8 e k) O$ N3 R
- else$ _% w. K2 @: W' t6 m c
- {
, L( v* u5 k: | u: U: `; { - return upD/downD;
& s+ H2 e: z, j - }- x% i0 x4 i1 o# X3 M
- }( z2 K; I1 p% w
- 8 ~$ q! T# q2 q
- void moto(int Speed_L,int Speed_R)6 W. Z- R2 t4 @3 U y
- {
, e& x( C: U- u+ Q - Encoder_2.setMotorPwm(Speed_L);
) p6 `4 P' b, I# k* x* G3 m - Encoder_1.setMotorPwm(-Speed_R);2 u. T4 ] x( }
- }
複製代碼 # |" R. `7 p2 x2 p- b9 \# E
MeLineFollowerArray.cpp
4 O$ J3 I; `9 c& o8 [6 H- _ e- #include "MeLineFollowerArray.h"
S5 E$ J3 s, C% ?! y3 g) S
0 y6 S) k2 n) `& X- #ifdef ME_PORT_DEFINED7 ~: j2 i" J7 N5 B
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
1 R, B) d& O* j: k0 \9 Y - {1 N' J- o1 w# B C# I. @
: J) ]/ a& k+ D! d5 z- }
1 q$ W3 G6 j# H; b( F6 L; A+ \0 Q - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
j6 N5 A% H, V3 O! z# R' ?0 h5 u5 g - {
[! q7 e& H+ x - _DataPin = mePort[port].s2;
/ U0 r5 C* P/ f6 V) T l - pinMode(_DataPin, OUTPUT);
2 N4 @; R7 f/ ?& a/ V2 l y0 \ - digitalWrite(_DataPin, HIGH);
$ _: S* u( K* u# r2 f# U3 V/ R - }% v c/ s9 G3 w
- #else // ME_PORT_DEFINED% T/ g. ^, O; _% K. R5 y
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
( g, b. J' B7 Q - {
& A& Q. m- ?3 H7 r6 i) v - _DataPin = pin;
& X" \' a, g! Z/ R1 Z. ^: `. Q - pinMode(_DataPin, OUTPUT);
! X" O' e+ G; G/ Q& |; Y* } - digitalWrite(_DataPin, HIGH);$ t, }: s p. F* L
- }- n7 R/ P" Q# J2 M3 t7 ?0 C0 |
- #endif // ME_PORT_DEFINED0 h; d( N* P# A/ X3 H
- # `4 e/ J P& v% v( B
- ; }8 j C, ~- Z9 t: ~- K
- void MeLineFollowerArray::setpin(uint8_t pin)
. Y% s/ L9 ^" L5 C% s8 G - {
$ a0 h6 c9 F# j5 {3 y% I - _DataPin = pin;0 C( f; ?, A# q7 W. i( _
- pinMode(_DataPin, OUTPUT); 0 D7 e5 ~/ J# O' N- n
- digitalWrite(_DataPin, HIGH);
+ Q8 b# y( ?& a3 K2 h0 N+ N+ s9 f
- P8 `) i1 Q& g. A+ V* w2 D- #ifdef ME_PORT_DEFINED6 Q3 s _6 ~3 n D2 n
- s2 = pin;0 f3 {( B2 I9 m/ S. S0 q( Q3 ]
- #endif6 L, Q0 d3 u! A
- }: F! u( a! a: b9 b
7 a& Y: ^8 w5 r$ h% ]2 g1 G- uint8_t MeLineFollowerArray::getValue()% P5 m. p! A& N: s9 Z
- {
& `: F' H7 }# x - uint32_t LOW_level_read_time;+ ~; X1 C ~0 Q; D
- uint32_t HIGH_level_read_time;
: S Z. r/ z4 u+ r- n- q - uint32_t time_out_flag;! r3 s2 _% V V" Q) e
- uint8_t Sensor_Data[3];
! }, Z$ w* x& ~% u$ Q - static uint8_t old_data = 0xff;/ j! d& r0 G9 O3 c& F
1 y. r9 {# l0 a1 W& G- pinMode(_DataPin, OUTPUT);
, e- R( I7 `6 L, W" @1 ~ L - digitalWrite(_DataPin, LOW);8 v6 S% @* @+ a" P1 M
- delayMicroseconds(980);
4 w; E8 v4 |0 V+ g: {! X8 z - digitalWrite(_DataPin, HIGH);
! @' X/ x" F. s) H - 4 M8 ?5 u- k9 E$ a$ c5 [+ L
- pinMode(_DataPin, INPUT_PULLUP);
( M" J2 _* b# t8 K! h/ H - delayMicroseconds(10);
0 w5 }* _4 u* ^ - 3 C8 v4 j V: v8 e( P
- time_out_flag = millis();5 ]6 _( n- c# s; A! Q9 B9 [
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );/ e$ C4 B3 i; P6 E9 m" u0 X
. }8 n8 [) q1 X$ M6 C0 |- LOW_level_read_time = micros();
! a3 g I7 r/ f - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
/ `) e4 [: r4 f; u6 Q8 [& j' t - {
# m& d$ r3 ? u+ p7 e7 W* q - return 0xff;
# Z! H5 N0 @7 ], ^8 J - }
3 m! e) W$ ~; t8 b0 c- L - 6 R7 M2 o0 p' G3 C! f' _* }
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
" y/ N. J5 w- }& W; s
7 `5 t1 P4 ^+ E3 r, a% _- HIGH_level_read_time = micros();9 ^* |0 C& D+ m! t. B9 f
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level7 z" E7 ~2 ]" z
- * ^9 K5 }5 Z! r1 d
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ P" k9 X9 m# I3 t+ n& H - {: Q1 }8 Q9 E) {" U! O$ n I
- return 0xff;- j: v; g: `- K' g
- }8 u' }, z% z6 n
- : i& v/ T& L+ x. |3 j4 e/ c; @9 M+ f7 B
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110)) l' w$ w2 T# O/ T5 z* A; b' I+ s3 i
- {/ Y% P; y3 |3 }# f
- return 0xff;3 r% `# h; S2 o% o) l+ ], `* \
- }6 ~( }) j& b: a/ B
- ( c4 ]& g6 u2 b t
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
, V8 [# P1 {5 I1 C1 T - LOW_level_read_time = micros();5 ]) V l- `- b6 @+ D& g- B0 I
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level4 n5 Q2 c' L: V- B; C) A. m
" f: }/ D0 L; D, U6 H' T- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out" s2 F/ q( W: F d) p4 @& I& b
- {
8 Y% V- v0 n9 h2 \; k - return 0xff;- K8 ] s% n+ ^/ e- R8 _ F
- }
9 X; Z9 L1 h' D' z* s- x
0 O! r" q, O0 D# A% M- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
+ N8 Q8 T4 L7 N9 F" t# ~. ~ - {
& x; c) Z7 a8 M* w5 A& w3 W - return 0xff;4 }8 a4 @% _. R7 w
- }
" D' U$ [+ @# J4 i. P
5 y( ?8 \6 o% @! A! ^7 Q- for(uint8_t k=0; k<3; k++)
/ a! ?8 Y. p5 y. W1 Y* ]& |5 p - {) G# o. r& V" C9 t, t# A, M8 U/ {" O
- Sensor_Data[k] = 0x00;
( c7 F, @7 t* I$ U# ?
7 V7 y1 S' t7 N' Y# h- for(uint8_t i=0;i<8;i++) [3 v9 R" x# x: T/ }- g; P
- {
* J( v9 X/ ]/ ?1 e - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level" K% f' K7 c9 e8 |9 I. m8 B1 k& ?& |
- HIGH_level_read_time = micros();( ^) w; [# x; P# [5 p
- LOW_level_read_time = micros() - LOW_level_read_time;% R$ Z" H2 }- G
- 0 x8 K) |* K/ ?
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )5 u- ]* ~) t1 E! u( ^( U
- {4 |: }* b9 _$ [* t# p! B) Q2 y& P
- return 0xff;
- M: P. v5 \% H4 `: ]; t - }" H! N/ y6 K$ v& {2 O7 [, e5 R1 a
: Z; W4 V3 V. b6 t3 G) ^- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
3 B3 R8 {: f$ H$ x8 W* `# S0 W% f - LOW_level_read_time = micros();
) `% q( U! _7 I - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level$ R. q; r6 t1 |3 q# P8 A% J
% X1 [/ F2 J8 g& }% L ^- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
+ L2 [+ A2 r! [9 H, s- x) \! j - {3 p" I) [+ Z9 w: m
- Sensor_Data[k] |= (0x80 >> i);
9 O9 |- d0 R4 n# c: P" P/ Y3 P - }3 Y. J% Y6 S5 ?: \
- else if(HIGH_level_read_time >= 100)- D( e. ~$ D2 c- r1 k. ^
- {) I0 O5 {* {6 E _
- return 0xff;- r, u* G5 [) S
- }
* @4 j6 h/ a0 `1 ]6 m, X2 L
3 _) E+ {- i5 C- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
# D" j4 k1 E$ T8 g1 p0 t0 o6 o/ | - {
: P4 N( `! D. j" d( ~( j - return 0xff;+ I& T; x6 P1 W3 r ]& ^' Q
- }
: q e; Y- |" n% W6 m: A: E - }+ L8 Z, @% J6 A/ N5 n* S9 z
- }8 l( U# u5 x6 J* g9 E* |$ A
- 9 X. h# C2 I0 e3 X) W, f- }
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level. P; P( \6 V, v* ]4 k: R
- HIGH_level_read_time = micros();6 h: l. I- r# N) A8 }0 D/ w
- LOW_level_read_time = micros() - LOW_level_read_time;
6 N6 k& p$ K' N" _ ^
3 Q4 r$ Y8 \' t, C# j- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ); s% W6 H! O, @: j
- {
* ]2 l7 U0 o0 r+ \( ?6 ` - return 0xff;/ w1 H) B( A/ p% j
- }
+ {8 b& B7 [& x* W; w3 F
9 n. @. ?+ p" s/ M7 R- pinMode(_DataPin, OUTPUT);
$ ~) @; f$ w) Y" D" g - digitalWrite(_DataPin, HIGH);
5 D9 K3 d6 H W: E0 N0 \ p8 g
: ?3 i F6 e* F- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
4 B/ K5 p. E: p! h5 S - {
, R: S4 P! _6 h& m/ ? - old_data = Sensor_Data[0];
3 y' v% @ G8 O7 M, E# L - return Sensor_Data[0];: W3 B/ {/ C: k, y: d! F
- }' x l; E, u* Y" a; [
- else& ]. n' u4 f5 V0 Z4 P" m6 R
- {
8 @! N& w6 o% q# R& | - return old_data;
, Z. c! E) J7 \9 y( X9 I0 s1 s - }2 b6 [! j; G$ `7 L2 l
- }
- A* |2 c- ?* |! d
複製代碼
3 |# H% ~: {& ^% HMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
: Z7 B* W n0 x- c: {3 @2 }. s' ]. R6 f
|
|