|
|
- o4 h/ e7 p: A% {8 ^" V7 [- #include <Arduino.h>& R9 F: p5 u7 J% r% o$ l x
- #include <MeAuriga.h>; p* i/ h$ i) L) E% S
- #include "MeLineFollowerArray.h"0 a; B1 E/ c4 s' m, ?- C
- 2 e6 `% n5 d0 h) F; Q( e. n
- MeEncoderOnBoard Encoder_1(SLOT1);8 _( @$ n' J) J- E% a5 _( P4 ?
- MeEncoderOnBoard Encoder_2(SLOT2);% u4 I- a2 j1 e, g8 v
- MeLightSensor lightsensor_1(12);
+ ~2 ]7 v; o$ Z0 @- Z% T/ v - MeLightSensor lightsensor_2(11);8 a0 f9 ]$ t3 u4 d3 S; v
- MeBuzzer buzzer;
) d7 z9 ]- X, o2 R3 l3 M4 ]/ \ - MeLineFollowerArray linefollower(PORT_6);
6 [$ S1 _+ f% I+ t' @* b. j - ! A$ n: C) d/ A2 ~$ r; ]: a
- #define Error1 1
P3 c" ~" H, Q - #define Error2 2- Y# m5 O8 p) v3 o3 z0 u
- #define Error3 3
" K; k' Y+ p$ H! U - & } F3 {, b% q, E- f. G+ {
- #define Kp 15: r Z% j, o6 r3 ^8 m
- #define Ki 0.155 l7 X9 ?/ }! H8 t
- #define Kd 0.032 q; L2 b0 d7 |' w
+ s* a; H% B0 x0 o' {% F4 \" i- uint8_t sData;! @- U4 T$ v9 `- {9 n \7 m; o2 p
- uint8_t D1;: l, Y2 _! E& i, M' F
- uint8_t D2;6 r6 |2 w) q \
- uint8_t D3;
# x+ ]* @' ?2 ^% x- p0 C - uint8_t D4;
; N' [2 l5 G2 \7 N; H. | - uint8_t D5;
" n' V- ~+ @3 A: D2 Y) Y - uint8_t D6;
1 N2 J" c: ~' {0 [
X/ A$ |' Z9 K2 `) Z8 H" U- float previous_error = 0;
, k5 g1 A( g, \9 \ @- t4 ~ - float integral = 0;3 l9 H. H$ `' u1 W
- float derivative = 0;
4 b; r( ?, q5 a+ S x - int Speed = 160;
: ]8 x# R9 H! J, x- ] - float output;$ Q$ S0 K: F& f6 h
- . j8 P) w: @/ c) O7 T5 Y
- byte Left;: g1 V: {6 ]6 h$ Q
* Z) L' y9 t) B; f1 _- void setup() 2 r% }. }/ w- c+ b+ [' q, K
- {8 i* C x' p/ k! v k) t9 c
- //Set PWM 8KHz; r5 g; A: k/ c# g5 |0 V; T
- TCCR1A = _BV(WGM10);% I' x! N% r( V# n3 \
- TCCR1B = _BV(CS11) | _BV(WGM12);
: _ ^/ {. B h% i - TCCR2A = _BV(WGM21) | _BV(WGM20);) X. A; N2 x" s: m6 o& j9 ~
- TCCR2B = _BV(CS21);
! t) P( a9 K1 M" ?. L# x& E - Serial.begin(9600);
" F! V5 G; F; u - buzzer.setpin(45);. X: P$ E. p, e; t+ V5 ?
- }
/ P" v- W7 A1 j; X4 ~ I8 _ - $ g/ M2 r& J# F+ q' A# C
- void loop()
! B0 a% \$ K! M1 R) D5 {3 G- A - {
5 X5 N1 X0 v3 L& S3 D0 T0 U; \" l( h: w - while(!((lightsensor_1.read()) < (10)));
* B% w! S8 I7 W - buzzer.tone(1047, 500);9 ?! L6 m! d1 T' S/ ?/ @
- delay(500);
# p- T( \ }* N. l - do
. G, i T1 e! Y q. ~' P, b - {
$ [/ b& y4 k" t: ` - int Err = getErr();
1 i: m3 C( Q- @" q - if(D1 == 1)
& q* u6 i% z2 j" Y$ \ - {) i9 h+ v& \+ O2 U* m5 X
- Left = 1;8 P8 D" w9 K; r/ Y
- }
$ K- ^- k" Q9 g" \ f: @ - if(D6 == 1)
$ s& y) i+ |6 `& t3 [9 j7 Z \1 H - {9 S- ?' R- r0 q6 N- ~* q
- Left = 0;% u, r9 j4 Z) T2 d$ i9 K
- }
: `' M6 J5 @& B; ~ - if(Err == 99)* {% a3 f) q9 U- m
- {- n+ C- C3 n$ a: |4 }0 s
- if(Left == 1)" p; \$ J9 \! u j* d
- {
+ k1 m( W5 Y1 s: Y2 _ - Speed -= 5;+ D! B, n7 ?7 F( o6 [
- moto(0,Speed);
3 F7 m- F+ Y. E, |' r# H - do
. Z e2 b6 C! D8 [% r% [9 V( v8 ? - {& {4 j6 Z1 y9 A. Q1 n# f! k% s3 F
- Err = getErr();
$ W1 C7 W& [" v% }( Y4 t' O4 O- l - }while((D1+D6) == 0);
$ o- w7 f7 {3 @, x/ M; g6 ] - }
$ d5 {& G: [( N - else0 X3 C& [* @- ^9 W9 ?9 z" }' }2 B
- {' m5 o& t9 q# a, y2 p5 s
- Speed -= 5;1 H8 v. ?6 V8 m) ^
- moto(Speed,0);
6 Z- ]( J1 H% K9 B( Z - do! E8 o' Q3 F3 c) V
- {# L b/ M9 w2 J* h
- Err = getErr();
/ n2 ~3 D- V9 m2 R - }while((D1+D6) == 0);
; E, O: ?7 S9 o: e - }
' ^* d" s) t d# `* D% p0 W - }
* w& p* y4 B) l" _0 n7 _. {) j# W - else" J0 F3 N& u, Z8 j8 ]
- {% m1 {# ]" R2 {
- if((Speed < 160) & (Err < 2)) Speed+=1;
M- H) B( }+ a# e8 ` - if((Speed > 100) & (Err > 2)) Speed-=2; % O/ ]* k" X: }* g8 r
- integral = integral + Err;6 H3 r( y6 p( G: r: a
- derivative = Err - previous_error;8 D' u& | E$ E8 R7 _; G! s8 h7 N
- output = Kp*Err + Ki*integral + Kd*derivative;
) h) b6 G, ^! @' d - moto(int(Speed-output),int(Speed+output));* |, W0 i. W. D7 f6 M* T7 f
- previous_error = Err;
2 q" \, B, l5 }' g' z - }
+ P0 ~. B$ Z, B. D - }while(!((lightsensor_2.read()) < (10)));+ D+ H a0 A- o' t. n; f
- moto(0,0);
, b7 x! l$ `0 I& Z8 ~# R - delay(500);6 r1 R* ^. W! V; `/ q: y7 [
- buzzer.tone(262, 500);$ }8 q Q' [3 Z, z
- }
: q Y d0 u. C8 [% f
$ N, R) A; e( y- v8 z+ O& a- int getErr()
$ ]* E. ^9 J" _) M7 b6 i - { , U$ M- D7 m3 h; r( D5 {
- sData = linefollower.getValue();$ o% e( i7 @4 h, E! X( ~$ Z. J
- D1 = ~(sData>>0)&1;
, |+ E) ~' J% B0 ] - D2 = ~(sData>>1)&1;: o8 ]0 ?6 Q. [! d2 K5 c
- D3 = ~(sData>>2)&1;! w! v o0 {7 f+ r6 @) x
- D4 = ~(sData>>3)&1;
3 L# m" \3 Y$ r$ h& } - D5 = ~(sData>>4)&1;' I3 p. z% [2 N$ G7 [" x2 T, U
- D6 = ~(sData>>5)&1;' f) o# y* P- J" `* _
- int downD = D1+D2+D3+D4+D5+D6;+ j7 F9 ^. G: K# z7 y4 T
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
+ _$ b4 S$ B' |* J2 G6 @% ~ - if(downD == 0); C& a# @" y" ~; t* O9 ~' x- E
- {
) o: e5 Q) f; |. h1 S7 s - return 99;
8 q# ]. Y9 q) V5 w9 s! U* h5 G& v. } - }; j; `% O" B/ k" X
- else
; m8 S6 i. q, E - {* {: V) K4 K; n$ ~
- return upD/downD;
5 m! `9 y% k0 j+ H" {9 x - }% F) h9 \2 B# B8 @3 k$ p
- }+ Z( G D1 r$ B, v
+ f5 h# D- Z0 `& G6 M: ]- void moto(int Speed_L,int Speed_R), B Y+ L, y6 C
- {
* J/ g' L2 F; U5 J' A - Encoder_2.setMotorPwm(Speed_L);
9 V; C* A: o6 s) \! P - Encoder_1.setMotorPwm(-Speed_R);
1 t5 Y! |7 } J7 q' F - }
複製代碼
9 U' |) M$ R5 w3 n4 uMeLineFollowerArray.cpp
/ ]4 C7 O6 I( ?% E& _- #include "MeLineFollowerArray.h"
5 b8 R+ O5 V( W. Q* ^( a - % J1 Y$ X8 [' a- c5 t2 u( N
- #ifdef ME_PORT_DEFINED
( X; h5 n4 @+ S9 ~( H/ u& N& ^ - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
1 n/ x6 o4 \2 {4 O1 i4 m! j% { - {0 H- T* j) D6 _+ {
- . {- f/ A8 @9 `1 k; ^! e @
- }
( ?* r3 K( |# }% P+ r6 R, E* i - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)3 x7 u6 F: _! r9 \" Q" b
- {
- b* \ E% ^! ` - _DataPin = mePort[port].s2;
) R, G W. E7 ]# b - pinMode(_DataPin, OUTPUT);
, f- A% M* y# k/ D( Q: j - digitalWrite(_DataPin, HIGH);. f6 _! |& @) Z
- }/ m% E/ T, X- g V+ S
- #else // ME_PORT_DEFINED
- W: o: y/ }; w1 Z7 @ - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)% f" o8 W0 X$ Z' y- ? ^
- {
M7 y- ]/ } n& }5 @ - _DataPin = pin;
* s4 f( @; g6 b2 _# k8 k - pinMode(_DataPin, OUTPUT); 3 s& W4 r6 }8 z. ~0 X3 E" h* M
- digitalWrite(_DataPin, HIGH);- `" X" m3 `2 Z' t' a
- }9 E& i. e2 p' J. N8 C) ~. M
- #endif // ME_PORT_DEFINED( t$ J+ z) a' h) c$ e/ W( m" F
2 I* a! L F% Z' i4 B- E+ F- H8 y8 Z( L* ^& S7 N3 a) A
- void MeLineFollowerArray::setpin(uint8_t pin)
, D! k2 v$ P, A: r, ^ - {) N7 p: V0 r6 m$ ~9 e6 ]
- _DataPin = pin;& g! s! A; w# F0 F$ O" y
- pinMode(_DataPin, OUTPUT);
y, x! n2 C: ]4 |$ o - digitalWrite(_DataPin, HIGH);
- c( p+ F: b0 {5 i' g9 R) x - 1 X& Z: { H6 N$ i9 m
- #ifdef ME_PORT_DEFINED
5 S7 u! F! t+ _5 d' s5 g1 ]- x, _ - s2 = pin;
6 N: p: G( ^% G( w6 k$ @8 R - #endif
2 G5 E0 t+ G$ z& b - }8 ~ D6 z/ z7 X" Y- F
- ! Z/ a9 p# S. p; D) j9 R
- uint8_t MeLineFollowerArray::getValue()! M1 X! {! j& _- R: i
- {+ o; ^# G' Y Z+ j% S
- uint32_t LOW_level_read_time;
2 v$ J- N) X6 b9 V% G( d ? - uint32_t HIGH_level_read_time;
?6 e" |- t6 M6 s# x8 Y - uint32_t time_out_flag;" [8 X6 P l+ ~! x7 E
- uint8_t Sensor_Data[3];
& n- x2 o* z2 u - static uint8_t old_data = 0xff;
3 z! z0 u0 ^4 i+ p j& `3 z
5 I5 a8 n+ q3 F- pinMode(_DataPin, OUTPUT);$ r: c$ g. ?& J# N
- digitalWrite(_DataPin, LOW); L _; p0 L P2 o" X8 t* j. P
- delayMicroseconds(980);- W* K' |4 s0 ^% ~
- digitalWrite(_DataPin, HIGH);
. t5 u, B* y4 ^+ E7 K
- O( p8 ]+ a% }2 Y- pinMode(_DataPin, INPUT_PULLUP);# p# f* F1 ]4 Y: r/ j/ l
- delayMicroseconds(10);3 S2 x2 X' ?0 L; M$ d
% m. \: G: v' k. h1 n- v; s: m- time_out_flag = millis();
: h& P( E4 P$ p( H6 \5 f1 H - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );; D; {: n) A+ {7 G8 S" n
- z$ H- t" H0 X" ~2 G- C9 ~2 f5 P- LOW_level_read_time = micros();
s, b# s- r: N) C# m2 o - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
: h: M" E: y, v6 C7 x8 ?0 x* q! h - {
9 g( R& i7 T; c, Y) V - return 0xff;
3 y8 W4 ]7 v# Y" a% L. E - }$ A, X+ ^& d0 j
- ' P" s2 B- l! h6 ]1 h
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ o6 G, D6 ^4 P" b5 f
. O9 q- }, Y1 }" L/ c- HIGH_level_read_time = micros();4 ^7 k1 ~3 g+ h- O2 f( {
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
, }8 R& } N" z/ V8 Y D) b
/ z4 \- n3 o8 @; }- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
/ i6 B$ @# c; ?2 P - {
& a3 _ O ?1 s - return 0xff;
3 k5 x9 s0 W& y$ S8 o2 Z - }5 R; C2 I3 `4 i
- 1 `& C N3 j1 L2 H3 C
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
3 n8 G8 Y6 a) } - {% } C" @3 G5 Q, {5 U2 o9 z" r& n
- return 0xff;
8 t7 n2 H+ P+ `! r* Z% V - }5 f$ v. H0 `6 a" ~: B* G: }, P
- ( S' F) d i( o6 ^
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );, ?8 H" D: [! r) ?( L* r) G3 k( X! V
- LOW_level_read_time = micros();: ~' L3 M* L: M; C, D2 `
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
* v0 R, z+ l" E0 o - & E0 F8 u+ l: ~. g1 s
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
~$ L$ S1 O! H; _ - {, P: y+ w* r5 r
- return 0xff;3 i* _, _+ {- Q
- }* V2 N8 H C C3 \9 M3 w
# n. W7 s; \0 O" J- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
: ^9 w/ Y0 x( P' I - {
6 r7 x6 T9 w7 O/ a - return 0xff;
$ v6 d: j( ]$ U1 K, ] - }4 x1 I5 S: q. M& ]
, N- l* H3 l( h- for(uint8_t k=0; k<3; k++)# [: C. @, M7 w, v7 a
- {4 N5 {4 R/ t; W9 S8 o8 s
- Sensor_Data[k] = 0x00;& i3 p6 _& I* v% Z7 W! |; ~" ~
- 4 R5 Y1 P- K2 T* r+ k
- for(uint8_t i=0;i<8;i++)
6 m$ Y& m8 B- A0 \* n& x5 ?6 @ - {
% J4 y9 F# D5 @2 I6 d- q6 p - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level% [. \( j1 d6 h) O1 ~$ K
- HIGH_level_read_time = micros();
8 X7 j1 c' w6 t% R( p - LOW_level_read_time = micros() - LOW_level_read_time;3 F& L$ v6 }3 ~5 a) t, S( Q
- % U' [8 O' C" g5 D
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )/ v6 D2 b$ q' l
- {
, x s0 K8 m# v' v$ c; u - return 0xff;% i8 z1 G' P3 }6 V" V
- }
1 u; u0 ^1 x+ S+ F" n- g' x* ] - / g; p7 L* j5 |1 D6 t6 Z5 L* R
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );% u9 r% z5 U1 F; f2 t6 c2 r
- LOW_level_read_time = micros();% H5 s T# p% A" H, Q, j3 J
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level% j# }8 u; ]' J! m
- ( y! H% X+ d$ @) V
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1& ?2 _ G! L* s) x
- {0 Y& O5 b0 p$ z7 G# _0 K, Y# @2 V
- Sensor_Data[k] |= (0x80 >> i);& o6 s8 g) \, w. Q! ?7 [2 r
- }6 Z9 J Z( H& u- d% ~7 r8 j
- else if(HIGH_level_read_time >= 100)
% h _9 z8 M p8 S& a6 R: ]# d* B - {/ Z8 ?, m$ o/ I! a( Q, ~4 Z1 i
- return 0xff;
5 H1 T* F8 q5 P6 M - }* I* t% I. V. W1 v
/ q8 o6 A& c0 _+ y4 ~- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
m0 F6 \' y% d$ V - {
7 K$ \( Y) @; U3 i - return 0xff;
4 @8 M& J% P9 B: T - }/ Y3 E5 S$ b* r' W" |5 S( Y) |
- }
! H. j. Q6 E; u0 n9 N# Q - }
3 S# F# a( y4 E3 x5 K
' b( o+ I' r* g+ a5 y, ]- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
3 ^9 @8 E# e& Y# M. w( j$ g! D - HIGH_level_read_time = micros();
8 D" V" \( e A& r - LOW_level_read_time = micros() - LOW_level_read_time;
" p( g* }. g" {1 d& F$ a$ |1 k - " `% g/ s8 A; X5 i9 ^( i5 e
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )! F# G X, G5 u+ f, G( `
- {; R+ F4 }/ d. B% b0 H Y. l
- return 0xff;
2 S# z! B/ O4 c$ ]3 {% E - }
# n ~: D f2 D ~ - 3 c2 B& j C4 S. w
- pinMode(_DataPin, OUTPUT);
7 e3 S4 J7 O+ j - digitalWrite(_DataPin, HIGH);( I; V, U) W6 {2 i* H* l8 x& t. v
- 7 n' \5 t i* C" N
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
& k% r. t/ E! }) U- C/ | - {
, m4 Z1 [. A# D - old_data = Sensor_Data[0];4 ^$ _ ~9 z: L& b4 M
- return Sensor_Data[0];
4 h1 P; c0 l3 }2 w: O& E( F; O - }
1 z7 p2 R& N4 B7 l - else3 \! [5 V% v5 M3 k* ?$ Z% i
- {
9 q% a& p5 W( M2 K* W# D& ] - return old_data;
/ {8 U7 u4 V8 c& R- ~ - }3 ` x% N& J s3 f5 H0 T
- }
# s% \3 ^0 l- d% j. N/ S" c
複製代碼 ' c* a# Y% @# U: U0 p
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
: y4 N: N8 h6 A' A j, a0 w E- D1 i5 Q- x s; ?! X) }! C' I
|
|