|
|
" d, { M( _: G) _5 W+ D
- #include <Arduino.h>
: c. ~1 g. E. ^* I* h4 v8 v - #include <MeAuriga.h>1 {! u$ M" \( ^7 P6 M/ n
- #include "MeLineFollowerArray.h"
1 M# F+ X$ z, M
3 V9 D, J8 z8 H3 d- MeEncoderOnBoard Encoder_1(SLOT1);$ A/ l& u; B) m
- MeEncoderOnBoard Encoder_2(SLOT2);/ V+ e, D5 I$ P& Y0 d A) O
- MeLightSensor lightsensor_1(12);
1 }) @8 q; c/ C0 } - MeLightSensor lightsensor_2(11);
# C- ~ g: g, Y: U$ J" _- ^ - MeBuzzer buzzer;: c) N# V' l& n8 q! I
- MeLineFollowerArray linefollower(PORT_6);4 N5 o0 q( c+ V! K8 o
- ) r- ^8 r" c4 y: U9 M
- #define Error1 1: `6 J) K& K- i$ v1 m w
- #define Error2 2
" J% u# \- ]+ |+ E' _; ] - #define Error3 36 e# I2 v8 Q: c r& K6 B
" S* `( c4 Q: f4 I9 h1 R- y- #define Kp 15
/ C" N- x# B* |, T q. ? - #define Ki 0.150 C+ l" t2 T9 [0 _+ J4 \, d( D
- #define Kd 0.03
, v1 W! _ V- l9 @8 y; `
& M2 P/ x3 {$ G/ m* K- uint8_t sData;
: c' w. M. D4 x3 w, v - uint8_t D1;
3 {8 f1 u Q( f - uint8_t D2;
: _1 @7 `; u4 Z- p& K$ t3 U# i - uint8_t D3;* c) G- L" _ [) k N c" P/ B
- uint8_t D4;
3 U6 \4 y: O4 N4 ?, ? - uint8_t D5;
% Q/ p9 E: C3 C+ Z) N+ { - uint8_t D6;
6 i- j! {7 i/ C: L7 W3 [ - 7 C+ X# X# k- N. p& ^
- float previous_error = 0;
$ l$ R3 G4 O( s# R - float integral = 0;2 f2 C Q/ p) B& Y( u0 S& w) R
- float derivative = 0;5 e% x- {# A0 h: ]3 q1 C2 x, b
- int Speed = 160;
* A5 l3 Y* x0 V4 ?$ W# C. k0 ?+ z - float output;, s/ L3 g& P* a/ X( I; g
- ) D0 i4 U) e* K5 v% o
- byte Left;
, |3 b% M" o+ l0 i8 e6 y
3 K. ?6 J( K, v- void setup() " g3 s$ _" X. S& G
- {
7 n) U7 Q- y/ T# p/ w0 f - //Set PWM 8KHz
+ I# j$ e4 I( f# ~: { - TCCR1A = _BV(WGM10);
( ` V6 \3 H0 E' A- J5 \* p* J- ^ - TCCR1B = _BV(CS11) | _BV(WGM12);
% I) x8 H6 M$ g. M; k$ O( a - TCCR2A = _BV(WGM21) | _BV(WGM20);7 \* _6 P6 Q) Q! D% b- r' Z
- TCCR2B = _BV(CS21);7 E% C V s' i( ?; ?& [
- Serial.begin(9600);# U& H t5 z5 E. e2 x
- buzzer.setpin(45);! D3 Y. \+ v3 Y- |/ V, Y1 D
- }
$ G0 ?9 z% Q5 l4 r0 e7 x" {3 n
6 C4 ?3 o6 b0 `) Z& k- void loop(): N, m5 Y* [% @. Y5 Z
- {( @, A! [5 x. K* }' \' f
- while(!((lightsensor_1.read()) < (10)));1 Q% u9 Z. b0 c- A" [
- buzzer.tone(1047, 500);
+ `7 {/ g+ p3 X/ a6 i7 K: D0 y - delay(500);) G, ~5 y" [/ U& q
- do4 V! u6 c O4 X% P" k8 C, _
- {- f' P1 a5 `$ P* R
- int Err = getErr();
% k; E$ q" H% i8 x9 {2 k; }' N - if(D1 == 1)
. D2 \! C, P$ Y' W+ ` - {
: J$ o+ Y, d; F: c - Left = 1;
# \7 r! R0 d$ H( }" O/ d' L0 } - }
+ D5 Q5 |& ~4 m, b) e2 u- q - if(D6 == 1)
+ l( U( X9 V6 r5 Q! | - {0 `. m+ P9 ^# ?; J* W0 Q5 b r4 n
- Left = 0;# l2 V) \" S E2 h
- }4 w- B- {8 I$ k# `# V: b% w8 V# z
- if(Err == 99)
) i5 l2 H: H+ y% h0 x$ P. @ - {. V) h, ?, z. B& \$ T
- if(Left == 1)
P* q: j( p; r( x - {4 }3 L) ~. u9 w) L1 X" J0 y
- Speed -= 5;+ q8 h7 X0 P. j7 q! N' v1 @
- moto(0,Speed);
6 D; F) I3 d. ?+ L3 A - do$ L8 d" b% N/ F
- {: e. D" A' i+ Q
- Err = getErr();7 n' n; v! a2 [; i) }
- }while((D1+D6) == 0);
/ H' q: ]; U4 k% n9 \; G( B+ ? - }* R+ o: H& R) e! |1 J( F" Y
- else; ^; r B' L9 E) f; q! B( t# Z
- {
# Y4 h6 Q' E- R" s8 v; s' Y) S) l% K3 {$ F - Speed -= 5;
- s3 }* J6 w; O0 g' h+ ^. _2 z* H' i) e - moto(Speed,0); 3 X4 |9 W* g2 m x; K7 y3 N, I
- do. Q8 e# m9 D* s% `
- {6 D/ n- z0 T0 h- Y; E
- Err = getErr();
. N8 s4 v0 z: X: ` - }while((D1+D6) == 0);: D$ y4 C0 j+ u( o5 B% n5 q+ r
- }
; I* d' Q# K+ J+ W" |+ G) s - }
7 B6 n- w3 X1 v* Q - else
5 Y2 D; J, V4 U - {
0 G3 g* U3 Q- a - if((Speed < 160) & (Err < 2)) Speed+=1; : s$ n! E8 ~* _6 C
- if((Speed > 100) & (Err > 2)) Speed-=2;
- g1 B2 L& H/ f1 F$ A: w - integral = integral + Err;
% K- R+ }/ _% n2 o/ ]7 r% O- ~ - derivative = Err - previous_error;! L/ H/ ]2 |. N
- output = Kp*Err + Ki*integral + Kd*derivative;9 g& r% _8 M0 @3 {: @$ p; ~
- moto(int(Speed-output),int(Speed+output));7 A7 x$ Z, p' G
- previous_error = Err;
+ X; c) n/ u' [. ?" ]/ P" ? - }& y$ ] t9 N1 Z9 }
- }while(!((lightsensor_2.read()) < (10)));; W% P" R) ]$ {9 s+ d: c# G
- moto(0,0);
4 l D. U" [$ t% u, \" P7 L9 l - delay(500);
8 Y+ Y ?. S5 G* X. K v, ]! R - buzzer.tone(262, 500);
% S2 s# u) L/ T! U% q& b5 Z5 P- X - }; U2 H w# q! E5 }4 c+ S
- 9 b6 Y( |- S5 |% Z. Z0 W
- int getErr()
* \6 n$ z: W% Q - {
; \) Z8 y$ L& P4 d$ B. t1 w- H - sData = linefollower.getValue();0 {: ~: I+ V! F; ]8 t* U
- D1 = ~(sData>>0)&1;, h- m6 R6 n+ [5 r3 W8 t2 S
- D2 = ~(sData>>1)&1;
% _) d8 l5 C" ]) v: `6 Z - D3 = ~(sData>>2)&1;4 N1 B4 r, p$ }5 G/ m
- D4 = ~(sData>>3)&1;
- @6 h9 u6 i0 h4 }- _ - D5 = ~(sData>>4)&1;* ?9 |5 J* L: e. b( p$ E! V& V
- D6 = ~(sData>>5)&1;
7 r- h' x% \, i0 a4 R - int downD = D1+D2+D3+D4+D5+D6; g( }+ [' S% e% x
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);9 v- X9 F4 x: B2 F4 Z
- if(downD == 0)
6 q3 s' D6 j3 h& Y - {( f& Q3 k9 Y4 \: A. B
- return 99;
' G2 n- t0 r# r3 l* r' i, U - }
4 Y9 @ j- Q* u0 b( k - else' [& O! B' t2 c' A. @0 F9 [% S
- {7 e8 q$ F1 q0 s0 R) E
- return upD/downD;
) Y6 h: D; z1 s* H2 X7 T& A - }
2 y% k1 G/ D. j: Z1 C9 m4 z# I2 o - }8 l1 K7 E% d# L r. A# N8 L
- " W l9 a, |/ @' W# b; _$ ], {
- void moto(int Speed_L,int Speed_R)5 m* `4 `8 W* S- f9 P; V0 w
- {
$ h" j5 X/ P1 }3 Y1 v2 d7 S) e - Encoder_2.setMotorPwm(Speed_L);
8 R7 X- X# J: q- `- J3 y. K5 Q* f - Encoder_1.setMotorPwm(-Speed_R);
3 C, `9 b% F2 t( o/ Y: P - }
複製代碼 % i( V6 Q# S! P9 c* k' G9 D& t
MeLineFollowerArray.cpp
8 W- H: f1 v: k) v; z- #include "MeLineFollowerArray.h"/ q$ U6 n3 e3 Z/ C5 E" e! m, E) Z& g
! d& {- N7 B- J2 E& w$ H- #ifdef ME_PORT_DEFINED
8 i. ?" K. B' z% X8 V1 p3 `" C - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)4 k$ S+ z5 u+ ~. g9 V- z% f
- {% S7 }, E" J" q2 L: x
# o0 a1 _, X3 }2 R0 s* S- }
: I# W# s$ ^4 {* f7 l' a' z - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)) ?8 B/ W d; ^0 F, Z1 M2 m0 k
- {- I$ e$ f8 d8 [& r* j0 C/ O
- _DataPin = mePort[port].s2;- a3 z) _6 F: z% O W2 ^( J) k
- pinMode(_DataPin, OUTPUT); 1 H j( l" e9 w; ]0 e q5 \
- digitalWrite(_DataPin, HIGH);
, j( G/ v# u$ } - }
/ H7 Z) m0 @4 W" }( h - #else // ME_PORT_DEFINED
5 q/ p" L, O2 h1 C9 ^) n/ A - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin): D$ @( E* V0 ?! v/ N* h; L d
- {
8 g9 O( F2 G$ W - _DataPin = pin;8 N5 O8 ~& g+ d( G" l4 P2 d( O
- pinMode(_DataPin, OUTPUT);
2 N+ O7 Q9 j2 A* H: L - digitalWrite(_DataPin, HIGH);6 p/ h5 |8 v1 e4 I$ Q
- }
; r$ z4 N+ Z; e4 X - #endif // ME_PORT_DEFINED
- P, L/ X6 O! Y) n! U$ ^
3 D0 F: k1 z, a: E4 \3 w5 {- % G- [6 j$ n9 k; F0 ^9 I
- void MeLineFollowerArray::setpin(uint8_t pin)
' F0 _% E# g% r, A+ g5 p - {
$ b8 B7 W* e' B0 e1 |2 G# B - _DataPin = pin;# m, U0 j% `* B: p4 U2 _& D# \
- pinMode(_DataPin, OUTPUT); % G* [! l; P* m. I
- digitalWrite(_DataPin, HIGH);! u- l5 K1 w% G3 _% O
, t3 H' N) @% B5 L- #ifdef ME_PORT_DEFINED
8 l7 v- j9 z" l B - s2 = pin;) {( G4 X* n0 E7 o
- #endif2 M8 B3 t. Z# m7 i# z
- }
% U1 a9 V0 h, f$ e: n( l/ m' }+ F( ?
9 m% {$ C* O0 \' u- uint8_t MeLineFollowerArray::getValue()
1 ?& W. b, j; T) e8 A - {
9 \6 p d! B( e6 t7 x: E- ?) m8 r - uint32_t LOW_level_read_time;
' h) Z0 E: s/ F7 J - uint32_t HIGH_level_read_time;) o# r n8 E8 |8 |# Q
- uint32_t time_out_flag;
. q5 }$ o1 \* j8 P3 z, O - uint8_t Sensor_Data[3];3 O9 V8 u: T+ D1 ]: w
- static uint8_t old_data = 0xff;
; |: o( F1 ~) O) p
+ H q8 P+ ?' }, ?! w5 H" O- pinMode(_DataPin, OUTPUT);* i4 [ B: `# M5 h) _0 y7 m) r" q9 v
- digitalWrite(_DataPin, LOW);. n' e7 }" v ?; I9 }+ x4 [
- delayMicroseconds(980);
" s9 Z* Q2 f3 O: b m1 ~ - digitalWrite(_DataPin, HIGH);
" P; G9 ~8 O5 |7 h/ f) o
& X" [2 q5 S2 l( H* e& X2 m- pinMode(_DataPin, INPUT_PULLUP);
: Q9 _& _! I3 G4 z7 j - delayMicroseconds(10);( Y; p7 M d9 Y: F! y
- + W) |0 {3 `5 ^2 P7 t: P/ f. h
- time_out_flag = millis();' m. |" Q% f: ?2 |5 [
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( j) J& _1 X2 w6 N. b, I0 r - 4 R# P% N% ^& f4 p8 D$ [! u$ h2 W
- LOW_level_read_time = micros();
# `" Q, U; M% w$ c, h; W: _ - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out$ B5 a% w0 x% c4 r& p6 W
- {
. U4 h! _1 \" D# P% L - return 0xff;' Q! D4 L, w" E
- }
r' ]1 W) j- ~" s
0 i8 v, `4 j7 T% g2 P7 x- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
$ c3 T- r3 @6 v( g0 y6 p3 U. b" } - ' o5 {8 k& g7 N4 j
- HIGH_level_read_time = micros();
* e+ t& ], q# ~, I% v9 ] - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
* S& S: \, l0 s( h3 q2 r
' N$ [) I3 [( l* s& p* V/ Y6 g; R" e- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
9 d3 w3 _* d/ V, ~ f! x - {
# ^5 E4 R# ]+ X7 Z0 O - return 0xff;
/ _7 f$ Q2 ~: b5 ^; b8 e& } - }4 P: I, c' ^& h, i, w. d* H
- ( D& G4 l3 v2 B/ _/ s8 \
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))4 T. k5 E3 v! I) c' d7 a1 y* p
- {" P$ k" `& _1 W3 }. ^, v- s) ^
- return 0xff;
$ K3 N& Q9 J+ z: s- g+ X - }* ~" g- |+ x9 R8 u; v$ S8 c
2 W& c) G: T! l- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
+ K; h( j2 i+ ^; m" S - LOW_level_read_time = micros();
0 E c W; F% o - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level( j* z% P0 [8 U* [! R' D
- 3 u7 G3 u \" ^) j5 H" [1 Y: S( C
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out+ a9 n& _) S2 e$ v% _; d
- {
4 H# B! L& ~" X0 W' Q# c - return 0xff;
2 |4 {8 r& a: \ - }; [# K( Q- i( A& ?
% i$ C" \* L: c" `- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
7 h5 C' a l9 c N - {
" W7 ?* ^+ Z( y - return 0xff;
& T n( b; f( m& k' B }# r q - }+ s& u0 ?% g3 W
8 I3 O/ v p v9 e- for(uint8_t k=0; k<3; k++)5 d" V6 @7 \4 @+ m0 K3 q
- {
, d5 l9 D! h" Y, h3 L- d- V - Sensor_Data[k] = 0x00;
- F4 H9 V& w3 k4 {$ c! A1 u - & q$ q$ U) E6 e0 m( d. j
- for(uint8_t i=0;i<8;i++)
( [3 S) i8 A; y `$ b {$ E - {
) ~0 B F: t1 Q5 e' h: S - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level0 }5 b# t4 L1 ~ _6 \
- HIGH_level_read_time = micros();
1 j4 q7 A3 I: {1 h; [ - LOW_level_read_time = micros() - LOW_level_read_time;
) U6 G% E% h$ |) ` - 8 `4 v4 j2 b' M: f9 |
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )0 L( h& I* u2 y) K, j$ q
- {
, ]3 b2 N" k B0 k8 g - return 0xff;4 P3 d2 X2 j$ N0 L3 H
- }3 i8 U4 L. W; B3 r5 M4 o, G
" t# B4 W) e8 {6 E0 f: o; t5 J* i- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );' P+ W( N6 \: i b( _
- LOW_level_read_time = micros();
4 v6 |5 k( l% F( u( H- Z - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level1 G- j; `0 ]) ]! @. G
* A: K$ d, G% F& A9 \# Y9 |& M- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
- u: c' f& p$ g) X. N - {
3 F2 M/ _( f# e6 d L - Sensor_Data[k] |= (0x80 >> i);+ i9 T: F& I4 d& |# k
- }; h/ ~: k5 z1 v
- else if(HIGH_level_read_time >= 100)
6 d3 k8 T7 Z5 z$ _$ k - {1 i# C1 K3 b$ y
- return 0xff;
. i" a& Y9 H) t+ [- h8 F) y" K: s - }7 A1 k% k4 B9 V# N
- 9 d. E$ s# z* t
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)4 P# t' v! s) z) @5 ?5 c5 A8 U& c
- {
1 Z, ?8 c3 s5 V - return 0xff;0 k3 o$ r" \& n3 D) X {1 M- Q
- }
" _/ g3 w0 J5 G5 m K - }
- w( x: b9 {) Z" U - }
; K, U9 R$ |- n5 z2 E - 6 A U) l# X! ~
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
- \9 y+ o# [- h$ I3 z - HIGH_level_read_time = micros();
: h& O+ U2 c8 a8 L - LOW_level_read_time = micros() - LOW_level_read_time;* \2 W9 l" V3 `" w* M
- 0 j0 B: A0 ~# z1 X7 T! \
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )5 X `& u0 d% [8 m8 ?. N
- {
2 A. `2 e8 X5 `5 }$ ^4 z. W - return 0xff;) J7 ~/ U8 Q* o- j0 o
- }
( f/ U. P7 ]; z3 e% Y - 9 z( L& |4 b! g! @% k
- pinMode(_DataPin, OUTPUT);
( Q0 Y+ d5 T! W u4 f2 [8 q - digitalWrite(_DataPin, HIGH);
. i4 b5 g* T) _ - 0 r5 e9 u& R7 F0 k- m3 j
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))' `* U" r* @: T( O, @
- {
) m K9 \( y H0 V- l - old_data = Sensor_Data[0];# q# J2 d7 j2 X1 Y$ z
- return Sensor_Data[0];$ S4 N9 |, k9 J+ K. C
- }
4 Q! K2 }# x; `0 X' u# V6 Z+ o - else3 y6 M# \7 M% F+ u3 X+ d
- {5 Y5 w: y" p7 s
- return old_data;+ g8 N) s0 e( E: x
- }
2 M0 U2 L9 Y6 F& { - }
( }& y9 C5 j; S
複製代碼
$ R/ J) f2 J, F: l: l/ jMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
* h: X/ X3 z* ?1 g& Y0 [
5 M, Y" t' G1 F3 e; u |
|