|
|
+ x# _1 K* B q
- #include <Arduino.h># J3 `% W |) T. I( Q$ o
- #include <MeAuriga.h>3 v2 n1 y' }4 @" y, s, G0 p
- #include "MeLineFollowerArray.h"
% e( }# s! k7 [) v, V$ A: e: g
& W+ i P5 n& E+ H- MeEncoderOnBoard Encoder_1(SLOT1);/ @3 C# @6 ^8 K1 {9 W) q, N% {
- MeEncoderOnBoard Encoder_2(SLOT2);4 ^8 k0 g3 G+ E p# H- N% \8 E
- MeLightSensor lightsensor_1(12);( r& I) {$ j/ E n- X( c
- MeLightSensor lightsensor_2(11);7 t$ u' E U' r7 Z! Q, A# v( a) V3 J
- MeBuzzer buzzer;
! v+ i% `! S$ I* P - MeLineFollowerArray linefollower(PORT_6);9 S7 U9 B4 Z# A
6 ~4 I0 r2 r0 E# B- #define Error1 1: n: w; n i0 Y
- #define Error2 27 y9 d2 |% v5 {& k0 Q0 F
- #define Error3 3& p# R% z! o: z' d
- 5 I: {. K( t/ M- ^- x( z! h$ i
- #define Kp 15( N+ ]6 e6 n: Z" B
- #define Ki 0.15
9 O, Y1 @- W0 _5 P - #define Kd 0.03" C- G$ p. c6 v: ^% v
- & w- c& f* [ a/ c+ `+ o# A" e
- uint8_t sData;
5 _) [* V5 t0 N4 W9 T. N* Q - uint8_t D1;% f& }& w a2 a* R' |. L
- uint8_t D2;
7 S7 p! a: f5 M" Q" @) s1 h - uint8_t D3;7 Z' j" |9 ~. H
- uint8_t D4;5 _: v8 e: n- V% @+ U# m, [
- uint8_t D5;6 o3 t1 ^6 s c7 ]7 b
- uint8_t D6;
. m4 G* b( ^* s2 O1 D- Z
+ e9 B. n0 }& E5 K- float previous_error = 0;
9 O4 |* I+ s# u) N2 s2 [4 T - float integral = 0;8 q" E3 {0 M% U% N1 P0 ]
- float derivative = 0;
; _, M, D# c1 |4 J" O( ? - int Speed = 160;
+ b8 \( Q" y, t6 Y- d3 y4 V6 o - float output;
6 ^2 @6 P9 }4 ]3 @
U4 q9 `0 \0 V8 U! }* l- byte Left;, v# ]7 p: U- r7 z6 e. ^
- 9 B/ k) Y2 G0 c f
- void setup()
8 Z0 z" z5 Q& s0 y0 P% z - {
+ r; q/ ]3 J+ H# C - //Set PWM 8KHz4 N# [$ H9 p5 k1 U0 }+ |- Q) Y- c9 p
- TCCR1A = _BV(WGM10);
+ D( T% `5 j& ` - TCCR1B = _BV(CS11) | _BV(WGM12);& J7 E8 Z* ^( H0 U
- TCCR2A = _BV(WGM21) | _BV(WGM20);# U8 w* D) v1 g( k6 a
- TCCR2B = _BV(CS21);
. ^4 O( B& K/ @9 P3 Y1 t2 `0 { - Serial.begin(9600);
. t! f8 l0 B2 _ - buzzer.setpin(45);6 u/ ^* h% a. k( {$ m0 i
- }
% x4 t. V7 ]+ M) @! ~$ m
6 l9 h) v) n& X- void loop()- a; T# f. @& W S7 D4 U
- {
" z O g0 o: k4 ]% n6 k W& H - while(!((lightsensor_1.read()) < (10)));
& X* V x: j' S5 h n - buzzer.tone(1047, 500);
; W" {2 R6 \' e8 G. W& W; L) { - delay(500);2 D/ a `- k' p: i
- do
* h# s( }/ W3 R, |1 N - {. p$ a0 x6 d. U9 A6 E! n
- int Err = getErr();
- d5 l/ {3 S' \9 V - if(D1 == 1)8 b7 \+ `) B' \3 y
- {
6 ^ `7 ?2 k4 U* x - Left = 1;' s& m% c7 u4 s# a
- }
, I2 e' X f0 `. z - if(D6 == 1)
; [! c6 X: _- x7 p: a - {7 `- `6 n8 e) X8 Y( o; j
- Left = 0;
1 n/ L8 @) l# C% Z) O - }1 \! D' I4 k+ x/ w
- if(Err == 99)- G: a% a) I4 i. h
- {4 B! M9 S$ T8 r# G1 p6 k! _
- if(Left == 1)% f' K0 d& U8 O
- {
7 @: p& Z) |7 n. k* ^ - Speed -= 5; G V$ e. K8 o, M% }
- moto(0,Speed);
9 ^% b0 \8 j- Y1 z - do: T& L1 l. J- x* T% N! e
- {8 M) F, l+ `) ]3 ^* Q. a& t. U
- Err = getErr();
& J' C: e$ e8 G - }while((D1+D6) == 0);
0 Y5 t0 z% r% D! s - }! h% O* _: { [2 z4 P% J
- else
' X5 c) X( z& o( c6 O9 O4 x) ` - {
% {7 V9 _. U8 J2 @* T" h$ d - Speed -= 5;
. ?& z, f& L& h8 T - moto(Speed,0);
5 ~- @+ q/ n# |/ d$ u& C- ^: j/ l' L - do
5 j- ?2 I. ~5 P+ R, v+ X) Y$ g! ^ - {; v: V' |4 A; `
- Err = getErr();( }5 {& a# U" Z) d# \1 p$ A
- }while((D1+D6) == 0);
8 h o0 w" H8 s$ Q. s% z - }% Q; ]: e* N7 H q/ T9 j
- }; c* s/ V, L9 m) ~* v2 Q/ M8 X% l' X
- else
8 _& B& q1 X5 U$ F - {
* u4 V0 a: x( _ o' r/ ~. h9 H) A4 m - if((Speed < 160) & (Err < 2)) Speed+=1; $ X8 R, z: e; X1 \
- if((Speed > 100) & (Err > 2)) Speed-=2; & } ?8 V) k& M4 g) z: q4 Y4 K" J
- integral = integral + Err;
5 J; D% B3 R: Q. f) P( t - derivative = Err - previous_error;. ~+ @# a, ?, F7 K0 d8 K4 C) f
- output = Kp*Err + Ki*integral + Kd*derivative;
. I! l, y& P) }4 D4 i - moto(int(Speed-output),int(Speed+output));
. g, o: F+ M6 a/ i M$ n - previous_error = Err;
5 u( O+ h8 p7 S+ X7 _. O4 y - }: f+ Y* D$ g, h- w. o: M% a
- }while(!((lightsensor_2.read()) < (10)));2 l2 f. T4 W N3 S# D B) S
- moto(0,0);
& f) g3 }2 d) j - delay(500);
. W, O* l! s% m4 v K - buzzer.tone(262, 500);* l. J: B4 n$ D' \: ~. k
- }
/ J9 |9 I* M7 w/ r3 y
# F, q: c+ M0 D3 q: n; O- int getErr()+ W. A. N3 b: v6 O4 L; b
- {
5 l2 j0 `) L1 d2 d. x# f - sData = linefollower.getValue();7 K! u. L4 h% s0 g$ K) z
- D1 = ~(sData>>0)&1;* D) b, J v Y( ^
- D2 = ~(sData>>1)&1;# g7 s' @0 y* h Q6 r: j% L
- D3 = ~(sData>>2)&1;
8 b! F+ }+ C/ M% L, b8 M; p9 B; E$ Q) M - D4 = ~(sData>>3)&1;
: |7 R8 h+ p( C& f4 } - D5 = ~(sData>>4)&1;
6 M. Q1 z- B2 Y - D6 = ~(sData>>5)&1;' Z: M. Y- l6 O& r+ N# H. c- ^
- int downD = D1+D2+D3+D4+D5+D6;
5 q- M" B$ w0 E8 F3 [% @ - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
% Y0 E# ]! t l0 k7 S - if(downD == 0); t7 y( p( |' _
- {
! O( ~( c' A/ e9 Q+ l - return 99;
+ D! i# H6 S- f0 i1 o! ~" w4 Z4 r$ ~/ n6 | - }
" Q) o; |! `% t; k - else. o+ [; r3 d; X% ^0 ^4 Y! V
- {( l& g' j3 V* E! x" u) ?' B! Z
- return upD/downD;% }( C# f- f) i! \
- }6 F9 C4 g6 C0 b' c- `; x
- } N9 p" y9 V; ~1 D/ Y3 e
0 x, L+ Y2 j$ u* F3 ^$ E- void moto(int Speed_L,int Speed_R)
7 h! C/ L. `: e3 M* m1 g% P) q+ { - {* T; @6 |, ~/ }* S
- Encoder_2.setMotorPwm(Speed_L);
8 r& y/ v" E% z% r1 L5 C - Encoder_1.setMotorPwm(-Speed_R);
# G, p7 u! H; J - }
複製代碼 7 L+ Z0 i( K2 Q' A& K3 B1 J# s; I
MeLineFollowerArray.cpp! X: d3 [8 Z' c# e& O
- #include "MeLineFollowerArray.h"
g1 i* o W R" F% D6 {# s: H - 4 _& g, a1 k; W( x0 ]6 e! A6 {$ m
- #ifdef ME_PORT_DEFINED2 x5 T/ v1 y6 n/ |; r5 h i6 q
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
2 x1 R' J& I) m# n - {
9 |5 B( V8 r" Q" D - - n2 F3 i5 f9 a
- }) D/ F, O) r& }# [$ P8 w/ I' ~+ S6 r
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
" x0 }- H# g0 D" V: T0 R+ g" u' k6 C - {' R! g+ C( V' o& w, E
- _DataPin = mePort[port].s2;, U' z6 Q, F1 r- e- q
- pinMode(_DataPin, OUTPUT);
5 S( O* T/ p0 A, d - digitalWrite(_DataPin, HIGH);" ]0 t3 n9 R7 ~# \$ L: }& G
- }
! y+ h9 I1 Z S; ^. | - #else // ME_PORT_DEFINED! h& X+ e+ G! l. r+ `% [5 D
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
+ X ]- a- p+ ]7 J; P - {
7 a% L& u( H7 E6 ^6 G. [* U6 y7 Y - _DataPin = pin;
! s l. K/ F" ~/ y - pinMode(_DataPin, OUTPUT);
# p* B/ f. ^1 i0 i, a& Z# v) \ - digitalWrite(_DataPin, HIGH);
& i s- u4 q$ o. p - }* p. ^, A4 [6 [1 c+ t8 q+ m
- #endif // ME_PORT_DEFINED
# J& h. Y3 E/ L5 i- m- ]9 {* g - $ Q1 E7 x4 M$ @' H& Q/ h
- ( V* ?4 q$ B$ p4 b; H, e
- void MeLineFollowerArray::setpin(uint8_t pin)
* T1 S% b- \/ p# K$ `; g - {
" O( y4 Y6 }+ f- u4 @ - _DataPin = pin;; c: p1 `. M' T/ P4 U) y
- pinMode(_DataPin, OUTPUT);
8 Q* }2 Z! u# }3 H* E$ a - digitalWrite(_DataPin, HIGH);
6 a) d1 ?2 K" l' [1 I; d - 4 f6 x. d( _6 {$ x. e* v
- #ifdef ME_PORT_DEFINED/ Q$ f; G* O5 R* G x& U) ^
- s2 = pin;% g9 U: X' `1 ^' E' [; a
- #endif
& u2 E F( e- V) m - }6 ]7 @8 A0 s' S( z. ^% w' L
: h$ r& W( T& `2 Q4 e/ F- uint8_t MeLineFollowerArray::getValue()
5 s& X/ q8 l4 S# f2 E - {
: h6 l* N3 q# ~( w9 x: @& | - uint32_t LOW_level_read_time;
. `$ f1 ~! Y2 D! M3 r - uint32_t HIGH_level_read_time;
. i9 L3 s2 N/ E M9 z7 T( T. @9 R - uint32_t time_out_flag;2 o" w! N# `9 i; R. ~
- uint8_t Sensor_Data[3];
7 s2 l& E" c) B5 t - static uint8_t old_data = 0xff;- ]2 Y( Q3 ^: t6 M, d; I
* g, R2 I8 H( d& `$ l! |- pinMode(_DataPin, OUTPUT);
+ U( ^/ p3 t8 H2 c - digitalWrite(_DataPin, LOW);
7 k9 M* b+ i" Y. W - delayMicroseconds(980);
6 l, o" n& q1 O; r+ B( h2 l - digitalWrite(_DataPin, HIGH);# }9 B* D) ~$ O
W: X" v1 n2 F% L- pinMode(_DataPin, INPUT_PULLUP);6 t# T& Z v6 I3 I2 N6 d9 z
- delayMicroseconds(10);9 {2 r1 `. g A9 U
- $ l- M7 f: z; O$ }/ K6 t7 s6 u
- time_out_flag = millis();+ { I0 q9 H$ e: H% t' U0 `
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );$ X4 p: R7 v" D; y& x
- + n4 A+ S- H4 Y4 G# V" B2 x
- LOW_level_read_time = micros();- O- f! k4 J7 Y. \3 E" m- H9 l) `
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out# e: y. Q$ F4 e# z
- {) X8 z) e: ?: Y9 x0 g% g
- return 0xff;
; l7 ^0 I3 i0 Y* O: G - }
8 i! T. \0 r% j, O& E" G) h
6 R% c S/ j. g" v* K" |% \ J0 t: Q- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
: u3 `6 V. o" C: _0 |
6 \& j' b# O, ?' U& B- K4 z7 c v- HIGH_level_read_time = micros();
3 t7 c* x/ ^7 ` - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
2 R1 H+ C. q2 R' J W( d
. B" l4 a7 N3 I- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out7 n/ G5 G' S: m- M
- {
$ x9 b G4 b3 `: q - return 0xff;# d" N, H1 z: w8 F
- }
0 F' W% C" |6 i4 X7 t - - _- w1 @" F7 O" d
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110)): g- K6 E3 k$ I* j' c! Q9 t
- {0 A5 b' z7 G3 Y% r+ S2 C3 ]" ]8 n+ Z6 K
- return 0xff;
: x+ C6 u; _; r0 k& _* E" V9 _4 c" A - }
# d3 L2 r- g( `; C% I" Y: J
& w5 V v8 m; S- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
8 F3 m. s* |% j9 k# M - LOW_level_read_time = micros();
* E/ @; W/ @7 I/ U; T: F - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level1 M7 W, _6 @0 X5 a% ~
( y* H) O5 N$ c/ }- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
! f* O" X2 R8 L+ l - {( F0 q0 Z ~6 W$ j' B& _
- return 0xff; ~ c) Q; Q' N6 h% G
- }' O% i2 @% Y0 m% g+ A' U2 `3 |0 f
# Z8 }' O2 Z' s9 S- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))' r4 r7 d4 r& |5 O
- {3 t& p/ p) j1 B/ S$ o
- return 0xff;
8 ~* H; `* ]2 I+ x" q0 \1 n' N - }
1 g5 f/ ^% G, G) _
- K( u) O U( Q' z) n) H/ y8 W) h- for(uint8_t k=0; k<3; k++)
0 {2 M, r% j7 `3 R& F) f- ` - {
, K+ Z$ O9 z; m/ M$ |6 V - Sensor_Data[k] = 0x00;
) d% t+ m' W0 J1 d, T* l - 8 Z- y9 s5 l6 J8 K% a6 f4 Y
- for(uint8_t i=0;i<8;i++)
8 Q( \- K$ o8 l0 d% I1 M) d - {
/ r( m( p* A+ x - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level+ l" _& M( v" G
- HIGH_level_read_time = micros();
" ^6 T" S3 [) m7 r; s) L - LOW_level_read_time = micros() - LOW_level_read_time;
8 `1 w) c4 ^6 q8 E
/ e: @6 @1 V) d u' U) y- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
7 ]) k& Y, J! `, g' l - {
3 S. {' z- s8 d7 n7 G+ O; F - return 0xff;* f8 n y$ k# V. a, u! j+ F% t/ j
- }' x$ M8 a7 A* I2 L; _
- , K$ v) \5 F) s1 p5 b0 I( p7 Y
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
" e4 N8 [; d6 a4 ` - LOW_level_read_time = micros();1 \' ?$ G& C/ D6 N8 `8 G g
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
* C3 A7 U: K) \# Y7 j; n - 4 d7 I6 Z$ ]7 K) q' B
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 13 v0 f, H/ U" b' Q
- {6 D# R. f6 |0 Y; v K
- Sensor_Data[k] |= (0x80 >> i);+ M+ x, [% W* o j ~/ X8 I
- }
: B9 B4 A) L% H - else if(HIGH_level_read_time >= 100)
, G# v, w, s/ A - {: |9 q2 _% k7 ]3 C; X
- return 0xff;0 @3 L A: ]- n* K9 I4 e7 F! f
- }
9 [& }3 r6 g- z0 F
# i! l4 Q B3 j! W% z% r- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)+ _9 v4 v* x3 b$ p3 {6 u
- {9 o0 _' K7 n4 E F5 w2 Y% k
- return 0xff;7 ]9 B. J- Y; X1 {3 p5 |; [- q
- }
: ~: a N6 b- A# @- W - }* B1 Z' m+ {% ?2 T! J' a+ W4 c/ I
- }
7 S; V# ]/ j/ L2 L" D7 |
* v/ W& Q; l; I* c5 Q- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level& o2 l+ v! u( h
- HIGH_level_read_time = micros();& o$ S: ?& F* \0 d" D! f' H% C/ ^
- LOW_level_read_time = micros() - LOW_level_read_time;5 w# `& Y4 h+ }0 i+ D* l" L D
O# n% K s- V' N% a- y9 P) w- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ @2 |9 A, p/ l4 v
- {& U6 ?2 P+ ^& E9 Y, E- {7 I
- return 0xff;+ s# a( M/ E% O1 z7 W$ a% e3 ]. q, b* V
- }, ]; }7 G/ @+ I
/ ]$ L9 Q# F: j! B- pinMode(_DataPin, OUTPUT);
0 {2 Q: Y% I. T" O# X& g - digitalWrite(_DataPin, HIGH);: ^4 `; L5 ^: z$ |6 s6 u+ ^
- : b8 {5 q/ R+ c7 D" @6 [. u/ {
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
" y3 r' F+ V0 O R# n: G - {
& Y" a; [& \& D- n - old_data = Sensor_Data[0];
3 `8 M3 @& S+ l. X - return Sensor_Data[0];
4 Z9 [ x0 P: S4 L, F; B$ P - }
( o& C- C( H. o | - else
8 f. a: J$ `9 e+ p# ^- D4 u - { R. u' O0 j) H2 ~6 R: H: Q
- return old_data;
- N- s# z8 h' b - }
2 _6 @3 t8 x1 ~& x4 | - }
9 ?( B& \& f6 g: B) ?4 Y
複製代碼 8 W/ e, S5 G3 }( ?* r
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
M6 h' d2 X+ N' w# x( f- p& C0 M6 e. J
|
|