|
|
4 ?3 A5 z, i( g- #include <Arduino.h>
. v* E5 }, `! p3 W - #include <MeAuriga.h>; |# C: ^* W: N* ^
- #include "MeLineFollowerArray.h"0 Z; s4 k4 P+ n) G
- + ?: d& Y5 [+ W$ p2 b+ j
- MeEncoderOnBoard Encoder_1(SLOT1);2 j: b# y: t1 `9 Y# R4 j; c
- MeEncoderOnBoard Encoder_2(SLOT2);" X/ n' p% D& J# R5 f8 c
- MeLightSensor lightsensor_1(12);2 x% r8 d2 `! U' F4 ^: J
- MeLightSensor lightsensor_2(11);2 A1 T: R% ~( g! w
- MeBuzzer buzzer;
. `2 t& v5 L! R0 L - MeLineFollowerArray linefollower(PORT_6);
0 f. o0 p6 S: m* U+ k
6 Q" W8 R6 b5 X0 J5 y* [3 B- #define Error1 1& k0 p0 Z7 G+ B1 O4 P: }
- #define Error2 2( H$ r# y4 t+ A9 i
- #define Error3 3) n; Z1 _5 J3 {- f( w+ s2 e
- 8 X& }" \0 W5 G# o& A4 j
- #define Kp 15
. O; O+ ~5 ^! ~) h* ~ - #define Ki 0.150 @/ o3 J: y3 G" ^9 R$ F
- #define Kd 0.038 A! d/ g* c5 ^! o! _
/ P1 L* a* @* ]9 r$ n7 Q1 T; R- uint8_t sData;7 n, G& B. n- x- v; n1 |
- uint8_t D1;
% N$ H. D' h; H - uint8_t D2;6 S9 [/ _. V4 N" c
- uint8_t D3;0 _* x7 ?7 o8 v8 Q, i
- uint8_t D4;" t _7 F' U. V! d1 i. _ E- B
- uint8_t D5;
) |( n, w' q) t& B! B ?! { - uint8_t D6;, {( n6 S/ ~, H9 t& w
- ! T/ T. E5 g. k3 T" s% r- @6 T j6 W6 I
- float previous_error = 0;
& F! C7 v3 c+ Y. H3 t2 [* t& X - float integral = 0;
0 d" Q0 v7 N- X# g6 K6 ? - float derivative = 0;
. _+ X& E# Y8 a1 e1 C! g5 Z - int Speed = 160;7 K+ t0 b( C2 `
- float output;! D2 M3 I/ @0 E" _: Z7 |# n
- + [5 @( v$ a% g4 }0 G& I% {
- byte Left;2 h5 K5 k" ?- ^0 ?% |
G7 r9 Q( p# m5 @; H; q" j, a1 }/ o- void setup() 8 b# N; B/ f( B' p% C
- {
+ e8 q' i4 t3 Z) t, v } - //Set PWM 8KHz m3 V8 A& |8 x9 D* o( ]
- TCCR1A = _BV(WGM10);" j1 h) [2 D7 @0 ?4 P) }
- TCCR1B = _BV(CS11) | _BV(WGM12);4 i- O/ P% F- H S
- TCCR2A = _BV(WGM21) | _BV(WGM20);0 N: J. E. y* k: ^: a
- TCCR2B = _BV(CS21);
/ @0 Q* c+ }0 } - Serial.begin(9600);
8 S: ]- f2 G- K$ E- p7 O; B - buzzer.setpin(45);
) B2 I; S3 L8 Z6 l+ i# Z - }
/ F# ~ t8 @" v1 { b4 u T. |3 T
# Y; V' H- L& a' `- void loop()( G, ]9 \# b) M. o+ M& |
- {8 u- r/ C( |0 h3 U
- while(!((lightsensor_1.read()) < (10)));
2 D7 g: L4 ?8 g- b9 O, }# w0 R - buzzer.tone(1047, 500);, ?! r9 p, {7 C. B- h5 _( Y6 n8 a- [
- delay(500);
* [/ Z) i& s6 j7 y, q3 o/ j - do! R* f% b) o) o" F+ Y8 S0 S7 z
- {6 d2 y3 \0 m; v. J
- int Err = getErr();" |9 C. f0 G ?+ f5 h6 q) u B
- if(D1 == 1)( l; l/ Q" a; c1 q
- {
p6 e7 b c7 z - Left = 1;- ?; [1 a& B* \% q( U
- }
+ _9 Y5 T; D8 |% {8 L1 { - if(D6 == 1): M2 g$ v$ D9 i5 N
- {- X/ ]9 C2 Y8 A) O7 s1 s) O
- Left = 0;
' F3 a1 [, d/ J, m8 A T8 [ - }
' j$ o0 u; C8 K2 S1 I: ` - if(Err == 99)1 C3 h+ _: E+ |) Z9 g+ c( k: n& M
- {" N4 j0 j+ t" T& }: k) X
- if(Left == 1)/ J* w0 _7 ? X2 W1 Y o$ z
- {
; g3 H' X" x. W5 z- \0 V; r2 Y - Speed -= 5;
! v# |; P& g; K* n7 ^3 j7 V - moto(0,Speed);
?$ \9 w$ U9 I( E4 r1 P: v6 O - do6 W1 A# f$ j, K+ F1 h7 X
- {+ W# h# k; h- N
- Err = getErr();
; Q9 `( f+ |3 Q - }while((D1+D6) == 0);
1 S+ P2 c# |3 r" z* ?% s) n0 ` - }
1 w! q/ a1 q3 C# C/ k# P - else5 ?! M, W* y# E/ S( `! r
- {
& O" t1 q( u1 t% k% a8 d - Speed -= 5;
1 [1 O# m8 c! W6 b$ F& b ` - moto(Speed,0);
( A+ ]9 ?4 d' m% Q0 M - do
- D- [, M8 e9 B* G - { Y5 }* u' y5 y# B; f% p
- Err = getErr();
, z3 F7 T4 ]# g F8 \* x N - }while((D1+D6) == 0);9 d0 C7 L6 U& I6 S
- }
3 Q% T9 v3 O; u& G( R - }
# O( C4 g- y' s" [/ _2 n - else3 c! s6 [4 S9 S8 ]+ Z, M& H
- {
+ L% y/ _( ^* F6 e1 [6 b* j4 M- | - if((Speed < 160) & (Err < 2)) Speed+=1;
d. h' e- X1 |8 U0 ` - if((Speed > 100) & (Err > 2)) Speed-=2; : E; b2 b( U4 ^
- integral = integral + Err;
$ \& H5 q9 e/ M* b1 V4 Q9 ]) L9 O- Y - derivative = Err - previous_error;
, \/ p( k3 u: u - output = Kp*Err + Ki*integral + Kd*derivative;6 g; Q2 \# p; a7 Z$ \7 ^! b7 r
- moto(int(Speed-output),int(Speed+output));
& Y! E1 M. J8 l* ~ - previous_error = Err;+ M J' u( U; y. m c
- }- a% `$ \% q3 K7 q) I1 ^0 m
- }while(!((lightsensor_2.read()) < (10)));
0 F1 C% y/ I- V5 N* D - moto(0,0);
* q- ~* ? I" ^ - delay(500);# O, w% O8 P. @' F- Z' V
- buzzer.tone(262, 500);
9 w, }8 D1 W9 t$ l5 h3 I - }
6 G6 E) H4 }. }; c, a0 M - $ Z% F1 \9 r6 i+ s+ f3 U
- int getErr()
. n: t: @ k3 S) O o5 r! g! p - { 2 U: v9 g& R" y8 e2 J
- sData = linefollower.getValue();* ]- P* ~1 R! { i
- D1 = ~(sData>>0)&1;( c9 f/ G8 k6 L+ `
- D2 = ~(sData>>1)&1;
+ f( d' I; k8 r9 I3 J( d - D3 = ~(sData>>2)&1;; S0 {: r9 O$ P/ o
- D4 = ~(sData>>3)&1;
/ Q/ }7 P5 r& i3 W - D5 = ~(sData>>4)&1;9 a# z2 j9 d' H
- D6 = ~(sData>>5)&1;
. D7 d% F0 N4 l( C1 I0 V H - int downD = D1+D2+D3+D4+D5+D6;- j1 e& k G2 o
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);' ~# B& C7 V. T8 O
- if(downD == 0)
; k) w, a! Q6 ~4 [$ g8 U - {/ Z( z( N% x+ H9 J) C$ ~- n/ q
- return 99;
' f9 M' ~5 b8 @) m# d5 u7 K9 |2 a9 A - }1 [; |1 e: h h$ O' R! ~' V
- else
+ T* q& W: m7 R# t3 h; f - {
3 V3 J7 R7 M* r1 {/ Z. A3 R1 p - return upD/downD;
7 U, r, [0 Y1 y/ i* P, v& s# w - }: e) I) X: M/ u9 D: b0 M1 R! r
- }& A! J( [. Y3 _6 [& j( e# t
$ _ s* M" M$ N! u# J) h, j- void moto(int Speed_L,int Speed_R)# a# q* u- R: |$ G
- {( N5 Z$ T2 x! p0 l% }, r
- Encoder_2.setMotorPwm(Speed_L);
: j4 e1 }4 O& `4 M n! Y - Encoder_1.setMotorPwm(-Speed_R);
, J+ l) _; B2 y6 F6 B1 A' E0 N - }
複製代碼 : L* G8 T- ]/ @% o6 q6 `
MeLineFollowerArray.cpp0 q& n. _8 @; V
- #include "MeLineFollowerArray.h"
0 {4 i' l4 ], R T9 W: M) Y - 5 E. b/ ^4 h! ?
- #ifdef ME_PORT_DEFINED
2 u9 l! |, z) H- E - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
2 B# r& a, e+ |6 ?5 G - {
+ ]9 C6 K* f) H1 Y3 z6 e4 @
) V+ E! ~6 z$ m# C7 R+ I# |0 D t- }
/ _8 g, m( D# z9 D3 u1 g - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
5 h& e% _ \& `7 A; G3 t6 k+ w - {5 d) a% r9 p8 V
- _DataPin = mePort[port].s2;
$ @1 I6 R0 ]/ y. e0 `. P& E - pinMode(_DataPin, OUTPUT);
7 n6 M- L. G' d& U0 { - digitalWrite(_DataPin, HIGH);
4 M2 o9 y* L5 M$ g8 {. a; Q - }
4 |) k% k) ^$ l# C1 v' ]" G - #else // ME_PORT_DEFINED8 O' h: @" l6 O' U" Y; b
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)( `6 E7 d: _& c, Y! O
- {& d/ N7 Y, \( ?- @9 g
- _DataPin = pin;
) K) t' q, ^8 `6 _) G) O% p5 _ - pinMode(_DataPin, OUTPUT); . r$ \9 m0 i* G. ~
- digitalWrite(_DataPin, HIGH);" V( F$ E' Q {0 Z, {
- }
% ~7 G- @3 O9 W6 y - #endif // ME_PORT_DEFINED6 F. j( v, A! l
- 8 Z% F( E9 i: y- V8 A
( q* y/ U/ k/ w( z: @- void MeLineFollowerArray::setpin(uint8_t pin)( e" ~7 `8 I3 ~) B0 f( @' u$ n
- {) q- Z- f3 I& ?
- _DataPin = pin;
" G7 n1 f8 g9 g0 g6 S, [ - pinMode(_DataPin, OUTPUT);
7 q0 a2 z; g) J5 j9 G# V7 ?/ A - digitalWrite(_DataPin, HIGH);) P; [! i4 @3 N
5 `' T; M2 w1 H/ _0 t- #ifdef ME_PORT_DEFINED( Z, }$ ?. {. \) J9 U2 l3 ~ p
- s2 = pin;
. I( o9 v: J: A/ Y8 R! v - #endif3 t7 e$ F7 X" }, o7 V0 {
- }
+ @$ R. Z8 F. k0 @ ]
4 o! c2 S+ a0 j9 c+ Z- uint8_t MeLineFollowerArray::getValue()
J: v6 e( w2 K$ j# I - {
2 X. ~8 e7 |% X6 T - uint32_t LOW_level_read_time;( y! R; T5 D4 M8 P9 c# V
- uint32_t HIGH_level_read_time;
- r1 ]2 M- S( J! @ - uint32_t time_out_flag;& D# K8 @" @* e4 k% z) \ l
- uint8_t Sensor_Data[3];
' C: K: d+ q2 \& k/ G4 H - static uint8_t old_data = 0xff;
) d7 S: i3 @; H. N- g/ H
2 c8 q8 t9 x2 Q6 w- pinMode(_DataPin, OUTPUT);4 S" g0 F4 y M+ o F" W
- digitalWrite(_DataPin, LOW);7 F7 `/ o' j" Z9 \
- delayMicroseconds(980);
7 X$ ~ t. I5 M - digitalWrite(_DataPin, HIGH);
: D- A, u4 t5 i# b* W7 ` - Q* t1 ]% T, [: T, U4 H
- pinMode(_DataPin, INPUT_PULLUP);3 w7 v7 {5 |; s0 ?1 a1 I& _
- delayMicroseconds(10);4 H0 [" g& b% k2 \9 ]: E
- 7 e+ S$ S( j$ }" E0 {
- time_out_flag = millis();
8 X- a' P6 u) @$ ^$ z) w* `# \0 z - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
7 i) j) e0 [% A2 o - + n2 A5 u1 g, e, n1 _
- LOW_level_read_time = micros();( w6 J/ T/ O3 ?9 Y
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
, z9 A% T" t6 T$ L - {9 q: G! z i5 t; Y! M
- return 0xff;' ~" s% T- k( }0 q- A" h
- }) {4 X9 U# h" D0 s/ k
6 z3 u/ @& l N, k- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
2 F0 u6 ?0 F4 y7 L7 _
" w% \3 |: z5 x9 v: U/ x1 i% M, M: B- HIGH_level_read_time = micros();
9 J; i6 I! l r" j; U: X) I - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
' q1 {5 c2 {5 \& S1 v. F - 2 g) [ { C. J2 p
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out j$ d Q1 O9 T* K
- {" c( G* ~/ {/ e) S/ u
- return 0xff;2 O5 ^8 c* A- O" f
- }' ^5 t$ I% h8 R+ w. a" D6 M
) x8 L4 q" F1 Q7 A5 n( g+ O- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
8 |6 Z! Z8 ~' M" L% a; z9 f - {
# F( l2 D3 Q- C+ w* x - return 0xff;
2 w- m# j$ N" P- V - }
8 C. Y5 I% ~1 u4 I# X' h+ n) E2 L - 5 D3 \7 i/ s/ t' X
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
; z' e" f' s# | - LOW_level_read_time = micros();5 |1 s% b0 v* X* b" h* U8 c9 e
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
' K0 l* q8 l$ a8 @
/ B) X0 g* I/ q: y6 H( @' `* M& p, R2 _- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out" ^; }# y9 ?( Z' w$ c T
- {) e2 l% d7 j# O5 U+ ~7 L9 k# H9 H
- return 0xff;
7 f% \5 n3 h; Q( _0 n$ W8 O - }% U/ u+ A4 V6 n5 Y
. |3 C; h1 o6 }" K- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))) ]5 M( Q" P% l3 x5 X ?
- {
" T" O, a8 G H7 y3 N. h' i - return 0xff;
" J6 l3 {9 H; t5 r4 ^' | - }
2 w8 p- _+ F1 M1 P' U - 0 f; @ \1 Z) B7 ~! R7 s
- for(uint8_t k=0; k<3; k++)$ k# H& @+ Z M7 m/ q6 N
- {" k/ e4 g3 g0 H( N
- Sensor_Data[k] = 0x00;* x2 f4 |( V8 k" X& J" t8 E
- ! a( V/ E2 I6 S5 Z) e
- for(uint8_t i=0;i<8;i++)
2 F0 R# M& d4 D - {
8 [) ]1 G$ h+ R6 X' X - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
4 M0 g" Z( x" ?6 ] ^ - HIGH_level_read_time = micros();
% ]9 {2 O$ L# l* Q5 D1 O$ Y - LOW_level_read_time = micros() - LOW_level_read_time;
- v* h9 ^* `, Z- f0 j" R
; I3 z: Q% c- {6 h9 S1 j/ O+ Q c- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ c3 F. Q0 A8 l( H
- {7 w: p: b. q W0 C y$ \) C
- return 0xff;
" w3 @; M) ?) R! ? - }+ \3 a t! e1 V& V
- : r) L8 E9 X f( C
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
7 I, G& {& V/ W6 \ - LOW_level_read_time = micros();
( e0 W. ^: b# y - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
4 t" t& o) s9 J: A/ G- |8 Y - $ U. f4 v7 Y) j t
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1/ i9 E) |) L- b& X# l4 h7 w
- {! {1 w5 |3 u" j; g! @
- Sensor_Data[k] |= (0x80 >> i);
, _ O1 H( m& r7 n - }. I7 s/ E4 t) a+ O
- else if(HIGH_level_read_time >= 100)
. n5 _9 q \4 o" H: Z- F. s - {
9 Z* u2 a# ]) M& ` - return 0xff;' M( g: v, e% Q& Q. W5 h
- }+ y2 s1 X! [2 U3 Q3 J9 w/ X, X7 v
- ( Z. m& Q! }7 v; x! g
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
7 c* X" O: R) B! H r; }3 }' G - {3 @: \& i6 N. F& v3 z8 Q$ T
- return 0xff;
6 R) Q9 [# U- ]7 R0 Y/ \2 |" a: U - }
5 V9 C0 D$ X5 }2 c - }: `. {% l# q, N' j, Y+ l
- }3 L* R& _5 d' c$ ]
* k, t1 G. ` t' V2 ?6 \ a- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
0 h- d: W, G B0 ]; `: n# u - HIGH_level_read_time = micros();
/ k( P6 y) [3 Y _0 ]7 X8 C - LOW_level_read_time = micros() - LOW_level_read_time; W- r9 F. h2 ?2 u' r7 B7 e$ {! S
- ' W( U9 I6 |1 _' b& ]0 S8 {' f
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
g& p# c- C& f N- w$ J) Y4 p1 d - {
5 [6 A1 Y( g' L2 o- i+ C$ | - return 0xff;$ K- I3 k+ A* _0 I
- }
/ L5 l e" V1 @$ t8 S" m
% G3 I: j. m; B3 m- pinMode(_DataPin, OUTPUT);
. k' q4 s) \- g, l" I - digitalWrite(_DataPin, HIGH);! [$ j# |- o3 p- j
& ?6 |1 [8 Q: z$ D: {+ d- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))$ I% G) s0 P. R7 N$ ?7 ]8 V S
- {
! O- Y0 s* i) @8 T! E8 s7 d - old_data = Sensor_Data[0];
* _; ^2 `6 J$ ^4 T% O _ - return Sensor_Data[0];: W# N8 \: `# A/ z( }4 n5 j3 C9 L( y
- }
& g" d: W6 Q! p$ o. G q- t - else) l5 n9 t4 C) h
- {
/ ^ Q- J" j3 u( n& \# w* v( o& V' Z - return old_data;& V) R5 ` |% l& G
- }* J0 J5 j! g. ^, G, b( A/ m5 S
- }( T, G# Q. m; w Q9 }- }
複製代碼
! ~9 W1 i, R; XMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
4 A. z( b9 d; y4 v
" w: K& b0 f6 q' }+ N! n |
|