|
|
" b% J4 m: C) ~: d0 b2 A& w- #include <Arduino.h>7 O. v+ d1 F: W; j6 A* O- c
- #include <MeAuriga.h>
# i' B* R8 ]4 S8 }5 M - #include "MeLineFollowerArray.h"( I+ y0 F; o2 [2 f: v+ U; n2 m9 Z
& `. y$ A) q' N- MeEncoderOnBoard Encoder_1(SLOT1);
" f. Q, i/ e6 b - MeEncoderOnBoard Encoder_2(SLOT2);2 n! F' @' q1 y1 ?* u+ j0 d
- MeLightSensor lightsensor_1(12);+ g% ]) V* v% o! b# \% i
- MeLightSensor lightsensor_2(11);
- z s4 Y' H$ [/ G* z+ m9 { - MeBuzzer buzzer;, \1 H% n3 c2 [7 g) T6 t
- MeLineFollowerArray linefollower(PORT_6);
! g! M3 ^) k" s' _) f8 } - ) t. e3 M) V3 y+ p7 L" F1 A: |
- #define Error1 1
. O( i: V0 r$ B: z' \$ @) b K - #define Error2 23 }. ~$ q6 R5 r
- #define Error3 3
$ H. H5 P+ s- J% e6 @1 A; ~
$ L7 G- n% m+ Y2 ~# ^' M: D- #define Kp 150 S/ h8 A/ m9 z7 J8 Y( [
- #define Ki 0.159 @, m; I$ e7 f9 @$ ]3 F& \) Y; m G3 z
- #define Kd 0.03- t# i) y$ H! d& c. T0 e
V) a& a( V9 _- e% P2 c) Q0 ]7 _* H- uint8_t sData;3 F( o( ]$ u: f. @
- uint8_t D1;' I; U) l7 \8 S
- uint8_t D2;
6 X* P( c l5 Q; x6 R - uint8_t D3;
" [% f* O+ b1 ~3 E# a, V - uint8_t D4;8 T7 s, G) f% g/ u$ T
- uint8_t D5;
" q; V$ U7 B( F - uint8_t D6;- |" W' Y$ a8 w9 F+ W8 [% o3 m
- ( Q- g* J B/ ^+ d* q
- float previous_error = 0;
+ r* q% D& K; O; B5 K6 K, p - float integral = 0;: t. e- G0 w# r6 a0 C! `/ T
- float derivative = 0;
6 g l& i, `0 M. d7 t' \ - int Speed = 160;
. i# U1 Y" v T# G - float output;
( d- e6 R7 G1 D+ O# Y! H - 4 k* ~# l' P% Y X% ~- S
- byte Left;
1 z/ e# p# Q$ ^" _! X: X5 {# T - / Q1 P) }6 R5 o' P
- void setup() 2 W# w' u% R! H# C5 q
- {
0 r: z5 u( n9 @7 S - //Set PWM 8KHz
6 S/ s$ W" O) k5 t - TCCR1A = _BV(WGM10);
* y4 w( b; [; T/ l/ Z3 X6 F - TCCR1B = _BV(CS11) | _BV(WGM12);4 m; M/ s" ?9 G6 |% c4 k
- TCCR2A = _BV(WGM21) | _BV(WGM20);. ~2 p( N* T; _ Y
- TCCR2B = _BV(CS21);
, I& K8 v4 ? Y* M4 u/ t2 P: p - Serial.begin(9600);
' k+ v! \: k" S4 ?/ i- [ - buzzer.setpin(45);
* }: `" S8 }$ a& i" U - }
+ ]# S/ S/ r; B( c" Y) `7 g* J
1 C6 m( v1 r5 }! q4 n; F4 J! j% C4 }- void loop()
7 `6 X: O* d" u! i0 t - {" {: ]% d/ f3 T9 k7 O+ ?: s
- while(!((lightsensor_1.read()) < (10)));2 r4 z# C) F( Q, Q
- buzzer.tone(1047, 500);9 _8 V. x, x8 a5 z3 c4 L
- delay(500);* Z4 W2 o" `) O9 K% S) l8 ?# z8 j; U# s2 G
- do
) e' l! n9 L j' a+ ~! y - {
$ J+ l( s% C R' f; q - int Err = getErr();- f# O; S7 _9 l8 v
- if(D1 == 1)' H+ M) m/ P) I$ ~/ X' H1 v* D
- {
: @+ |8 Y1 a! j8 t0 ^, i - Left = 1;( l, k- F6 Y/ U
- }- w3 |2 ~+ M& x$ z. i
- if(D6 == 1) R! M3 F; o5 ~
- {" u+ k/ r& w- x. N
- Left = 0;5 Z, y. U6 Z4 T: @6 Y
- }
$ i7 b Y& x/ m: H0 x - if(Err == 99)
4 D& | k* w, ]+ \5 _ - {/ W6 i, v4 y; Y( m
- if(Left == 1)
" r% ~4 W5 g3 h* _ - {
- l9 j! b, o0 J9 C$ |7 | - Speed -= 5;
' S2 s h/ D8 l - moto(0,Speed);# r/ F+ R" {( o$ t( F
- do
4 G& B8 x+ R7 H2 W7 s( u8 v - {
- g2 `: z5 c9 h; G2 v% A5 d c6 M2 [1 X1 g - Err = getErr();
# b/ ^" V7 X# g. \1 a2 {& _% F - }while((D1+D6) == 0);* s, Z/ I* J7 M; b* @
- }
6 h' V/ J6 ]! a3 c: F; | - else
+ ?9 `" n+ V$ p9 ? - {0 Y1 k0 F% d! @( h, y
- Speed -= 5;. c8 l- ~& b; w' V. O
- moto(Speed,0);
2 G( t0 @6 N: ]5 M - do
9 |- @+ S- }2 `8 L9 d - {$ l' D9 \% K2 X
- Err = getErr();
" {7 [" K+ h7 t; B# k - }while((D1+D6) == 0);
" D8 p" Z6 _$ K2 @ - }
( B0 S) c! \' N1 g - }& E/ \ A j; n% L$ G/ I
- else8 Z% R/ h& s' r
- {
6 \! ~( n% q) Z( [- R9 V - if((Speed < 160) & (Err < 2)) Speed+=1; ' s, G6 ?7 D# m
- if((Speed > 100) & (Err > 2)) Speed-=2;
& Q1 |# _1 l! u: Z. s$ @6 y. }; Q - integral = integral + Err;
, t' b* F2 Q$ l) [1 s - derivative = Err - previous_error;: m" I O# A' c# b
- output = Kp*Err + Ki*integral + Kd*derivative;* s1 x) B7 i* R# w' l, s
- moto(int(Speed-output),int(Speed+output));$ u, [5 A5 ~; b: Z% q% J6 B! M
- previous_error = Err;6 D1 W7 Y) D8 E3 p6 S
- }8 W7 A0 B. P3 U& n" l
- }while(!((lightsensor_2.read()) < (10)));' Y$ Y3 Y) ^- F7 @& V6 x$ E
- moto(0,0);) k$ ^& T' V o" {# {9 G* b
- delay(500);# Y8 ^" E: p( {% y/ T& ~
- buzzer.tone(262, 500);
! h/ k T4 {7 G# d& N' u - }
; ?4 `' D7 N% {! @ @
6 f! p2 y t. B, S, q- int getErr()
6 y+ P; r$ a- }/ r9 Q6 V. ?2 j4 _ - { ' A: A3 d0 ^% T/ w" u
- sData = linefollower.getValue();
, L2 x2 X% r7 u: H - D1 = ~(sData>>0)&1;) [4 d5 _9 j# }# J, C2 E0 |$ G
- D2 = ~(sData>>1)&1;8 b( E& M+ w) f! y1 p8 t
- D3 = ~(sData>>2)&1;9 I8 h9 G, K; n5 \- }
- D4 = ~(sData>>3)&1;
0 l) |$ E* c6 _, Q- V: ~ - D5 = ~(sData>>4)&1;% V( C* M% ]9 `( e' @( _, F$ k
- D6 = ~(sData>>5)&1;" P9 D9 O! Q; t" E- N
- int downD = D1+D2+D3+D4+D5+D6;
4 S* U0 P( ^# D2 b8 H - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
. |* x2 B6 L8 {; I5 z5 j - if(downD == 0)
* y: _$ |1 g) R4 f9 t8 O. h& A - {
2 X8 G: _! d: a9 G - return 99;
" B U# G$ W+ y - }
- t9 h, r3 y7 x: p3 a9 N - else8 j, e9 g/ y! g- a I
- {
3 D8 y; M9 f( {& v8 n3 r0 l - return upD/downD;# e$ j% U9 y, ?/ G$ j2 ~& M3 s
- }
; C: v4 W+ g2 t4 ~3 U8 O - }1 ]2 @; K% d! ]# A; {$ _0 _' ~+ D
! V0 A# w9 `. e/ e- void moto(int Speed_L,int Speed_R)
4 ^/ F+ X7 T9 f4 D: u: o: e - {
l: ^; V3 W7 e2 r; x7 z - Encoder_2.setMotorPwm(Speed_L);4 V) M/ W: U( d4 U) Y N
- Encoder_1.setMotorPwm(-Speed_R);
# j. G1 Z( N6 x! {6 z% I - }
複製代碼 8 B }# N% d# L3 Y( W8 g2 ]
MeLineFollowerArray.cpp
1 }+ K, j7 J& D) _5 }( q6 ^$ g- #include "MeLineFollowerArray.h"
2 d! v" V+ O+ n3 u' Q: I3 j - * \) h0 R9 e5 ^& ?! }9 R/ B
- #ifdef ME_PORT_DEFINED
. |* R7 ~+ b& |$ H" d - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)+ L: q* n3 r; \* Q& C+ L% U
- {* j: R B4 ^8 ~/ Z9 [
- 1 T) E8 M3 Z+ V, g
- }
* [. v/ F9 i) ]9 x7 @: ] - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
5 o8 ~) p1 l7 d: E0 D8 f: M - {2 Y4 l6 {- h* L0 j( R
- _DataPin = mePort[port].s2;
( j2 ]4 m$ o7 X+ R- f; ~7 P% @ - pinMode(_DataPin, OUTPUT); + z; s; \) J1 y) U
- digitalWrite(_DataPin, HIGH);' C% y/ o: v4 F4 q/ Z+ P
- }4 s4 R! q# Q+ ]5 ]% r
- #else // ME_PORT_DEFINED
8 G3 }+ y' _5 i5 |1 O7 J" E; M - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
9 \; C1 l& t" p! j8 ^( e - {
: x+ V7 l/ M0 t/ L$ _ C0 }# A: `7 i - _DataPin = pin;% u6 Y) f# O4 m8 v1 _& v- w- ~7 g, [
- pinMode(_DataPin, OUTPUT);
8 y& V- @0 ]! f. V( [. M; X# B: r - digitalWrite(_DataPin, HIGH);
* U* B6 D* |3 ]( J, U - }& l1 I r$ k! s1 E' x. U4 \
- #endif // ME_PORT_DEFINED$ }% n/ _. U' B+ r
# X+ n$ R8 i, w, F- D+ \7 y3 l2 _% l
- void MeLineFollowerArray::setpin(uint8_t pin)
7 l( C' K3 {2 X1 h* [% x - {
$ b( v$ X/ h6 c6 G9 G) f - _DataPin = pin;
4 d: w& W$ } o, a - pinMode(_DataPin, OUTPUT);
, Y1 P7 g4 L2 ?4 r6 v/ Y' F - digitalWrite(_DataPin, HIGH);- ^/ Q# L ] S7 u
- ! V4 W3 Q- k' U7 P9 x$ _5 ]% c9 x
- #ifdef ME_PORT_DEFINED
$ l4 S' l3 [7 x - s2 = pin;
9 c/ @* x# M4 A: ~) } - #endif" O# i2 z' K, D3 u9 c2 J. G* ?
- }
5 S3 k% Z/ b* v; l; E - 4 ^5 D/ r' |# |7 }7 \
- uint8_t MeLineFollowerArray::getValue()
6 s4 [. f/ x% E# J; n7 P/ p - {
, k0 }, J* G1 V# ~ - uint32_t LOW_level_read_time;
# Y. C% k. [4 S) G' m7 y - uint32_t HIGH_level_read_time;
8 z. Z/ ^% u* }5 z - uint32_t time_out_flag;
/ o& w; `7 [: p# z8 ~ - uint8_t Sensor_Data[3];
" o4 w. R! I4 L2 A5 s - static uint8_t old_data = 0xff;
- e1 F1 D* H1 `6 o! `- M
; _: F8 h8 o# g9 T5 h- pinMode(_DataPin, OUTPUT);
" e. X4 U* o2 ~' K# J/ O - digitalWrite(_DataPin, LOW);
# W+ F# h% z2 D _ J8 w2 W - delayMicroseconds(980);+ ~; m' ^) |, r9 S$ z' Q
- digitalWrite(_DataPin, HIGH);; r3 I, W1 H3 x `) g
- + K) E) A: H$ k# O. h& ^
- pinMode(_DataPin, INPUT_PULLUP);
" Z6 K" P/ {' U, e - delayMicroseconds(10);; k8 O! `+ U: z" V2 G
* T# l J9 M, P5 c7 I- time_out_flag = millis();
! ] y* [4 G8 A: L - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );$ ]- u/ W @ q7 C& H, {7 s# v
- % ?7 O- Q+ a* l: I4 |
- LOW_level_read_time = micros();
' T7 r' O( i6 r9 f - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out+ l1 q+ |$ J& R+ V4 V/ f' g3 M2 E+ p
- {) \& f' x1 `. C8 R- V% @) i
- return 0xff;
# x' Q: f) S% ~4 [8 P# b. h - }" T. [- j9 f* }1 V
- ; ~. S9 z0 M* j
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
4 u( R( j% B4 K4 Y7 C6 t, G. b3 K
" O+ G; s; h$ F) P; s C0 @- HIGH_level_read_time = micros();+ C- {9 M' M9 F2 c
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
- [) @8 g# ?* i* n9 d8 l - : n6 m( Z. J; X
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
* P4 l) F: X+ F - {
+ L, q8 Y. y0 K - return 0xff;7 C& b* |. \7 k3 ~
- }
/ T% ?) B, p' X7 w3 { - 4 c, O/ a- P& [# D4 x+ @% j8 u: P
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
2 s3 H+ ]" H& s. l" m# g - {; }2 x3 B) W1 ]2 ^ g
- return 0xff;3 X# b3 ?2 ^- w. P( t
- }
7 ]" n1 _) {6 v. b" R; @. G - * @) D! o* _- Y' o
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );$ H$ g0 U2 J u+ ~' K+ x( C
- LOW_level_read_time = micros();7 Q; [+ n$ ^2 `' J
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
9 g/ p, D, N8 G1 p- X/ D. U. W - ) Y) R/ F' |+ W5 R- [* w1 {
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out. |$ ], I: c3 U/ j- V, k8 }4 o$ E9 X" i
- {
! ?1 C+ A q. @. l J! H9 v" p+ j - return 0xff;
7 L" I" L' L% T9 C0 h; [ - }8 t) D, u# o5 ` Y. \6 q3 ]
- ' ?* r5 t6 s, {
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))! E9 N- h8 `. d# r- M
- {
, ?2 p% S# Y% {8 | - return 0xff;
) E2 R: p9 C5 O: o+ p - }
8 Z: g @# r: d. n* x
, G* U" q! b# S+ E' e( g- for(uint8_t k=0; k<3; k++)7 W2 {: E& B; U8 q9 M$ b7 D- I
- {
8 t" G( a; C9 k$ L+ k" D - Sensor_Data[k] = 0x00;
# p$ L. i ^% r( I1 h - ) j7 e! b- e V9 _
- for(uint8_t i=0;i<8;i++)" @- n5 S. ]$ B S
- {; S( E4 H' k9 E ^
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level2 @: o1 c% _7 x' `
- HIGH_level_read_time = micros(); A2 i6 j# P# a
- LOW_level_read_time = micros() - LOW_level_read_time;
6 N2 ? l3 S1 O! ?. Q
( V; B# k3 V% Q3 l1 x! u- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
3 H2 S; [7 K$ m - {. i7 W0 @$ Y' D, _! h* k
- return 0xff;/ m. Y) P( y3 q4 d' E8 I% q% y9 O
- }0 B8 w( N6 H2 V
# @6 |! i4 N, j& H* }4 B5 I- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
+ G: ^" W9 T% L% \; A A - LOW_level_read_time = micros();
1 _& i' m" g# r+ Q% b! `* K - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level+ f, l3 f5 @% b( i7 i
% e3 F: ]3 E( f7 n4 q# b- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
# V* N0 e7 C5 y9 r - {9 x9 t) d$ w# I" a) S
- Sensor_Data[k] |= (0x80 >> i);
0 a1 X% ]" R7 E7 D7 ~2 A- I - }
" I* m6 R% F0 N. _) y2 h& ` - else if(HIGH_level_read_time >= 100)% G9 D" a& k: m% q3 o: c: D& q8 t) {
- {
8 {: t7 S6 T3 D. w( Z3 w - return 0xff;
" N) P! @0 k* E: w; z7 d) m9 Z - }# b% e- [8 }& [4 X
- 4 U! E. m8 I/ n
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)2 o- j S. Z" J
- {) @& P2 L" q: k+ g' X
- return 0xff; q9 n# S5 x: D5 p
- }* K- j% ]3 P' J% r! P0 t+ N
- }0 |2 I5 \) R' n4 L) b! J
- }7 T8 H& y& q }7 n! ^% l
' \" ]# [! q( C4 U3 t1 d a A- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
, s6 z9 T! w$ N5 A0 R# o! _) \ - HIGH_level_read_time = micros();4 U7 B' p- P( y8 G3 c# `
- LOW_level_read_time = micros() - LOW_level_read_time;
" D1 I. c8 `" V2 {( A# z
8 x, m A; C' B- o- y4 m- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ). E2 p. w4 \( f
- {
: S; j' q& t b - return 0xff;
# F3 T) t' l, x- I- @/ Y3 {" D; ^" g - }
2 W" B& [" T, ~ _# T' L2 G$ [2 h1 U - 7 G- u C% h, ?( g- y# g- g* `
- pinMode(_DataPin, OUTPUT);
; a4 q8 F% I" B - digitalWrite(_DataPin, HIGH);
) ^" F# G k; T- \8 Z/ ? - 1 V/ m6 w2 P p/ `9 g& O- \% x3 t' z' A
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))9 z' a0 K$ i. l
- {
5 m4 t' ?9 F' \& o7 o+ ?1 o - old_data = Sensor_Data[0];1 U, {* N8 ]$ E/ ^5 `
- return Sensor_Data[0];: f; y" o0 g0 a) E
- }
# R! i+ d% r9 A& o - else
3 r6 N ]& r; N2 V3 O - {. P8 r/ @4 o( F
- return old_data;* G% Y8 I0 y( J) X2 A
- }
/ l6 ?9 `0 d9 y- V6 p9 M. t# ^4 m - }
9 E! G% _0 w8 J
複製代碼
3 v/ \0 S- M) S* q2 dMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
4 p7 B; V5 f# U U. L
- R/ Q" X0 ~! ?& N6 k) b( ~) e) C |
|