|
, X. f. w: x/ H- #include <Arduino.h>) h3 H. t+ l6 Y: I/ e: W0 A
- #include <MeAuriga.h>
- e2 _% n1 i m- ^7 C# F; f' W8 C" X3 ~ - #include "MeLineFollowerArray.h"
/ \0 J5 B& b& ]! \
4 |* K6 K2 _7 k( W- MeEncoderOnBoard Encoder_1(SLOT1);
! M' ^! E# L# c' n+ T+ h - MeEncoderOnBoard Encoder_2(SLOT2);" j G8 I1 ]- t1 N4 I% p
- MeLightSensor lightsensor_1(12);1 N. R$ J" K5 g
- MeLightSensor lightsensor_2(11);% e/ u" P( W* g
- MeBuzzer buzzer;0 s: }/ t) Y3 g
- MeLineFollowerArray linefollower(PORT_6);- {. D8 C% D* b, Z
- 3 O1 _% C3 C9 m# a( q: F
- #define Error1 1
6 a2 t4 k3 y0 z. d3 `8 g- t, f/ l - #define Error2 2
( ?5 N- ?5 ~! K5 N, K$ { - #define Error3 3
9 T* ?* d" g' s, y* O
5 m- h& h7 U7 B6 k- #define Kp 157 \, H% D; h3 a6 j7 W! r3 u
- #define Ki 0.15
9 h2 d1 }0 W( z% e9 f8 b - #define Kd 0.03) y" \: M( |' p: A2 g, N
- 1 |( |" r6 Z) }# f1 t
- uint8_t sData;
: e& [; [' ]5 W - uint8_t D1;! b1 i$ E) H& ~6 b" d# \& C
- uint8_t D2;* f; V+ O7 n% y1 T$ v6 G7 w" E/ P# w
- uint8_t D3;5 U; F$ I( v' k
- uint8_t D4;
5 w) [+ {1 ^' @4 R2 q4 I - uint8_t D5;
8 D l+ D( G- g- y0 K( D+ E9 B - uint8_t D6;
( ]' F8 }% N0 S, s/ P7 s- D0 o! R
+ s1 K9 s% J+ i8 z5 v2 E- float previous_error = 0;
$ m' G3 X' \+ H6 Z: [* i - float integral = 0;
- A, j5 Z4 C* ] - float derivative = 0;7 t- L2 w* W5 A- H* `/ h, w( U
- int Speed = 160;
3 e h& ?, }$ q1 ~6 f8 Y) o" N5 F - float output;
8 z5 X+ G( ~4 x2 l- a( i - ( G* G x' H/ ?
- byte Left;9 g0 W+ n, w6 {
4 Z+ J3 X& Z! {" y" C4 n M) g1 u, d- void setup() # G. r% R9 [* l5 f i+ ~, H
- {
, P8 s2 m) [! W! s9 T) y5 Q - //Set PWM 8KHz
# z7 B7 V9 L. K - TCCR1A = _BV(WGM10);& g/ a3 K ]( g' Q" Z% f" k1 l/ L N
- TCCR1B = _BV(CS11) | _BV(WGM12);/ i6 P+ F `' R | w7 d
- TCCR2A = _BV(WGM21) | _BV(WGM20);* N# s+ {# _/ T( K7 k
- TCCR2B = _BV(CS21);: V' ]2 f6 Q& C0 X* i. f) d/ l
- Serial.begin(9600);/ `4 _5 z9 o' x4 y
- buzzer.setpin(45);1 B, ^: F" T6 `0 b$ A; j
- }
g, C# G! b- Q% M' d1 T: F8 S - + O5 }% @: d2 K8 v2 p6 w
- void loop()
1 t! i2 @+ N% @& h4 A2 O( D- f - {8 Q! M! ?/ [1 H; c, C& v
- while(!((lightsensor_1.read()) < (10)));
+ ~( m* K$ i: a# i O1 ^ - buzzer.tone(1047, 500);1 _. a P1 Z) A$ b' n
- delay(500);
3 `# M5 t- l& B" K: C! q: R4 V - do
0 @. R3 V1 K3 L* Q - {
4 e0 J2 E3 }7 u; X- t5 {2 w - int Err = getErr();6 f+ m" c* D9 d; `
- if(D1 == 1)" C- B( _1 o% h, {
- {
2 s. S* G2 j! f - Left = 1;
F g9 m0 ~3 G Z" i - }
- v$ O5 v9 H9 J3 P; G: S1 U - if(D6 == 1)
5 ], v1 e! d& {( l$ \* n# p - {
3 n/ C; P; n+ R6 D; u5 t! I1 K - Left = 0;; P- U8 @# [7 }7 S% U* ~4 o
- }
2 i q) [& P4 y# k7 k - if(Err == 99)* v4 Q5 B2 F$ x3 p# k
- {! Q* u6 T% F# R* v
- if(Left == 1)
+ X' N- U9 F) V! v0 Q. W - {
! h( d3 w# v7 N - Speed -= 5;
3 F6 d/ N4 x% f$ a - moto(0,Speed);
/ f8 L7 |. e$ ~6 m" F2 P - do
- d5 p! [: R2 A6 b - {, |: L. d/ Q0 M
- Err = getErr();
4 C" G8 ^6 | [/ ~- z" S - }while((D1+D6) == 0);4 K9 o. l; }+ T" [/ v
- }
& a6 n$ n- k& H4 h4 Y9 G n - else! }7 J* S/ O# t4 n7 V7 W2 U
- {
# n0 \5 j3 s3 a/ y4 ~ - Speed -= 5;5 p, p; z) w; b6 s& r2 l
- moto(Speed,0); 5 m, f! y# ^1 j- C
- do3 w9 Q2 W$ g1 Y, s
- {
( B4 J T& a5 H. L$ A0 }) { - Err = getErr();. t2 e, q8 O! j9 v* @
- }while((D1+D6) == 0);
" J3 C- E4 z7 ~( S; ~ m - }
; N) Q; |: k3 |: z4 d& C - }+ b0 y" M! E, w8 O4 A+ _% I
- else
1 S, S5 J: d/ g - {1 c. |0 w7 v& l h# U) P
- if((Speed < 160) & (Err < 2)) Speed+=1; / O( I+ a. e8 }
- if((Speed > 100) & (Err > 2)) Speed-=2;
9 b3 L+ n8 ~0 h, y0 ?- z - integral = integral + Err;
( W/ d- L6 I" n+ X7 B: z$ m - derivative = Err - previous_error;
1 i, K7 c8 s9 R: Y - output = Kp*Err + Ki*integral + Kd*derivative;
6 D# d& z3 t- F1 O - moto(int(Speed-output),int(Speed+output));0 E. M6 |% x9 z0 F' m: M9 X5 M" c
- previous_error = Err;; B; i( N% r0 I& Q# `, W
- }
9 q& o- W6 M) \; b, k - }while(!((lightsensor_2.read()) < (10)));: Y7 a' q+ [2 Z
- moto(0,0);) a5 _% n/ H3 e8 ?8 R. i4 J
- delay(500);
3 t8 Y; P0 J/ L' @6 w, H7 ] - buzzer.tone(262, 500);
. l3 @. `; `, r5 h* d- u - }
7 W& s/ N8 |! |
3 d7 O+ ^, l/ i0 |- int getErr()& D! R; @6 F* E1 i! y) y
- { ( G' C, B% a2 C" V! e1 R
- sData = linefollower.getValue();/ A( c/ r+ ^: B' h: o0 W
- D1 = ~(sData>>0)&1;. l4 _. l" C8 G, h3 E
- D2 = ~(sData>>1)&1;
3 m0 C v& l1 M Z3 ` - D3 = ~(sData>>2)&1;
+ b5 c9 h7 k, G% N1 V1 v4 h( P - D4 = ~(sData>>3)&1;
; { I( m, b" j - D5 = ~(sData>>4)&1;
9 e' s6 O6 c! @' S - D6 = ~(sData>>5)&1;
3 F/ I( u* c/ a2 l; m% A - int downD = D1+D2+D3+D4+D5+D6;
6 M/ J/ l3 y, n& A - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
( M; G. Q$ g: v - if(downD == 0)
7 C& j3 g' v" c4 \2 V% b - {
7 P. H' T1 u g( o - return 99;
7 F/ J- X4 R4 Y* l - }
/ N1 ? Y) W. B3 x# z7 I; s) ^ - else
. a" u+ l& d/ N - {
) V# R6 T$ |4 ]% F) b - return upD/downD;$ ]" i4 t0 _4 s/ D
- }
' S- P1 K, A5 R- R - }
+ A2 N7 C; T X& @8 n - 0 h/ w7 Q% T8 V
- void moto(int Speed_L,int Speed_R)6 a6 A, d& ?' t5 B# E( s
- {4 U3 r* |2 p! K5 e1 b: W9 x
- Encoder_2.setMotorPwm(Speed_L);
+ i' l1 r% |. h* N* [* h% g) F - Encoder_1.setMotorPwm(-Speed_R);- Z7 K2 Y2 X+ `
- }
複製代碼
H' W$ R& X6 j% m9 r- [5 ?& Y7 E% GMeLineFollowerArray.cpp
7 i b4 H+ @. B2 s, o- #include "MeLineFollowerArray.h"
/ G2 `0 A9 ^' g2 |& p# h% W - . J! Y g: G' b Y8 K9 i5 u
- #ifdef ME_PORT_DEFINED
: l& y) @9 F+ j6 `* t( h3 M - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0), U/ h4 {, \0 h6 v0 k d t
- {
1 m. l( Z& |' {9 j/ O5 n# g
, f" w; n) _0 N* \! M ?1 O+ J3 R0 ^+ F- }
( `* W% z: m3 T/ K) Z( ]' J - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)1 I( m5 t+ K0 J2 x" [/ L3 C
- {
$ X9 X/ Z( [, N/ L) R - _DataPin = mePort[port].s2;
- f/ ~* u, ?) K+ f: e: j9 b2 A - pinMode(_DataPin, OUTPUT); 0 g2 M& ?. C1 g4 }
- digitalWrite(_DataPin, HIGH);7 n2 P$ j* v1 J4 ], b8 i: C
- }9 {' q* A e$ ~% n% x4 S& Y
- #else // ME_PORT_DEFINED# ^: F$ E9 r. E
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
7 L0 o! A2 p R+ _) a1 b6 B - {
l' s0 }0 E6 K9 J, a - _DataPin = pin; c0 e( y, @/ c/ X( P6 S: u- T
- pinMode(_DataPin, OUTPUT);
, O* s5 a, K$ a2 b - digitalWrite(_DataPin, HIGH);
' F+ u. V/ l* d5 H' U& `) A5 m* k - }$ C- l# N# G) Z; u g
- #endif // ME_PORT_DEFINED
" f9 f8 @7 Q* P Y$ |
2 t5 f9 W$ @$ q! d' L' s E- , v! L. s# M$ l/ s) G; r
- void MeLineFollowerArray::setpin(uint8_t pin)
1 o6 Q- s/ h! O - {
( D, a4 Q6 ~3 t" U$ j0 a - _DataPin = pin;9 U: r8 ^+ g* v4 V; B4 i
- pinMode(_DataPin, OUTPUT); 9 p( G& H! V) J+ T$ _
- digitalWrite(_DataPin, HIGH);
) P% I9 s; }" R3 j1 _
. z! M' i; F$ O. s, N3 n2 m- #ifdef ME_PORT_DEFINED
! O+ i: |/ B( q# J! k# M - s2 = pin;
9 {6 N2 D, |# W$ f - #endif5 `7 r% I! R) b: Q% g/ [
- }! i3 f9 N' t) ~: P- w1 v: x9 }" O2 }
$ n) x- K" q& d6 X+ X- uint8_t MeLineFollowerArray::getValue()
, F( M4 [, M! g6 }# r" O$ C - {: F( p) d2 Y- C( @& l7 S R. Z
- uint32_t LOW_level_read_time;. l5 I/ q! N+ s/ ?$ P6 l9 v
- uint32_t HIGH_level_read_time;; a; }/ y" \. H2 g% Q& f" ~" I
- uint32_t time_out_flag;/ _) y& t; t- A& W
- uint8_t Sensor_Data[3];4 e4 i* o$ K: p4 G( c3 E& f
- static uint8_t old_data = 0xff;( {+ G! L( E# S$ g" ]7 A+ A3 l
% d8 s r" K/ ^5 i- pinMode(_DataPin, OUTPUT); v; [+ O/ Z' V* k. ?& ~& b: Q
- digitalWrite(_DataPin, LOW);8 ]$ r7 s. [8 t' @5 T( E) h' g
- delayMicroseconds(980);
- D5 S% v, ]4 m2 Y9 A - digitalWrite(_DataPin, HIGH);
6 @+ V- K, S9 k, u$ ~) x
, B7 x) A7 v+ t' W' o7 J- pinMode(_DataPin, INPUT_PULLUP);/ _' D; Q0 H9 `9 D, }8 S0 o
- delayMicroseconds(10);- t) |7 w4 u. m$ I! x- C
; G/ o8 V7 b; I6 h: E0 K2 E- time_out_flag = millis();
1 f8 s; i" _: C+ g - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );! q$ m2 j& `- }6 {& Q& K
b- _* M! }4 D! i- U/ |8 B% b4 _- LOW_level_read_time = micros();
3 g* r+ T6 @9 N - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
' y; Z. n: F1 {* w6 l7 L - {
, {! o7 r' @, H" W9 ]$ Y - return 0xff;- ^& V: T6 u, u+ S4 N
- }
; C( _- X1 E. {
: V+ N9 ^0 I& ?7 g3 B7 |0 n- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );3 P, ~% M$ @, e2 g, n* F4 u: |: l
- b+ A5 |9 I& g
- HIGH_level_read_time = micros();
; _0 N G( c' w$ a; ]* b - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level. R; p; F) {0 e) u- o3 B2 ]2 o9 u9 B
- # Q# ]3 ^7 h2 q0 v
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out( M2 d& b; V* r1 c# ]( g0 D
- {
/ b$ v2 P- A5 r4 \9 r - return 0xff;
: p" p5 g& ?( y f. ~ - }
% k8 @0 R. D9 I" h) |. r2 j0 f - . n+ |* u: ^7 \8 [/ W& l
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
8 q4 D0 \7 Z8 y3 ^. s3 K9 C I - {
/ |4 N J) D5 k% t# t2 q7 \ - return 0xff;
& g; ~7 m# e# ]4 M/ J5 H5 y0 v4 H - }7 ]( H1 g, i( _- h! N U
- 3 c+ U8 _- D# n* X) D
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );0 s1 h( B- F7 H ^9 R! L* N Q" M
- LOW_level_read_time = micros();4 M& n8 h# g8 p- F
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level: a8 y( V. y+ _& M' a
- ; I- R# D. M1 ]
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ D$ Q$ s' }& j) \7 n& { - {
- Q* w7 ]* ?$ Y) O' f3 \ - return 0xff;
* R; T( i/ _7 T. W! S - }. k8 f) |9 Z& T* g- \( a# z
, f' Q9 U; g3 L* g/ t# l- a- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))- X+ |1 a+ w4 a! q0 }8 X; O
- { N4 v8 _# ^8 W$ z& h. R
- return 0xff;
* y( b Y+ r( t( w, r: i - }; I) g9 n/ X& Z1 l. u+ i- |+ O
- 8 h) f+ u* V' J0 ~
- for(uint8_t k=0; k<3; k++)
4 @1 ]5 q- |6 q- f, v4 k - {, w8 h8 _1 b; V
- Sensor_Data[k] = 0x00;5 L+ V. r, p6 c
; L: z' \4 m- D' M6 y1 L- for(uint8_t i=0;i<8;i++)% ^ a4 v7 S9 l- q0 f, x
- {
+ n# x' b. J$ Z! ?! f' C+ L - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
+ j V+ w7 o& f7 P7 E$ C" B5 V - HIGH_level_read_time = micros();# S4 j* Y* {3 X
- LOW_level_read_time = micros() - LOW_level_read_time;
6 {6 w+ m" Y1 Q# i- J! O
! W$ p0 t- q4 ~ S- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )! e% B W j6 e
- {& v$ b2 K* V- M# L- m( V" O6 u
- return 0xff;
! D/ n" v3 N$ m) g/ W - }) u7 o3 e3 a# @# ? X! l: D
6 c1 w2 A: k2 c4 M- y( U; C3 ]- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );+ K+ t. r8 l- `0 J
- LOW_level_read_time = micros();
# C- s5 D, Z4 v - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
. N; u3 P/ R6 F! J8 K
7 R0 ^5 ~5 X* E- [- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
7 w+ y4 X X/ d7 C, \ - {
% {+ d( C' o7 B. v8 z; c$ u - Sensor_Data[k] |= (0x80 >> i);
# M; }8 L+ X* q& Q, U - }; F$ h; }4 k4 h6 J
- else if(HIGH_level_read_time >= 100)
+ [" ]0 F6 \* N+ v) f+ R6 I - {1 p7 h" S1 M- G
- return 0xff;. T3 E) q* i0 x u; R
- }- j# K1 W8 K! y4 v* B0 S" h
1 ~7 [3 T8 c4 E, y- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)- f# `/ |- V( V; z/ ~% h
- {" j! Q0 m. E! t$ Q$ A
- return 0xff;
* D# U: h* f) D7 f- P$ r) z - }
/ q+ @& G& [7 [5 n8 F b% ? - }5 u% t5 L, v0 v
- }. u, C* j7 f0 b
]' ~: b" C$ ?4 B2 X/ Z0 c- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
. m/ @* c4 E3 p: s/ q - HIGH_level_read_time = micros();
% W1 L* L x- N/ y; s - LOW_level_read_time = micros() - LOW_level_read_time;& i- M# z1 k3 P1 F4 {
- + ^7 u* u; ~8 ?: Q: v
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )3 A5 K% s! {1 U' F7 c, V! S' j
- {
' M5 y1 @! ?- o2 y( w - return 0xff;
0 B: Z" d) n& _- D6 K - }" e3 d6 H8 P9 O; a8 J# h, ~
% u/ }3 y; L! H" a2 c8 V6 O+ k- pinMode(_DataPin, OUTPUT);, M1 g. E9 x( r9 `/ |' X; t% J
- digitalWrite(_DataPin, HIGH);
& D- U: j* z4 h; N - ( v+ n5 M0 t2 s3 b6 R4 s4 e
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
! s3 ] C; R- k3 }) j, p: Q& | - {
7 w: Q5 e9 B Z- ~% ?' I( m - old_data = Sensor_Data[0];+ K8 S: L# r3 }6 Z& Y; u
- return Sensor_Data[0];
6 j' r% n/ H+ ~0 n E4 m* z - }
3 M) K: Y, M$ a" T; ?/ g - else$ P5 k( U) c" g
- {
$ |! Q) x' f l2 I% Q - return old_data;
) C: O7 o9 `2 p7 v - }# @/ m+ a& F4 h5 k) [
- }/ q2 w/ Q$ Z( E0 v9 i
複製代碼 9 m! a" ^' E c$ V, I7 s
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
$ h, h2 Z% m4 w2 o
! O! y0 U, k( T |
|