|
|
1 Q* p+ O) B) m/ ~$ u% U f4 n
- #include <Arduino.h>
1 q A0 K0 x# H; A4 X5 y# F - #include <MeAuriga.h>
, G2 K) v0 v8 V3 V/ X' w - #include "MeLineFollowerArray.h"* G3 L: Q0 {8 {: M9 j
- ) e/ S0 @4 v9 @4 N- u- m/ O
- MeEncoderOnBoard Encoder_1(SLOT1);; T; D; `1 Z* T4 T; L5 w
- MeEncoderOnBoard Encoder_2(SLOT2);
* k: r7 p) N {( `% Z" F - MeLightSensor lightsensor_1(12);
$ ]: a/ i" \, o - MeLightSensor lightsensor_2(11);0 O8 |, C" [# X( _
- MeBuzzer buzzer;
4 d6 y/ K8 _3 E; p - MeLineFollowerArray linefollower(PORT_6);' i1 X v# v3 e0 ?, n" X/ e6 [
- + g) _. @6 `! t5 | \7 T' Z
- #define Error1 1
. V' H6 {- F r, _; ?5 O - #define Error2 23 q4 r1 M& w. P; Y& d; j; t
- #define Error3 3
$ c j) v8 V$ N- l( c3 w& z* s0 ~
. ~8 \8 n! O# \9 x6 ?: Y- #define Kp 15
. o) l0 F& M" y& I/ [% A3 [ - #define Ki 0.15' V# N: o& X8 _' L0 k
- #define Kd 0.033 X- P [& p" s& W4 U% @' n, }
- ) k( L8 Q& ]7 I: n0 L' W6 a3 k3 c
- uint8_t sData;
6 K. U. ~7 w: B! f - uint8_t D1;/ k& }9 M; ^- n8 _5 V( X+ j
- uint8_t D2;
! b! z3 A: s: u% n9 q0 F. u - uint8_t D3;
9 q9 Y6 ?6 t/ C; V - uint8_t D4;8 p& y& X- c7 h6 j
- uint8_t D5;
, w* t* z, t D, }+ x7 H - uint8_t D6;8 }/ b% D- u& P% }
- * {. m7 x# X# h3 f/ v5 l
- float previous_error = 0;
+ a5 C: \7 w( d t3 w9 w! \4 m6 g - float integral = 0;0 A4 Y7 c1 X1 n% N& \9 F
- float derivative = 0;
1 u; j* S9 `5 Y5 z - int Speed = 160;4 v# S1 X' d, w+ S( [* r3 a2 `
- float output;
1 J% w0 I$ j& }5 N$ \
5 n8 e2 F/ s5 S" q, X: I" Z- byte Left;
9 E8 F/ H& `+ m, T$ j K
4 }: \4 b9 Y% S; m+ Q* A- void setup() 0 Y7 ~7 v/ k8 y1 w. p# I) T
- {
; Q$ _5 q/ d/ S - //Set PWM 8KHz, P+ H& l* m& }( U: X. C- W# \
- TCCR1A = _BV(WGM10);
% Y5 P R) h) a - TCCR1B = _BV(CS11) | _BV(WGM12);+ A1 q6 J2 K5 p4 r f. l
- TCCR2A = _BV(WGM21) | _BV(WGM20); }1 s' a2 V+ {5 j& ]
- TCCR2B = _BV(CS21);6 K! K5 F9 x8 K7 l2 }! V: m! n
- Serial.begin(9600);% t6 i) s" M7 [+ T5 y
- buzzer.setpin(45);6 I7 B8 h9 ^7 I* w8 S
- }! a, D6 k# W9 {2 e$ E
8 c; Q$ @1 F4 @! @3 ^; G- void loop()$ Z: M) e4 [, C1 d. K5 n- j
- {
$ d' Y) X( j" {" `: F) u& o- f - while(!((lightsensor_1.read()) < (10)));
8 h K& D' F0 C) l3 z1 w - buzzer.tone(1047, 500);
' W. X3 h/ O' }) ~# h7 [ - delay(500);
1 q) y* t: O1 k0 y7 x: n7 n& r - do
* `9 ^0 C! V4 o8 q1 e# V- f& K( [ - {
3 @6 N$ {5 F. x& m5 D - int Err = getErr();
4 J7 z1 K; B2 ?: a! l) j - if(D1 == 1)
K4 O2 E7 i/ Y - {
6 f& c! @' i1 E# A8 g& O - Left = 1;
2 i: U: g& G7 H% a# A - }3 {+ d0 Z5 N; N# C2 g* E
- if(D6 == 1)
4 @6 [* W p6 o$ [2 f$ o - {
, x" i8 ~" q6 N9 t - Left = 0;
' @5 D. M {2 m" E0 q, F# U: E - }1 Z4 X3 c" i% }: W
- if(Err == 99)
& F- V0 g5 W/ H& [8 R - {
/ y+ d# H) D9 I$ M7 i { G0 ]3 V9 B - if(Left == 1)
3 b' y) k1 K# E3 O$ e - {
P a4 y: d+ o - Speed -= 5;
! y6 ]- X' \4 H# x' ?: a - moto(0,Speed);) {/ [' o3 c5 M) t4 \8 v
- do
' Y V6 w; [$ I$ T - {
* @. G* |7 b1 f: p, E - Err = getErr();
' y2 ?5 C- ~2 E - }while((D1+D6) == 0);+ L8 p8 o- u2 A3 t6 [1 S6 z
- }) }0 d4 p# v% ~
- else! b* l f* F3 K
- {% w0 R9 Z, F G. b( j+ X
- Speed -= 5;" ~! k6 f: K% l; H6 r
- moto(Speed,0); ( m5 d: i( h5 G9 w: V
- do
( N( E% F) m% t- S a$ _ \ - {. b+ C0 e+ m$ T8 w
- Err = getErr();
- J2 L" y: K: M; B5 I! @. F, {9 u - }while((D1+D6) == 0);% m4 U2 a6 [5 W# i' x, ~; Q+ L" \
- }
7 z3 _) z q! n# W5 M9 A - }+ I m- |0 l' m9 {
- else
0 e% N. P; _( H - { A# G$ p) m" Z9 U8 u: p. ]
- if((Speed < 160) & (Err < 2)) Speed+=1;
% h+ ~/ k- a" z6 z" X [8 U, Z - if((Speed > 100) & (Err > 2)) Speed-=2;
+ i# H- E7 p1 U: }$ i - integral = integral + Err;' X: G$ H/ }1 _, V# \( q! Q. T7 w J
- derivative = Err - previous_error;
0 X% j H k) F$ j - output = Kp*Err + Ki*integral + Kd*derivative;" ?5 m" k# e# \% @7 o/ i1 H
- moto(int(Speed-output),int(Speed+output));
; ~+ u7 f1 g7 P1 ^+ O9 _) }0 U* d - previous_error = Err;
. K* S+ @4 i1 f - }" J2 @' @! r* [) Y* P
- }while(!((lightsensor_2.read()) < (10)));
E* [: V" w. O+ r - moto(0,0);2 R6 \; D# o3 n# ~7 c
- delay(500);
' M- X) C" x" G" A - buzzer.tone(262, 500);
- U' Y' T" j% G. [. u - }/ h, E4 o. d3 h# e0 K
- % H# ?# _! U& `. ?8 u- l- I
- int getErr(); g# ~8 y, z+ W5 P9 I$ l' _
- {
d! J, d- i. s+ {( W$ M7 Z5 B - sData = linefollower.getValue();4 z, m6 ~$ p9 ]0 [" y, V, c0 F1 I
- D1 = ~(sData>>0)&1;
$ {& ?# E5 B% f. z# _: K - D2 = ~(sData>>1)&1;: b9 q1 A7 Y6 I
- D3 = ~(sData>>2)&1;: y6 L( Z U/ X
- D4 = ~(sData>>3)&1;
: G+ j5 w5 P6 l, u" C" ? - D5 = ~(sData>>4)&1;7 N' d5 @- V6 ~
- D6 = ~(sData>>5)&1;: l6 `: i4 u; w& O( f
- int downD = D1+D2+D3+D4+D5+D6;, m. k3 B$ `) @+ }6 Z9 A; m! U
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
( M+ I& h5 H4 w% y: E0 ^$ M - if(downD == 0)! n2 r, ^% M+ R- @
- {+ Z* S" q3 f( C3 F
- return 99;6 {# ]: s+ b; D4 H9 @: Q& F, A
- }
5 {: Z# J" m3 V, U: k% N - else
* \/ ]& n+ v) M6 I7 p4 Q - {
n* H4 c$ j, j6 n# |8 \ - return upD/downD;1 i3 J9 E% J( K9 E. ~; B+ L6 G
- }( z* ^' v$ Z! A7 P$ N
- }
; Q) ^. H0 R0 R5 p2 Q+ ^ - # v0 Q" i8 f4 f4 g" K
- void moto(int Speed_L,int Speed_R)5 j. ` b' J6 s# ]
- {1 m3 s: I. t( N5 Z* ?# s
- Encoder_2.setMotorPwm(Speed_L);
6 e( U+ Y8 ~1 Y1 [6 p! X1 t3 Q - Encoder_1.setMotorPwm(-Speed_R);
, G& y, G3 s! B7 o4 c - }
複製代碼
0 \+ d# y% v, d% o0 l" eMeLineFollowerArray.cpp2 r: `. b! u+ }( E) T
- #include "MeLineFollowerArray.h"
$ j& }6 ?7 X& S" f! O# @7 _$ s9 ? - * i2 i8 R6 @$ I
- #ifdef ME_PORT_DEFINED
. T) e: [0 y! ^7 Y, Y& b. g [ - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0), v9 S4 S, V$ d2 G0 f9 _
- {
7 m* @" A- b5 s6 X' ~/ y* B - ) a8 Z6 [4 z. X" ?/ X" Q0 t1 h1 }# k
- }
5 @4 t4 y$ D+ z' o/ l! k L - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)6 {4 }. _. y2 D0 I0 J' b
- {
- O* Z" V4 h! E- e) d' e - _DataPin = mePort[port].s2;
- f* r0 s8 ^- l1 g0 e - pinMode(_DataPin, OUTPUT);
H) ]3 q) V. e+ y E5 Y - digitalWrite(_DataPin, HIGH);2 x+ J% n5 L* ^3 `
- }, Z% g, O- T/ N+ Y: z; V3 i
- #else // ME_PORT_DEFINED
: h7 g9 m; q l1 g, ~. s% i& l - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)# ]# N& O! y' }( ]) h4 e
- {& v: b0 r, M. H. M: G- O
- _DataPin = pin;
8 d3 L6 [% N5 ?4 k0 c - pinMode(_DataPin, OUTPUT); 0 e D: O' m" ]% f
- digitalWrite(_DataPin, HIGH);% E U) e& {2 [+ D2 r) v [ ^' n' ~- B# k
- }
7 j# N0 a/ Q* U' x7 q& E* ~ - #endif // ME_PORT_DEFINED
* o) H/ j) [; g I1 g3 X; N - 4 ~0 K7 w; L7 F. R7 D c: x/ ^) e( I
* H: L5 H" z0 U9 w$ s9 K- void MeLineFollowerArray::setpin(uint8_t pin)
8 K. n p. p3 j' [! I# \ - {- Z! f7 X& B# Q/ U6 e% P
- _DataPin = pin;
6 \0 a7 H4 F3 w9 {, \" m - pinMode(_DataPin, OUTPUT);
/ a! b+ w7 o3 [5 U - digitalWrite(_DataPin, HIGH);5 Z/ Z- p$ X2 S, `6 S( \" j; H- c
- - ?$ I, w9 G' b0 w# O. y$ Z6 J
- #ifdef ME_PORT_DEFINED& P E; W# D$ F% i* K
- s2 = pin;
9 a! W2 i' x: t9 n - #endif0 K1 j6 Y' l1 k3 ~1 m: V" ^
- }
5 y9 C+ k1 n& f
@" |; m9 H! y/ G$ t: q) g v- uint8_t MeLineFollowerArray::getValue(); K3 ?" x9 [; x1 f! H9 L
- {
( M5 Y; d, C2 E* L - uint32_t LOW_level_read_time;
1 B. R; |! u7 k0 L( v$ I# r - uint32_t HIGH_level_read_time;; d/ F6 X ~" |) u/ \
- uint32_t time_out_flag;
3 M2 T% H* ?6 {% d# `6 s - uint8_t Sensor_Data[3];
% Y9 W Y7 Z# D8 q7 L - static uint8_t old_data = 0xff;* w. W; c4 x! Z7 W1 i! b
6 u* f# S3 m2 ?; C3 H- pinMode(_DataPin, OUTPUT);, ?# r& E6 }* c2 R
- digitalWrite(_DataPin, LOW);0 W A' ~- H# a5 K
- delayMicroseconds(980);
- ?) z: c, B) Y5 Z1 g' L: \, g - digitalWrite(_DataPin, HIGH);
# X2 d4 r0 `. }/ e! s
6 s; G [' s7 B" c! @- pinMode(_DataPin, INPUT_PULLUP);
! t+ J% w9 n9 c: ^ - delayMicroseconds(10);
$ M& J4 t8 h6 [5 g" H8 b8 X7 V
/ W9 Y; H2 k% o! ^# i- time_out_flag = millis();8 z* [' D W' o: |( |# U# U
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
2 [4 {' w8 q( ~9 \/ H - ! G2 ^3 F; A$ B2 b; s
- LOW_level_read_time = micros();
- M' @% h' a ?8 r7 v8 X% [; | - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out2 V; O3 z4 W# B1 Q) ]" B
- {2 Y% w; n8 r: z+ V3 u& h
- return 0xff;0 t6 P- r; F8 r1 s& ^
- }) F; B6 `7 m" y2 w3 ^/ j
- : z1 B" V- y$ Z- s
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( c# k8 l b# m+ D
- 9 `) |& k: l! v" v
- HIGH_level_read_time = micros();
* f% x! {! v& `( z- r/ K - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
) M0 q c U; a& W" C* F* _, A - , G7 }: O% P$ B
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
9 A4 I3 i! H% D1 V% y6 N - {
0 Q% `# A5 @& c' B1 n+ c! t" u( [ - return 0xff;8 ~; C7 |$ S+ Y( r
- }
3 B4 u5 ]& K! Y/ u; r- f
1 Y* n$ Q3 H0 q, @ r1 E- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))% `$ G: N& ~" D, ~, m+ ?" w9 r+ N
- {4 r, R2 u( f9 k$ b H0 D
- return 0xff;
+ A: b$ c5 H( r - }5 M7 F' N7 v% s0 J
- d# M* k) n" m4 N/ S. u6 x( W+ F- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );) `5 q2 G' p+ H% K6 d6 w* {! u
- LOW_level_read_time = micros();
6 h ]' d+ e L7 o0 P5 U- I5 n - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level! l3 _; f& w/ U0 [: v, t' |4 a
- / c3 c8 \3 s2 n, E2 w+ Y
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
" C! g' s3 S( G1 Q3 ` - {
3 c2 }5 ]# I& W w - return 0xff;
% L0 h* o0 n4 @/ H6 I( x1 \- m - }
% K+ I/ `0 |) {) M
1 u& E2 g- q, B6 i$ u2 p% |8 U9 a- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
+ {) J9 ]9 N9 T$ R+ m" k - {+ @% C, G8 u( F F* |5 @
- return 0xff;
( Z- o6 u8 I. h8 D. e8 q- V - }
2 m& [4 o3 r5 b8 f
0 A, g& F5 s3 N% Y+ B- D& w; v- for(uint8_t k=0; k<3; k++)& Q6 `" N9 S, v
- {
; _- b/ h7 \% o+ Q) u" w3 Q! w7 } - Sensor_Data[k] = 0x00;
7 t5 F8 U$ c& O% c- k1 M" |
$ Y6 p3 \0 J1 s4 [- for(uint8_t i=0;i<8;i++)
q5 w7 i* }! X: l/ ? - {
9 D2 Q: Y, z& [ - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
0 [ `/ o$ e% `/ V: _ - HIGH_level_read_time = micros();
8 ~/ c5 h W e; z, _5 } - LOW_level_read_time = micros() - LOW_level_read_time;; s2 V" V7 k0 d; T) r2 i8 Q
- & S5 z5 G. C; D* m
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ): ~3 D8 ?9 Y3 e3 E6 C$ E
- {5 M% v' h( z) o/ r9 p
- return 0xff;6 }* B$ w9 D' K
- }$ d$ V" i9 d; W8 o* k
- 0 }# {3 w+ ]% n7 p
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
8 o# j+ V1 v" X/ n - LOW_level_read_time = micros();
0 W0 ?8 r% E. X9 H- L$ U - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
; n+ d2 ?/ q i, ~$ V
5 i/ b' i# v; p- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1$ y7 R; J$ u) |, D: f9 w u. {! F0 U
- {( a( h4 [; ^9 r; K& z: A3 j
- Sensor_Data[k] |= (0x80 >> i);% D! q# K5 b" i9 @
- }
* f. S4 e2 i& D7 G( F' K - else if(HIGH_level_read_time >= 100)
& X4 F" v: C) }8 C! F4 [/ f R. L8 @ - {
& O" m: a) p7 q" }5 L2 O) E- S5 V - return 0xff;
5 M1 L# ]1 ~, Q- U; J8 `7 z - }
. D. Q6 L1 e. r! } - 1 y7 X4 ~0 C2 I
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
5 ?$ o5 {; u3 S7 o2 s - {
& I4 S! F0 `* R - return 0xff;
) c, v o6 J# @0 K( n5 M2 _ - }
' E" t. h8 n5 j6 O! p5 Y' b* U - }
7 \- o2 l( ^4 f& R - }
, q$ Q7 S0 m& k( X: L, c* l - $ g2 ?1 B( T0 O) }8 p( Q
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
* e8 G/ g( f' W: O9 `$ D0 I4 Z: C# d - HIGH_level_read_time = micros();0 _6 q5 j: }! c) R8 t& j
- LOW_level_read_time = micros() - LOW_level_read_time;" w( ]- e/ j" {/ C1 Z
- ! l) q# u- @) U9 l7 g
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
2 |* O7 {+ _# p - {
4 i" T% x& N; v4 r9 A: }; h - return 0xff;7 ]# ?% {4 A; V5 u$ Y/ b$ U8 I$ w& \
- } z$ p+ ?8 R4 `( r: \5 S3 O
- , a U) G. b& h' o& |! P5 Z
- pinMode(_DataPin, OUTPUT);
1 _7 L8 p: E2 C) {: o6 a5 ?. D' { - digitalWrite(_DataPin, HIGH);% |5 Y# I2 v& f8 V V- @$ B
- % y+ _" G1 y; f$ C
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))9 k3 [' P. r( f; L: e+ l$ C2 |, M
- {
; q: @: r/ x; ]$ {) p - old_data = Sensor_Data[0];
+ k: a8 I9 y9 [, K- y! [0 u4 z' p, t - return Sensor_Data[0];
: _( U: }& y" p% ^& W* C3 O - }
# s E4 M7 h- ]7 I9 [ - else
3 k) ^7 Z( l: o, F0 C3 A; N6 Q - {+ ~: ~* u) x/ L6 @' b& a- p' a7 S
- return old_data;/ H; h5 J4 p( o
- }3 G O; B8 o" W0 E! K9 \
- }
' ] ^# g' R. n9 w; |0 z
複製代碼 + ~# t& e7 m: `( C1 w1 j
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
2 l& A: Y) n$ p: Z! |* V
0 F/ I' b; Q( O |
|