|
, J+ j1 g `! c0 n9 k
- #include <Arduino.h>1 y1 ^3 o: m. B9 E6 ?2 ]
- #include <MeAuriga.h>
2 i' I Q! K- Z: q - #include "MeLineFollowerArray.h"4 l6 }# N( w: P, w7 D0 V% ^, K% I! X
- 6 F ]. O: v+ M& m% U9 v/ Z) N
- MeEncoderOnBoard Encoder_1(SLOT1);
3 y6 i9 Q; H; Q - MeEncoderOnBoard Encoder_2(SLOT2);
2 o" d+ V5 i* G3 Y - MeLightSensor lightsensor_1(12);
0 a- s2 ] Q+ A - MeLightSensor lightsensor_2(11);" K, a' }% z3 E8 h/ X
- MeBuzzer buzzer;
j$ o* X" r/ r2 s# {9 o+ s. D - MeLineFollowerArray linefollower(PORT_6);8 R$ ^) @$ o: @7 q2 Y
$ _3 i' |: c) U8 L( q" x- #define Error1 1
9 \$ w* T- r/ v9 { - #define Error2 2
% S. V% n6 J# S5 n4 Y d7 d$ `9 z. d" z - #define Error3 3* v$ `, z+ y# ]) n6 R; i
) A9 h `+ i2 w, A9 _8 k3 ~* l- #define Kp 15
% A1 d6 x' u0 E7 Z/ R K5 ` - #define Ki 0.15
. D8 U, T3 t1 p7 W J2 a - #define Kd 0.03
& c7 u+ ~: O3 H - / G& p: ^7 x8 A" X! T; ~
- uint8_t sData;- _( h% q' c, E/ K
- uint8_t D1;
2 E' ~; _$ i0 A; j - uint8_t D2;
+ z, q- @, V$ n! }- ^+ }7 s/ j - uint8_t D3;$ L, }: F5 P3 g9 b6 w4 _
- uint8_t D4;( C+ o8 E2 B( ^
- uint8_t D5;
: x' e% T* M8 e - uint8_t D6;
P2 E. G7 ~2 F8 a8 b- r2 ` - 3 F/ ^3 C' s- A' t, y
- float previous_error = 0;
" D0 w7 s% I1 I4 ] e/ ^ - float integral = 0;
) k ?) Y$ `" @, R. U* }% Z - float derivative = 0;3 J. W' m; `8 t! B
- int Speed = 160;
" G' f$ _! @' E% S5 j2 v - float output;
5 x/ ?2 ?3 t) Z1 A U- D& K. i
" J% N6 p+ ^. s% \& _- byte Left;
2 ~) P' v" O7 K! }8 K/ P - ' x' c9 L% G) d9 t: b& Q$ q
- void setup() . \6 V9 ?& ~% s6 b
- {0 [! `. V- G5 x! Q" i
- //Set PWM 8KHz
# M7 v' {) m* @ j, \8 l - TCCR1A = _BV(WGM10); C( R" S; ]% z. A. G% q
- TCCR1B = _BV(CS11) | _BV(WGM12);2 [0 {. r5 r$ H+ l* g6 c1 `
- TCCR2A = _BV(WGM21) | _BV(WGM20);3 H3 _) n8 I ?& a1 m7 `
- TCCR2B = _BV(CS21);
0 G2 u1 H9 ~% x' z6 s - Serial.begin(9600);
* b% f9 V& F% ?% b' g - buzzer.setpin(45);$ ?7 z1 e _% `$ f' Z, I
- }
" i3 R' ?) L$ f, K( O" H0 y
. w/ l4 W f3 q- void loop()
0 N; M# p% O) ~. S: X - {8 b9 T. P8 o$ Y1 ?" v; M- x9 l R
- while(!((lightsensor_1.read()) < (10)));- B5 g: R$ N/ f7 `! n. E& `
- buzzer.tone(1047, 500);# M5 k* d! I3 K! f0 f9 v( z
- delay(500);' U1 @! y" W& o- \
- do. r8 U% h h6 ]5 I. ]
- {
" J/ G0 Z/ M+ a - int Err = getErr();: t. y' c& x7 ?5 [$ K+ d
- if(D1 == 1)
( t3 W, r9 {7 z+ k; F# t0 v' u - {* j" g! a+ w+ Y+ a
- Left = 1;
# |8 y; x1 X O - }
0 e& u8 r4 j& r - if(D6 == 1)) c2 x7 ~0 A# |' Q0 f! @
- {) r/ ~7 s( b/ i' c
- Left = 0;
8 P: Q* B O6 |/ D - }9 G' S; e- P# h1 u
- if(Err == 99)
9 u! m3 A7 e$ Z& c" C1 w( E2 S - {- }) W( H+ o7 A2 B( m" Z; v0 s
- if(Left == 1)
3 V9 r$ ?$ w; T8 J: o5 D2 F - {# b/ u% l, \" C
- Speed -= 5;
. s& y1 X+ y9 x+ c6 g' I( t$ W - moto(0,Speed);& D$ n6 e; S! l. M* J# D0 J
- do3 T9 B3 D: @3 n! c
- {
+ X3 v7 r- a% c8 K' j$ T+ t - Err = getErr();5 ^1 D! M0 Q" {9 K9 k& n
- }while((D1+D6) == 0);2 `8 g0 v" M% I( A* Q, i9 R
- }" w, j. S+ w- L1 t% `
- else! f/ K( _4 A, Z/ x
- {
. ^; G- D, c3 Y) ?8 d - Speed -= 5;$ n) k3 e% b6 z& w
- moto(Speed,0); 1 v/ g) L7 ^) K. I
- do! B N2 Z" F7 _4 ^- l5 _0 H
- {
& b2 s& f; I4 I9 C0 L" Z - Err = getErr();
o: ]/ u, X0 T% f8 G5 d - }while((D1+D6) == 0);9 z- x3 o4 l& l# E
- }
3 G" T% W! O; m: s5 y. o! }3 I - }
$ Q3 x r8 n$ Y0 t- P - else: J" f2 a. g$ T t
- {
7 Y. E9 y$ ~: V" m) F& i - if((Speed < 160) & (Err < 2)) Speed+=1; 8 ?# b0 P9 ^- e* {2 ]
- if((Speed > 100) & (Err > 2)) Speed-=2; % ^1 a$ Z" ]: `) w
- integral = integral + Err;. B( p9 S# Z7 P* i, L3 \2 {
- derivative = Err - previous_error;
% [; V. O9 b+ \3 l7 V) J - output = Kp*Err + Ki*integral + Kd*derivative; t; s4 i' [$ P( \! r
- moto(int(Speed-output),int(Speed+output));
& w/ c @" U1 f2 X7 z+ L t - previous_error = Err;- p5 H1 s5 F0 e. C
- } {1 S/ x! n" v1 j
- }while(!((lightsensor_2.read()) < (10)));. N9 C8 Q1 U, K2 k1 I6 ]9 o8 e0 ?
- moto(0,0);8 ^8 h0 h0 D2 J" H9 g: L7 H6 {- l1 }
- delay(500);8 G9 x* P( ~5 g7 V$ ? \" A6 y
- buzzer.tone(262, 500);
& B, ?9 S$ \1 M1 g+ E* ^ - }3 x* A: a! V4 J0 Q N) h
6 S. n h+ s Q2 i( r- int getErr()7 ~( X. I% x9 i; W2 h9 F
- {
" f9 c; B, K @0 D3 O* v - sData = linefollower.getValue();
7 O+ r5 Z) x, M" n# R - D1 = ~(sData>>0)&1;( k# R* g f$ z" m) Z* @3 o- \; P
- D2 = ~(sData>>1)&1;+ W( }+ y9 j: \1 i: v4 I- U3 m
- D3 = ~(sData>>2)&1;. X, J$ E' {( A1 o/ c
- D4 = ~(sData>>3)&1; X1 k+ b2 t, V) ~2 L! @
- D5 = ~(sData>>4)&1;* ~2 \! b4 m! c# P
- D6 = ~(sData>>5)&1;
0 b2 w( ]. V+ h- l6 ~/ }" f - int downD = D1+D2+D3+D4+D5+D6;+ ?+ S* H/ _$ A- Y
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);# W+ E# Y2 e( R2 ^1 |4 {
- if(downD == 0): H* F$ Z1 \3 ^/ l9 N1 e# m6 }
- {
3 Y1 ~0 [; X3 P' c! e1 I# J+ s - return 99;2 q! ~" Z( f. }' x/ x" {+ h* r
- }
( A' j7 x l/ q - else
+ j9 [! ~9 R" O9 V. d5 F* G" ^ - {
& F, `1 K; {2 S1 X- m2 i" c - return upD/downD;
$ E2 j9 Y- T% F( G7 [ - }
; _+ ~& t: l! ^1 ^9 j1 k! m6 g - }
! G2 z- ^1 _- |; `3 q - 0 }2 p+ O- y# [+ Z. m4 z* e
- void moto(int Speed_L,int Speed_R)
, z5 ^8 G: D# e% i1 d - {6 }- u \ H1 b, O* \# ~
- Encoder_2.setMotorPwm(Speed_L);/ X/ Q" W- G) f# ]
- Encoder_1.setMotorPwm(-Speed_R);
- V) W% h; R2 [2 d n, a - }
複製代碼 8 g1 S ?2 G8 h: D" y
MeLineFollowerArray.cpp) s6 d: |2 T3 y4 c* ^6 G
- #include "MeLineFollowerArray.h"# G7 j% S4 d0 |) D" r
- " h6 I' \6 t& C6 l
- #ifdef ME_PORT_DEFINED' k9 B9 @4 M% d, p2 ~9 O( ]8 m( h
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
; k2 a) U0 ^" P4 K5 K0 @5 ^5 E. l2 i - {
4 M! s/ h' x! M5 B - ! k) F6 G- B/ ^2 a
- }3 h0 K6 a1 w, e
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)+ {- P; _6 K8 x' O
- {
- F2 h) X7 j" Z5 x' ?+ Y - _DataPin = mePort[port].s2;
7 z5 `/ u z5 m; u" y7 I - pinMode(_DataPin, OUTPUT); ( U9 f- ?2 C/ T* i# W
- digitalWrite(_DataPin, HIGH);
7 `: [, y: P& E+ O - }
& k. B& a, ~* H( {7 F$ p - #else // ME_PORT_DEFINED5 X# w5 b) R" q4 n
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
( E2 ~5 s, ?, E5 { i - {% {5 }- v$ N) h, e
- _DataPin = pin;
3 m2 I' p% K: H5 j0 b, o Y - pinMode(_DataPin, OUTPUT); # Q& L0 p3 m Z- ^3 |, |
- digitalWrite(_DataPin, HIGH);0 k$ U7 Y9 I f; l% J; o- z& U
- }% x# Z+ p. K2 n' t6 {& C# \
- #endif // ME_PORT_DEFINED
/ D/ R. m/ B x. ^ - 7 z, K# S. G# z! q
6 ~$ a2 n1 J1 d7 l! M* A- void MeLineFollowerArray::setpin(uint8_t pin)4 `, v e1 y' E2 }( {9 F
- {
# Z% d, S1 [9 Z; N% m: y0 M - _DataPin = pin;
0 p% J$ S/ A6 w) i - pinMode(_DataPin, OUTPUT);
. h0 l* C; L) ]& [6 }5 v4 p - digitalWrite(_DataPin, HIGH);' x/ C5 q7 @" p8 Y; c
/ H N2 m0 f2 |- #ifdef ME_PORT_DEFINED
% V7 { Q6 f* d% v$ x7 O- @5 h - s2 = pin;
! W( w& A% P+ ^6 a+ H: Y( m - #endif
, ]- a# V' u( B6 ? - }, U3 W6 v4 L% Z; U3 t
- ) k1 }1 f0 F- m$ z
- uint8_t MeLineFollowerArray::getValue()
. X# @% r8 P6 [( N! h# \" S# Z - {
) }3 u4 x" r) O0 d - uint32_t LOW_level_read_time;
& Q1 ]8 r; Y* T/ K# M0 X - uint32_t HIGH_level_read_time;* d$ d& D0 n+ ]7 `/ c
- uint32_t time_out_flag;
3 q# P4 i6 F/ c+ v1 p) O" l - uint8_t Sensor_Data[3];: u" J: [6 F; R2 ^% n G
- static uint8_t old_data = 0xff;( J5 V1 F7 @ [5 o/ I% R* `( S
- & }* J$ M; u+ Z
- pinMode(_DataPin, OUTPUT);
' T2 h/ {: N; b6 G( z( }$ f9 i6 f - digitalWrite(_DataPin, LOW);
) _% Z$ W" D6 ?4 P- R3 m0 @4 j# V - delayMicroseconds(980);3 i8 {" i% a# H$ `. g9 K3 b: F: V" `9 o
- digitalWrite(_DataPin, HIGH);
( ~9 P7 H; `" {' d! A! |
3 C2 o) o2 z* o$ r* y- pinMode(_DataPin, INPUT_PULLUP);
' q; R+ I3 Y7 g8 i - delayMicroseconds(10);
/ o2 N7 x; E: @ - 2 D& N5 Q, M( z% J
- time_out_flag = millis();3 I h. v! l& Y3 L8 u z
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
7 b0 t* `2 n$ i - ' }9 I* W9 z q1 `- j2 M
- LOW_level_read_time = micros();7 w. A0 g3 |1 u, J E2 J
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out7 u$ m2 N: ~5 S$ N8 S- H
- {
, N- B+ g( f C( f" C - return 0xff;3 x- O, u% D6 x0 p; p( {0 w# I
- } y; y+ e& B7 Z i) x
- p5 Y# w1 _% ]0 {$ J- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
. W, G0 l3 ?4 W% O4 @: Z, A
' Y. x9 I! e9 ^4 @* Q7 D' a- HIGH_level_read_time = micros();& p9 _9 i$ u3 ]! y% `% q& x; `
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level9 p- q: l4 L3 `9 K
- . s4 j7 E7 r) p' @
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out( u4 _$ w- C. `0 z, V3 E7 d& b1 S
- {
# k# S+ o. ~* t: h; v& k6 U' k/ l. | - return 0xff;
$ ]; r( Q+ o6 ?$ T! x5 G. H - }
% m' ]+ N2 H1 v+ h. f6 u+ k7 Q/ n6 p - 6 k% r5 f9 q9 M7 K+ }5 c
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
$ ]& z/ j2 z3 h/ }3 @ - {" V8 q4 p, ~& c. z0 \4 X8 q- r
- return 0xff;' a' D$ ?' P% D$ f
- }) u9 e* }5 s1 A* X" C1 y- P! } P
_+ \5 K* ~# n4 x0 P7 H- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
" X$ r( n1 t( b( \; i# U - LOW_level_read_time = micros();
3 j, V) o8 M J; D - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level! ?5 ?7 C+ ?, b
- % U1 I7 M# Q; c% h/ P$ Y
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
" h' T& T% M) S" @: ]/ w - {
! o; l; L% W7 W8 Y M2 e) S. S2 p - return 0xff;
6 U6 B0 X1 [7 Q$ v' }/ @8 q( R - }
3 z2 o( A( b, O" Q' q& I/ Q1 Z$ ?$ o - + H" l' L; \8 }) ?
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))9 `+ B( y9 Q* E7 q2 C
- {- H9 n5 p/ }3 B* }; a1 B
- return 0xff;' Z7 n" K9 g' v, S/ A
- }! u: }' A0 m1 d0 B# u
- * d( i* d7 b$ p
- for(uint8_t k=0; k<3; k++)* ]8 B7 W' S. P! i# f5 k
- {
% f4 d. I" C. ~. v& p6 ^0 U - Sensor_Data[k] = 0x00;
+ a6 [/ N: d; c A
J6 L; R' k. y. u3 x- for(uint8_t i=0;i<8;i++)
3 w# o. U, v2 Z+ H - {
, p, K' d' B' n0 Z( q - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
& E i% d" m4 X7 H3 X - HIGH_level_read_time = micros();, u" W5 _6 J q, `* L2 O
- LOW_level_read_time = micros() - LOW_level_read_time;
. _$ s U* r! z3 F* R$ l
+ n. G% K) T! @! d6 x( g+ j- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ), ~! m2 i7 z9 i. t6 h8 L% ] ^
- {
+ j, d3 V2 k* T2 Z3 } - return 0xff;
! s! ]9 i$ z( L, ~% c3 I' W+ O - }
4 k1 B" g1 T2 x - , ^' u9 h% N/ K! }- M
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
8 U9 A. I" \* @, ?6 E - LOW_level_read_time = micros();# m, n4 p; ~/ e4 x3 I& K' t
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
& p% v, x4 I9 J, D+ H - 4 s5 b$ [: }8 B: h# T; \
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1) J; [6 Q: @% M2 P! T% E
- {1 L& @! b3 b% I. B" ]; c
- Sensor_Data[k] |= (0x80 >> i);' \8 H) j9 X: Q
- }7 ]/ z) ?+ v: e; u6 d r
- else if(HIGH_level_read_time >= 100)+ N) W* j5 x _, F9 T- u
- {( r( [3 |8 k& A7 i$ { _. d9 e; U
- return 0xff;
& v0 V9 |' s* R Z9 S3 Z - } V N! M3 E( I) I- Q/ u$ O
' d! ]) ]4 b* d- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)3 A/ W# G# N) d
- {( B1 A1 m& X+ A3 H3 b+ S, x
- return 0xff;
* m& i, _! O6 @* ~9 H) q8 ^1 b: `% l - }+ n6 t, n; N0 T) J) k0 Y
- }3 e/ X7 ~9 Y2 s4 b
- }" M0 ~+ N3 r1 J( b8 A" E
9 t0 e) i8 @1 U- Q' N- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
* j7 ?& u; ^* C0 u" a - HIGH_level_read_time = micros();
! A5 @9 A! h+ [- O1 Z( B" H. f - LOW_level_read_time = micros() - LOW_level_read_time;2 o% H/ s2 ^8 W A' N& h" d
3 x5 p5 z8 E2 }2 Y. U( @, M8 a- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
7 x: A* e3 S. \. J - {
+ d: y/ p L7 Z% C; d8 Z: @ - return 0xff;
8 B( X+ C$ M7 E7 B, ?8 M# M - }
T, l' S+ b% v1 O
# o' g: Q9 d1 P/ C4 i- pinMode(_DataPin, OUTPUT);$ _9 V% z# V" X; I
- digitalWrite(_DataPin, HIGH);
7 s$ B3 A x8 x% A8 e9 F8 E
4 H* [, A; j6 r b" ^8 O- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
+ D% \7 u2 t9 y6 k# O" {* c& @ - {; w8 _" q' b% a) b% L
- old_data = Sensor_Data[0];
) l7 z9 o7 \8 }' @& z z - return Sensor_Data[0];! L8 I& u. r: x W6 h" y
- }
; N/ F7 ^/ f' m8 X2 y; } - else
$ u" j% c- N3 S; Q' w n - {( ?4 D2 _% t. M
- return old_data;6 ?, n8 T3 N; H9 o* k5 y
- }
0 }. F4 d7 M z' p% ^% V( P - }' n, Z" y6 k- e9 g! E$ W; z
複製代碼
: P" y0 g/ }/ d/ g% n9 U* o: YMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
; Z$ Y5 t9 m M% C7 I2 K) {! m
0 ]# w; p. b+ W, a% Y+ p |
|