|
|
& T" @# _# g9 J+ B }, M
- #include <Arduino.h>9 B q: m g; H+ ]
- #include <MeAuriga.h>
6 Y7 c3 @+ d, ]: Z - #include "MeLineFollowerArray.h" H. X( t; W2 O- a
- 6 g3 L! F& X5 v& ?; @
- MeEncoderOnBoard Encoder_1(SLOT1);
' Q$ s W/ b. X4 f. ~; _& f; _ - MeEncoderOnBoard Encoder_2(SLOT2);9 H* c2 m5 o# B! W
- MeLightSensor lightsensor_1(12);
6 I5 K$ ` M9 y2 W8 [+ w - MeLightSensor lightsensor_2(11);+ x/ b; @9 z% E( s' z4 {) b2 n5 v
- MeBuzzer buzzer;4 ^* h% ]4 c% A: d+ B+ q# j6 @
- MeLineFollowerArray linefollower(PORT_6);
$ b8 U6 A5 u+ ^$ A O! z
. C j2 i2 i* j! v# G& K% H- #define Error1 1$ _' v. u/ X8 \
- #define Error2 2
; a: H' P) n9 q4 f! e4 V- c0 @9 S - #define Error3 3
7 F& k) @5 }, Q$ w0 m6 a - 3 c9 V& o1 X5 y/ h4 `0 q
- #define Kp 15" ]$ p( G, l8 L( o: C7 g1 {- f. o
- #define Ki 0.15* r- S: g: R' b$ h5 c7 f
- #define Kd 0.03
! k3 v/ z+ @! S t9 s* m. ~5 J* K& b
$ {5 W% W& {0 J9 B; D* \- uint8_t sData;. d$ a4 E1 }5 P
- uint8_t D1;
0 t. W' O& a1 N( z! z1 E2 f# ~ - uint8_t D2;/ `* x4 C! p m; n. I7 Y
- uint8_t D3;2 H: _0 m7 `2 [7 n
- uint8_t D4;
! a) \ k5 l2 l4 r& \2 I% C: h - uint8_t D5;
1 L3 Z5 n+ G, A6 _3 i9 w - uint8_t D6;& U1 [. W1 P# Q/ B
- : M8 c; d$ u9 k* e; N
- float previous_error = 0;6 ^6 x8 k" }/ B y- S# r: t
- float integral = 0;# v& l$ f" G0 p! `; Q# g
- float derivative = 0;1 b- Z' q/ C% l6 L! S
- int Speed = 160;4 S% Q" F+ w: y/ e
- float output;
( m% w8 ?# e' I1 s" T( p
5 R. T, q- A' q0 g/ H& G- byte Left;# V0 W. e4 i9 f* n2 v/ F: h/ l
+ m3 A) J! r# c' c: w- void setup()
2 m2 l: `# T' ~/ } - {8 V' T& E" F! y$ D
- //Set PWM 8KHz
" O, ~* ]/ ^- Y! t6 ]# W - TCCR1A = _BV(WGM10);
, G3 Z7 {, ]+ [7 b8 y ^) } - TCCR1B = _BV(CS11) | _BV(WGM12);
, j% W% @+ ?* Z" C& H L$ H - TCCR2A = _BV(WGM21) | _BV(WGM20);2 }% w, p5 |6 s) ~+ W/ k
- TCCR2B = _BV(CS21);
3 s! I- h. h# N& J- g - Serial.begin(9600);/ V0 x- j9 |: A/ I. s) ~
- buzzer.setpin(45);9 |' i+ K5 X" y r
- }
( H' v1 I* j ^ y- {2 }' U+ E
+ {1 p3 s8 L3 q- void loop()
[+ c) e( s$ \/ {; N - {
( s( F1 M2 D. W; D& r% Y - while(!((lightsensor_1.read()) < (10)));
& z* k' N0 N5 }0 v& U1 Y2 F - buzzer.tone(1047, 500);- }3 s; e& N# L: m. Q: b1 P/ V! x
- delay(500);
! s! O- l& Z. o5 ^% {, x/ b" e3 P - do7 M) c/ V4 E( a
- {( Y5 N* m& p+ `$ a* {
- int Err = getErr();
- f# Z) W. C' k! w+ c5 {( L - if(D1 == 1)8 o$ y# v' E* h1 `: K" C9 W
- {
2 _# N; X# {1 R* b: R$ i - Left = 1;' Z* ^% P1 C- h+ ~
- }
5 w" x2 T6 e6 P$ B. p: ?( i( o - if(D6 == 1)
+ x- i: ^: d m. a7 [' j! A - {& o1 H. i- o# f! ~1 K: C3 x6 q& a% R
- Left = 0;
) Q- u, H; y; W' G - }
& M* S& x2 V {) z4 G Q) C - if(Err == 99)
% z0 D8 w, Y0 V - {
+ o) W4 h/ z0 H( ~2 B - if(Left == 1)
/ e; \/ J0 G) d- p8 e. i - {
" C- L* K6 ?# R9 v: m/ B - Speed -= 5;
A, ~8 ]9 c$ R. J# i- M9 k - moto(0,Speed);
* _" W g5 _# o' q# w: D7 y - do8 O6 P+ G V6 ^1 y+ M
- {
5 \: \' J0 V0 e - Err = getErr();
$ w* }; b9 }, U. r, E - }while((D1+D6) == 0);1 f; N3 [8 J1 g+ K
- }! Z- f3 x- q: m! R6 a" Y2 \9 t% t
- else
4 }/ B1 k5 I" u2 I8 @ - {
6 M! I3 }; Z, C8 ?" r - Speed -= 5;+ v" ~/ V) E- W! F& `
- moto(Speed,0);
* Z' C+ _3 K! |1 W/ P - do
7 T/ h! {4 i$ u, |7 X2 s4 i - {
& `/ U3 w2 x4 s' C# m ` - Err = getErr();
+ k; b- }+ Q9 c - }while((D1+D6) == 0);
# r5 g: H# E& z% u, d% U - }( r8 F7 k' G3 Q
- }
7 r& g8 D- u3 I - else
) V2 f8 r) ^3 S2 z7 t! p% p2 S - {
r$ u. W s3 S7 u3 B) t - if((Speed < 160) & (Err < 2)) Speed+=1; ; [. [1 G8 i6 O( u0 z. _5 l
- if((Speed > 100) & (Err > 2)) Speed-=2; * o7 E* j$ a$ z2 v
- integral = integral + Err;: A5 a8 g" ?) r
- derivative = Err - previous_error;
4 x9 w9 t, ?' f& W8 V* ? - output = Kp*Err + Ki*integral + Kd*derivative;
b( {$ g4 m6 V8 q4 z4 n - moto(int(Speed-output),int(Speed+output));+ s) t$ V- Q E2 f6 [
- previous_error = Err;
- g+ w2 Y3 f2 K) A% D! E( W* B - }4 }# m1 J8 ~4 w- a& c
- }while(!((lightsensor_2.read()) < (10)));+ z8 }. {5 [& q9 K! e6 I; Z5 a3 c! ^
- moto(0,0);/ [3 @, a/ X; k( t# `2 @! H, A
- delay(500);; u5 z) M$ D. n8 N. k5 j" W
- buzzer.tone(262, 500);
5 U2 n" s& a! W; J/ Q$ Y5 P - }
" s f# A: ~; T R/ C8 o% J- i/ v* H1 p
2 q1 W/ o7 D. ~4 Y- int getErr()
$ m+ H: m. Z% t: M - {
4 z) M' G' o7 D x! u - sData = linefollower.getValue();
. j$ S; D6 P6 U, T8 ` - D1 = ~(sData>>0)&1; i& ^* ^- e+ u O6 {2 ]
- D2 = ~(sData>>1)&1;
: K& Z p% D2 H6 C. a - D3 = ~(sData>>2)&1;
6 u% a# w8 z* W* i9 u - D4 = ~(sData>>3)&1;3 d# r/ l1 I( K$ h" j
- D5 = ~(sData>>4)&1;4 I3 `% z3 I9 Q6 I6 Q R
- D6 = ~(sData>>5)&1;- x0 @1 o, S6 ^; D1 r) P4 ?
- int downD = D1+D2+D3+D4+D5+D6;
- y. g" R# S5 c* C: a4 c" j - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
2 @. O3 `& ^5 M- O - if(downD == 0)2 g+ Y6 d! V) ~" c+ h* p0 V
- {( d4 l5 K6 C5 z+ X( `
- return 99;
2 N& G- L3 n0 p+ }2 Y- q' R - }
+ Y$ A. f9 _2 |4 G. l- f/ S/ u3 l - else8 G* [3 L/ U) u! i
- {3 P$ h8 a' W* }( d
- return upD/downD;
8 W8 `. f5 ~6 @5 @ - }& r9 w. O( {( b
- }
( F4 D- l7 a/ c% s+ N0 W. k
, c9 t; I; f) x8 @! w- void moto(int Speed_L,int Speed_R)
# F- s( g" L$ Y3 }, z7 ^ - {
$ N$ z: c8 ?. G: Y$ o( }5 {! R - Encoder_2.setMotorPwm(Speed_L);
6 p7 U; }5 I L. d3 t; y# K3 a- F - Encoder_1.setMotorPwm(-Speed_R);
V( j4 n/ G% u, _( m* J& P - }
複製代碼 . G6 z+ p! H# ~: W# a4 G1 l
MeLineFollowerArray.cpp/ z1 S; l; {' ?1 T, m
- #include "MeLineFollowerArray.h"5 Y# f! T& {' j0 X; n0 s* i# a2 A
1 v. l% ?# I" K- #ifdef ME_PORT_DEFINED
8 l7 ^& N; a/ c- W0 O* [$ R - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
C! A. h7 N2 c1 [# x& Y - {" {. t3 j% X& j6 h
6 y) ?6 S/ s# c. F3 v. C6 e. B6 C- }& L3 B) U! Y6 }- u0 X
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
4 d3 E* u& j" L* e) g3 ]( J0 x - {
7 b' o1 [( i$ g! m+ t - _DataPin = mePort[port].s2;) q7 Z2 j: P* `7 Z
- pinMode(_DataPin, OUTPUT);
# ?- I2 f; p! C g k4 |! `$ C- ?7 n" R# w& W - digitalWrite(_DataPin, HIGH);
# S$ S. P1 J; y. Q* T; e% h - }
; p, r; W- k" c5 q+ d - #else // ME_PORT_DEFINED
6 D+ f% J* g5 i - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)- \4 T: h1 }, k4 k) Q8 i6 x4 f
- {
" D, w" t7 \$ a) o0 u/ Z$ O - _DataPin = pin;
* ]2 _: U( R! Y D. M, u1 P1 I - pinMode(_DataPin, OUTPUT); 9 y/ Y* M3 V4 y( E1 \# b+ n- S6 W: r
- digitalWrite(_DataPin, HIGH);
9 ^( l! x6 z: L G6 J1 S - }' X# ?* }: h- k) P
- #endif // ME_PORT_DEFINED0 _ R2 Y4 A5 t) o' M3 _
- ' ]/ r: T! X. P5 O& p! X3 P' S
+ F' F! t& H& R! g- void MeLineFollowerArray::setpin(uint8_t pin)
+ L4 W9 G# g2 S! ] - {6 `, k, s5 e. ?( z
- _DataPin = pin;
% q6 W$ ~: F& e9 ]' F - pinMode(_DataPin, OUTPUT);
. k% b1 r/ r$ q! U' d+ a, X( ` - digitalWrite(_DataPin, HIGH);. Q; L) K1 y. o" W( l, F7 W% w
3 l& u( \4 [0 h$ Z1 k- #ifdef ME_PORT_DEFINED
W% u# K: \5 D' ?3 ~ M - s2 = pin;% F, m8 `6 d0 k( c
- #endif* r9 L! j' m; T5 E
- }1 b+ ?- ~" G# U6 F
& H, l q# v/ w0 E- uint8_t MeLineFollowerArray::getValue()
7 x$ h" m8 Q. T7 X - {
4 Y) W6 S( M ^ - uint32_t LOW_level_read_time;
0 T8 b O1 u- t0 g - uint32_t HIGH_level_read_time;
8 J! V! W# j! @ - uint32_t time_out_flag;6 L# E* D' J& e4 u0 ]0 N- W
- uint8_t Sensor_Data[3];7 h) P% i( W8 K$ H% E$ K* t
- static uint8_t old_data = 0xff;
- i9 y( v' M( Z3 G* w. X1 A
' {: x: F7 m; C# g- pinMode(_DataPin, OUTPUT);
: f' s, V- @" v - digitalWrite(_DataPin, LOW);
0 j) m6 H* M) ?2 W9 f - delayMicroseconds(980);
3 O U6 z! P; o4 _ - digitalWrite(_DataPin, HIGH);
# d" V4 u& m8 s8 [: h. u7 i3 O
* g x3 C- D% q- pinMode(_DataPin, INPUT_PULLUP);3 d8 p3 \6 G1 J; `
- delayMicroseconds(10);
0 H+ L; X7 G. Z( U" a! a8 W - + [$ E& I1 Q' w
- time_out_flag = millis();6 S( W! s1 ~' g4 @3 F
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( K! e9 s, y/ T% a
3 H, B; c+ Y6 y$ ]* `) ?- LOW_level_read_time = micros();+ m1 j- A) q1 q5 ^: H/ g% N
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out1 j; n: z) \4 I R% H+ f( F
- {" I" \- g5 ~3 i! A' l- {* s' P
- return 0xff;# I( ^7 u! g/ } L4 h# R
- }' ~% ^7 U, U& ^& O0 f; w) H
" d8 @& e$ y8 `& _0 Y3 k1 Q- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ x$ m' o7 ]6 M - 9 i) M0 N! t0 u! Q8 D
- HIGH_level_read_time = micros();% F0 S6 C# f# x; Y3 s' d
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
: {9 t0 R2 b4 {2 C' j, }' H - 2 z' Y6 T6 u( g2 B6 u
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
[6 N1 p, w! S9 v0 r - {
! x/ s2 ?8 O4 v - return 0xff;
% s6 ]4 X6 d# n+ {0 x - }
7 Y/ g/ w3 q+ s+ B - % U7 D7 `( V# s( {1 S' q& O! g
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))% X( K- `: `) L
- {
9 N: O0 k0 {. |! | - return 0xff;
2 b% Y$ F3 Y; v) o - }
+ }# f+ ^+ T/ _: A6 h1 v
1 l0 e9 _" L" |2 U- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );" Y/ o7 ?4 H, @# X6 Q4 R
- LOW_level_read_time = micros();2 u7 `( \4 D& \5 k/ ` o
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
) F9 ? p |* k3 `6 s$ G! G - / E5 L( K& r) D$ M- o2 G/ u
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out! M6 k! K# Z0 {9 s# R
- {5 _& O# I7 G7 [3 h# t% [! o
- return 0xff;5 S/ L3 |- l p" i- Z
- }
: l2 b& w2 P8 U! l' Y6 w0 D) g. w
* F- ]4 D `* s8 n- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
* \" V4 L0 F. w6 n - {& S* x1 ^+ | K6 m* c) h" F
- return 0xff;1 R5 `: h- f9 J1 A @* O! |
- }, H/ k0 ]7 Q/ W8 Q7 P0 z4 Z! G# I
) T/ G, c9 z4 B5 G0 |- for(uint8_t k=0; k<3; k++)
0 _1 K0 ]4 s+ P% U `6 ^1 p - {
: X2 x$ [# g+ b, b K) M - Sensor_Data[k] = 0x00;
' z7 [0 e: a9 O$ n$ { - : I& }, N0 y3 U3 u$ N7 j
- for(uint8_t i=0;i<8;i++)
5 U: H) E4 ^4 D+ m3 t3 \3 n8 ] - {
& Y5 J. P5 R) O - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
' w- U* P; u8 R2 Y8 c* \% j' p - HIGH_level_read_time = micros();8 Z+ D1 i2 H% P, F# S; O V
- LOW_level_read_time = micros() - LOW_level_read_time;& x. e+ [9 C( y& {$ z+ D
; ~0 w1 v8 t) j' J- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
9 O$ F" {5 i [( T3 _8 L - {
6 W& n# h3 {) [ - return 0xff;9 o: ~- q0 i6 o; Z1 U) z
- }
2 S' `1 J1 h: H9 M- m* E
" r' h/ b. N& L- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
{2 w5 I' j. F6 A! t$ } - LOW_level_read_time = micros();
0 B: b( n" j# E% @* H" E" h - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level/ ] ^$ r5 O6 B! f
1 {- s: ~) g1 C# i2 f# ?, |- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
. D" z9 n& Z; P5 q6 r' j. O - {
- y$ S: \# e4 b7 V, b4 M" v" M+ ~ - Sensor_Data[k] |= (0x80 >> i);
& t1 p, |( W$ `6 r& } - }2 \ j, c- J' \% r# ]1 P
- else if(HIGH_level_read_time >= 100)7 S; Y+ }9 z6 ^5 e2 n/ }
- {
4 {' l1 N; j" T3 |. c - return 0xff;
, {$ x/ H0 U* L6 j - }* `* ?- W5 f4 d. F. m
0 ], |9 W; \4 t: n- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
7 c# t" C9 }2 r& t* {; u9 d - {
+ {3 ?/ ~8 f3 ` - return 0xff;. y) {/ d* U2 N" S. c' @
- }, L' f- r X0 }( n# s# \
- }, s; \% P; r% g# P* }9 w
- }' d5 v' [0 s8 |
- # x6 Y+ j* i9 }1 k( k& B2 v" d0 v
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level: ~% F _4 T; c: |
- HIGH_level_read_time = micros();0 _1 W& h9 p% f& e
- LOW_level_read_time = micros() - LOW_level_read_time;8 e- _9 r; \* ?" _8 K
- & H- N1 K2 w( a9 \5 z1 @ o6 G# j
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )/ f' l4 t& {, M n
- {8 a! J# o7 h) p3 L+ C' x
- return 0xff;% k% f: t; x# F N) x8 Z
- }
6 D' ^# }0 z8 }0 F9 `1 `3 \
% h/ [* q# j* w- pinMode(_DataPin, OUTPUT);
) |& B7 S2 Y2 G6 e+ Z - digitalWrite(_DataPin, HIGH);
, l9 S: A4 V" V1 q# E6 E* w/ \+ s - ) O' z# |; M6 E+ S* N/ e% [# O7 i
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
, c, @8 t% k* J% u$ \% I - {
4 `# I/ ~, u! m - old_data = Sensor_Data[0];
6 _8 x2 A; V1 E4 X - return Sensor_Data[0];
8 ^# n0 n& i; X) F' L; g - }
/ O: {; i& R, Q( Z0 X5 R6 S: R - else$ d" E! {+ b- b7 y6 C, z! F
- {
# Z$ i4 o* |( r - return old_data;
1 B/ p' f* w( h% \' s7 w' W - }" ^( w# R. K/ j
- }
5 S! }0 e l+ ?9 j8 o1 z
複製代碼 0 B. u5 X! G1 V/ M
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
* V8 o* S C, a; \. I: ]/ H' h' q9 t4 i
|
|