|
|
2 [4 g- K! i4 ^% |+ o
- #include <Arduino.h>
$ ?! L8 F8 J q3 } - #include <MeAuriga.h>4 ?2 `- K5 e% g; x) Z7 s
- #include "MeLineFollowerArray.h", N) I0 s; L: a: P
- + D- \2 b9 I7 `" i
- MeEncoderOnBoard Encoder_1(SLOT1);7 a* l- e! @1 D% r
- MeEncoderOnBoard Encoder_2(SLOT2);7 X0 [9 j* m2 m1 h/ h# _
- MeLightSensor lightsensor_1(12);
8 C! k# b2 @- ~" A% G2 l - MeLightSensor lightsensor_2(11);
P) }# A" X4 ~! {8 y3 ~3 m - MeBuzzer buzzer;
/ n; ?$ R; l! i* X: L" B+ w - MeLineFollowerArray linefollower(PORT_6);
6 B3 M' [' I: ~. l+ [7 g4 w
: B7 j2 L7 Y/ U8 [! ~- #define Error1 1/ E" u9 Q: r4 o+ K4 m' S6 w
- #define Error2 2
8 v; o: F( w/ K* t" M - #define Error3 3
# i O$ M+ J2 x( b" O$ Y7 T) [
0 B E: u. P4 T$ ~8 H5 D2 c4 H. W- #define Kp 15
+ t9 ~# ]+ n' d - #define Ki 0.15
3 y# ~- Q7 x# z8 V1 A+ }: s - #define Kd 0.035 ]% f; W* v `5 L) k. g
- * X. r( }/ Z1 |; a$ g Q: Y+ f# O
- uint8_t sData;
# ~ t! v& S$ M" X9 N: Q5 J4 w9 t - uint8_t D1;- k1 T2 L2 Z" R$ X0 a/ J
- uint8_t D2;# ^ r% Y8 i# o0 b8 [
- uint8_t D3;
# f( E: O# y* s% J% Z - uint8_t D4;/ p8 {9 l& N, X& J8 Y0 B& P' Q4 t! c
- uint8_t D5;
. G, S3 V9 F& M/ T - uint8_t D6;
2 G, ^& q G5 t u - ) D4 S' N3 T* y3 |+ q' T: \ R
- float previous_error = 0;
* b3 ^) ]& B* k. B" H: t+ k - float integral = 0;7 h6 M4 \' r2 }" g
- float derivative = 0;& s3 \5 m+ R! |5 V
- int Speed = 160;0 m5 h( t. b" i( Y
- float output;
+ D& d7 F- d4 a) m
- M# @) A% u$ c, H0 F5 t A, X- byte Left;7 D) `1 R7 q) H' v
- 3 X; d; I# B& y
- void setup()
2 g" y( v# r! v7 R' f - {
5 g+ B: n; W- b2 m/ S: p - //Set PWM 8KHz7 ?, {5 y0 Y2 @: ?) N* s5 y' O& C
- TCCR1A = _BV(WGM10);
$ g9 Y+ q6 m/ O$ p - TCCR1B = _BV(CS11) | _BV(WGM12);. l- y* S4 n- | k$ F( }' \: N
- TCCR2A = _BV(WGM21) | _BV(WGM20);
1 C& P8 r W% D$ g- w" U$ f - TCCR2B = _BV(CS21);
) ?* O6 d" g7 v- s c( V - Serial.begin(9600);: E7 @9 b! I M1 |! p2 v- Q3 @
- buzzer.setpin(45);
0 u# z/ y5 p: ?1 { - }5 x5 b" r. m" G' U2 T: ~: r
& S4 w( \4 i5 O- void loop()
+ j# C) f0 X6 f; \! O% w$ x - {
% T4 L3 H t0 m0 `+ J' E2 s - while(!((lightsensor_1.read()) < (10)));
$ V2 c( \0 Z* m( q - buzzer.tone(1047, 500);
" z; _* w# ~7 ~( C; L$ i+ A - delay(500);
& S/ n, y2 k8 ]( ~4 B: ^0 I0 Q( S - do
% O. O8 ]8 L( c7 s+ |4 Y& b6 h - {
) H5 a" K) l% B! C0 Y0 |: G - int Err = getErr();" c. q( {) {8 B F
- if(D1 == 1)
+ }. q8 E* U0 s( T - {4 F" i2 m5 D$ R- g; U0 e2 h
- Left = 1;
# y6 T: a. B$ i5 G9 y* ]3 S - }& b/ C! S6 I. I( T3 m% C, k
- if(D6 == 1)
) g6 ^! ?: B1 [; D/ Y; i" } - {+ t- n& d. f" m7 w/ \3 Z0 [
- Left = 0;
: j4 n* H7 H2 c) j9 J0 n8 a4 @ - }) [0 Z; P3 r. `0 W
- if(Err == 99)% J) z( a% u' N# s# Y6 L/ O
- {
0 M% n. r z& \' ?; Q8 n - if(Left == 1)
" v5 C% a! B# ]! O9 M0 Y8 Y J. w' ` - {, x l" r" W( ^3 Z1 H; o
- Speed -= 5;3 p8 Y N4 b; g! Z1 W( `
- moto(0,Speed);
( p. S. @) T4 X2 `- {# Y - do4 l8 k6 ?6 k% y7 N. }8 W7 ?5 d
- {
: [! C, _7 d. E. t& `+ Q: p: x c - Err = getErr();
; f+ U) N# Q# f0 C$ l8 e - }while((D1+D6) == 0);" v. n& x7 F9 |6 X
- }/ [* b+ ^' ~) m4 j, F
- else% E b% B* Y. _9 k: |
- {5 ~( p/ |7 [0 O9 b" z& P
- Speed -= 5;! Z7 [3 {1 e6 y3 `" J3 d
- moto(Speed,0); ' P e2 z- S( f& Z" S* z) m
- do/ G+ I3 J. l2 S5 z! g- f+ P; {5 P
- {
* q3 s& m2 p& t& Y3 B+ T - Err = getErr();
: f& S$ L+ L' T+ w1 Q) ?0 c - }while((D1+D6) == 0);4 ]. W' m# Z" _8 j0 [* T
- }8 _: m9 C6 k% Q$ l5 ?4 H t
- }
& B" u3 o; d0 V2 w% b) I5 a - else
/ M/ B' q- `0 E2 P, c - {
8 e3 X" f& i# b; ~ - if((Speed < 160) & (Err < 2)) Speed+=1;
" J: u+ x6 Z7 b4 h. |* [+ y0 R - if((Speed > 100) & (Err > 2)) Speed-=2; 7 _3 h' |, d ~7 d/ _5 a
- integral = integral + Err;
1 i- {; n# `" b) V% K5 @1 l$ k - derivative = Err - previous_error;
7 q, d0 Y3 E" a3 s Z: c - output = Kp*Err + Ki*integral + Kd*derivative;
$ y) {4 R! |1 u) E. l - moto(int(Speed-output),int(Speed+output));
% H( F! o x' }# }4 k - previous_error = Err;& @" B' w; S: U. h' A* y
- }
& }* \& `4 O2 s- k0 ^# A - }while(!((lightsensor_2.read()) < (10)));
, l ~- K) Y, p/ v; v( j0 h# L - moto(0,0);
; F5 L& F3 Z9 B' \; j - delay(500);
7 c7 t, Q+ p' B - buzzer.tone(262, 500);: j7 J* ~1 C r7 v
- }! e W0 ]4 a F3 H/ {. c; K; m9 ]
- * G" Z a8 U& I6 h
- int getErr()
: I7 L% [' p: M5 _ - { % s, m H) Y0 x; z: s
- sData = linefollower.getValue();( ?4 b$ L; k. i( `
- D1 = ~(sData>>0)&1;
9 Y- Q0 ]* T; V1 K" x% o3 y: z2 K# I - D2 = ~(sData>>1)&1;
9 B' L' j" n: W7 I3 v0 I, ~ - D3 = ~(sData>>2)&1;
! I7 E4 d* o3 s$ Z - D4 = ~(sData>>3)&1;
8 f" S. @1 R: x4 m! g2 c - D5 = ~(sData>>4)&1;4 M9 n# |& u% C, {2 m# L
- D6 = ~(sData>>5)&1;2 L4 O& Y) n, i7 X! M! k
- int downD = D1+D2+D3+D4+D5+D6;
9 a. b, Q/ ~* H* X8 d - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);/ r4 O- ~ w m. \6 z/ N
- if(downD == 0)
# ^! u7 q' c$ q - {8 r3 S$ t. {# _0 G, Z$ {7 V6 K
- return 99;
6 ?- P% z$ [) Z, [2 C - }
. T- v, h% D( J/ M2 v0 |" V# q - else
2 G( L# O- ^/ ]1 h7 N% S - {0 Q9 a$ J; T$ w2 _
- return upD/downD;' l7 z2 N/ u, ^: I( B/ i2 q! s
- }
: m$ p' T' ^' m2 k% T - }
3 E# E; ~" U8 N1 D3 N: t
& L! ^; [% V+ `, X- void moto(int Speed_L,int Speed_R)
/ Y# y; ?4 C. O4 F8 S5 y+ ~. C; [ - {
4 [+ q! @" m, e4 U, g - Encoder_2.setMotorPwm(Speed_L);+ V2 l3 ?5 v. z$ |
- Encoder_1.setMotorPwm(-Speed_R);
l" a8 \: c5 h7 a3 T" x& R - }
複製代碼 . ~, Q% G" g9 d$ [2 `- C
MeLineFollowerArray.cpp5 E/ ]/ h. L0 E& Q0 c
- #include "MeLineFollowerArray.h"
2 n8 A% S p1 x+ a1 W1 c. j - 0 R- Y8 G! d8 E+ L( @" j/ S4 e
- #ifdef ME_PORT_DEFINED& u. F& L5 u3 R
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
+ z8 T% a4 Y- H k0 U. N4 x& F - {- S3 ^; K1 i0 Q( w' r
W/ }; A' ?( @( W: h0 x. T4 B- }/ u$ m5 T5 i5 V, F& @, S# k
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
8 C1 K! V/ N# F$ D4 @2 u - {
, m6 x7 E/ N5 x - _DataPin = mePort[port].s2;' \$ g- |' m. w0 P
- pinMode(_DataPin, OUTPUT);
3 ]9 ~) n; ]! M - digitalWrite(_DataPin, HIGH);( B+ x$ `" n4 l" `/ |
- }& y: j# l8 K; b9 C4 \8 a5 ^
- #else // ME_PORT_DEFINED6 \; D# i* u1 n* o$ N/ U, Q6 N
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
* b" I7 Z0 h* \: Z+ i - {3 I3 T4 U7 v0 U% f
- _DataPin = pin;
4 o/ t0 c% i1 b2 T2 a - pinMode(_DataPin, OUTPUT);
8 X+ w* e5 o* o) F7 G4 j2 H5 D - digitalWrite(_DataPin, HIGH);
: l% A: @- @' i6 h4 r - }! y) `3 O, v) Q8 X
- #endif // ME_PORT_DEFINED
3 `" m6 a' \6 F1 u* P& d
2 S9 k' i8 q5 j- ) d: V$ Z! u! t1 K) n/ ]! @) ]- |# o8 h
- void MeLineFollowerArray::setpin(uint8_t pin)
( F4 N' D B- y1 D+ Q+ O: l - {! g; P# B, z$ U9 I/ ?; D: V9 N% y) t. X
- _DataPin = pin;
; p& V" I7 h0 M( c1 F - pinMode(_DataPin, OUTPUT); 5 n1 z+ X5 F* h+ @, G" r
- digitalWrite(_DataPin, HIGH);+ h Q* Q' f8 F1 F
- , V. w5 A4 ^ n# `* j/ K. o, @
- #ifdef ME_PORT_DEFINED
5 n$ v$ y% m, A6 Q - s2 = pin;4 E: [5 ]7 U6 M% v! J
- #endif4 q3 i3 o: S% Q( _$ ^
- }
, u* a5 Q8 e t4 q8 x - 8 d" F1 P% u7 ?7 q8 h7 ^5 g
- uint8_t MeLineFollowerArray::getValue()
0 @2 z$ X% z2 b/ Q* ^3 S& X - {8 Y1 s! m6 g* ?
- uint32_t LOW_level_read_time;
4 A3 O. x: @6 n) E - uint32_t HIGH_level_read_time;
2 U- B' a" j ^ Q9 ~6 v6 _ - uint32_t time_out_flag;! f7 D0 s* f# e; l/ m( s4 U
- uint8_t Sensor_Data[3];
4 _4 u$ w8 K% ?7 I& m2 ` - static uint8_t old_data = 0xff;
m6 @1 J( P. @+ o5 A, I
0 t" b4 c8 ]2 _" Q' f- pinMode(_DataPin, OUTPUT);
& i6 m- k$ q Q1 j, X6 Q - digitalWrite(_DataPin, LOW);9 s& U# K1 \* `1 |
- delayMicroseconds(980);
( y" r m& o3 Z% h - digitalWrite(_DataPin, HIGH);
# z+ ?1 d* [7 {9 K) e5 ^ - 1 G5 a- l0 K; R2 C q2 D& |0 j5 G" b
- pinMode(_DataPin, INPUT_PULLUP);
' l1 A! P9 Z( Y5 V, w \ - delayMicroseconds(10);
7 J) m! l& u$ J - / u E6 V1 L3 b' i$ y, A+ N# w
- time_out_flag = millis();
/ l9 h. Z8 e' g# W. s0 N8 o, P - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); Y+ K0 v9 g) ^
- & Q& n) y# p6 [/ o# W' B
- LOW_level_read_time = micros();
( L3 G1 s$ s# H3 f) U: M - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
; l; {* M; b# ~" E, T$ P - {
7 B& A7 x" F$ B% m# Q- i, z - return 0xff;( v1 X. @1 w% w0 r/ r9 O: Q
- }
( K! ]+ m; b0 Y Q; g1 K) @- q9 { d
# F: U# k" `; t8 ?0 u$ s- _7 }# r- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
+ Q! o. N' {0 N! s8 d2 D2 |+ c - * B& R, K6 N5 H
- HIGH_level_read_time = micros();
9 D. ~& L& H9 c - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
: U- W/ V9 v0 ` - 7 e: l" U* K. c& D
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out$ z* q. Q& i% w0 H( ?
- {+ r: U6 G9 K. l) s
- return 0xff;
% `# G0 J" z- a7 i8 q - }
. e; o( W, n* j% e. L j1 N
& O1 S9 j8 y2 L% e- I: |0 g- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110)), Q) Y7 b3 x7 K- C4 s3 d* D! v
- {
* }1 b( f! e% {$ _) K - return 0xff;( C$ w) u$ k" s! m
- }
% v; H: B Z, J" X6 D6 u0 _ - $ d+ L/ p+ z, ]& ~+ K. t1 ~
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); M( x6 Z2 f6 [+ r ~9 N) {
- LOW_level_read_time = micros();7 q6 ~; b! T- ?2 \; b
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level+ J% J9 _4 a. o/ T& }
- 4 F, y) b" y9 u* a0 o
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
1 M. w$ c. H# T! _3 O - {
+ ]0 f( r* L9 Z; c - return 0xff;6 ]( j+ H7 [, m4 |( s2 {1 q/ D
- }3 L$ P0 T: b/ w8 T$ d5 c
- 5 y8 S: X1 l$ D2 i$ c/ l
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))' O% R; d5 n2 }6 w6 e' h- i5 M
- {
6 k( M8 q2 v: } r. z - return 0xff;
" l& a$ c0 X' n! K3 Z. B& v - }
# `9 E+ Y7 q h
3 ~" j( Q4 ?3 K; O2 A- for(uint8_t k=0; k<3; k++)
2 M; s) }5 }) J* T - {+ a) l S% }; f3 K8 D
- Sensor_Data[k] = 0x00;
3 v6 c! c1 f8 t/ m" P4 O
0 q1 l2 `4 C `4 h3 l4 A- for(uint8_t i=0;i<8;i++)5 q4 n1 c9 j# G+ ]3 ^
- {
' u t+ B* i9 L1 n. j5 l/ q - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
9 P9 @9 C. a1 n6 s$ b( Z& ^4 i - HIGH_level_read_time = micros();
+ O" Q2 [6 v' n6 I - LOW_level_read_time = micros() - LOW_level_read_time;
' P! Z+ i3 y) q - 7 g6 m6 K7 O7 w) t% m
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
! h) G3 t7 o6 i9 v! ?0 A - {; b/ D4 Q# {9 P+ ~
- return 0xff;
8 k8 j- d5 ?* p* Y+ m/ f @+ y, Q - }
& K/ R/ P( o+ L/ { - 0 K* w. T, c$ M- V _$ O% K
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ L. F! t# R2 r8 d - LOW_level_read_time = micros();
, u$ F6 B/ `% j - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level6 F: z2 x( A. z( q! C, T" d
0 i, @5 p% [+ Z3 b a1 J- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 14 Z& m$ e; v: n3 {
- {
% X8 t( U& A' C1 r+ i( P* w' B9 f - Sensor_Data[k] |= (0x80 >> i);
( N! j% y$ `5 W2 d' Q1 R/ j& z - }
. m: E- S# j: ~0 P - else if(HIGH_level_read_time >= 100)
5 o' o9 _4 e% Q [$ u* R1 U - {* b: r& i' `# v# m4 E# D$ b
- return 0xff;
) b7 i. F( U8 D9 J1 E - }$ H! S; R. d7 N) u% `
- " B5 b8 U7 K+ a. {7 P* F
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
' s3 t, w: Y- a1 {$ j8 ^4 V - {
2 E' a c' y4 {! ], X# l z - return 0xff;
2 c/ W( L( a; a" ^8 t' f - }
: Q* ~; s* d. ]8 m$ Y$ _% r8 @/ s - }3 b" t( z& w; {; D2 T* v- Z2 G
- }
: [7 ^- w# B- R0 Z" ]' ]. H6 i
6 |- y2 x) Q! G8 Y/ L8 c, \! ]- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level4 n; t0 K( r. Y
- HIGH_level_read_time = micros();
2 M4 ]: V1 E0 U* C, V9 i& t - LOW_level_read_time = micros() - LOW_level_read_time;
3 K; a1 O+ o: d8 ^
8 K* W: G3 y. E; ~+ d; k; S- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
/ G7 D6 A! O6 p$ L8 X" O( F1 z - {3 z7 a7 Y0 y0 W6 R( H
- return 0xff;
) ^/ l2 z+ r; C0 U: r9 {3 `3 Y7 ~2 a - }
. m1 d, t& ?* t6 I - % L$ d, N/ n4 @0 }
- pinMode(_DataPin, OUTPUT);
8 a3 J6 `1 f5 E - digitalWrite(_DataPin, HIGH);
* R4 Z/ h4 m6 d0 x2 A! {$ `. _8 [7 \; G - 6 ?0 a% w% ^" ^' \4 O$ x
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
6 J. K9 H0 V* v6 g/ D: k - { l* E+ t7 }' ^* K* K; @1 q
- old_data = Sensor_Data[0];
0 W) Y$ Q- p& l& P7 J8 S - return Sensor_Data[0];
; p" ]* J5 _! h. w/ M/ ? - }
0 U7 `( w+ K. K4 s4 y8 h. r - else
$ G1 c; N3 Q& X1 O2 g5 I5 K. g - {
8 u4 p9 @. G' |+ W' ]. U - return old_data;
. X3 \9 f6 e* m1 i5 V1 O/ M - }# F+ R% V* X0 b, H- P+ Z! g
- }
; n) `+ c2 M9 O9 i$ V6 }7 L# L- A
複製代碼
. R* X, Y- B1 ~1 Y7 e% pMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
- G1 }8 H+ u, b
- g% {' J) K6 ~/ t% K1 D h. n
|
|