|
|
3 H; ?6 F/ N' k4 \- #include <Arduino.h>
: O7 a+ J! x& X+ ]4 L. q& x - #include <MeAuriga.h>
; `$ d) v8 E8 k& a. j( b, N - #include "MeLineFollowerArray.h"
# `* \7 \7 K* j% d. N4 v# H" X - . v& j7 O0 `% P/ l/ }. f/ V- r
- MeEncoderOnBoard Encoder_1(SLOT1);
) Z; w9 C5 t" I9 [8 c7 c - MeEncoderOnBoard Encoder_2(SLOT2);
1 r9 v, m3 n; W6 J - MeLightSensor lightsensor_1(12);
& B5 m$ `! a) e - MeLightSensor lightsensor_2(11);, |( f1 w, U. S
- MeBuzzer buzzer;8 m, S3 y$ V; \. R8 O r+ ?* A1 m
- MeLineFollowerArray linefollower(PORT_6);8 `- w" \3 d: h0 G6 m1 Y/ S
( q3 @$ a- r! U) o' X- #define Error1 1
$ Y, k2 Q" ^' f* z0 z - #define Error2 2
% E: Z, A7 A2 w& Z9 s# H - #define Error3 3# @& F3 q: f* E# @ S, z; O
- * j; C! _1 ~ Y/ i
- #define Kp 15
2 N9 }! A: U! x3 W6 f& |$ M+ I - #define Ki 0.15' v) J- @) @/ {
- #define Kd 0.03
6 t! k/ [$ I' N( D! L/ ^5 {& j - : `. _; R6 i+ a
- uint8_t sData;
3 |, R, s* q! G! i - uint8_t D1;
0 I, }5 {3 _1 Y+ e+ ?% G - uint8_t D2;' r- n1 A9 A. T8 S9 M
- uint8_t D3;" L% h* Y$ x0 r6 h* r+ v* F0 @+ A- L& O7 a
- uint8_t D4;& s: d/ i& n4 v% |/ x
- uint8_t D5;
; t& [5 q# K. h7 j/ S! h" d - uint8_t D6;: A4 a2 O: b4 S8 I) D1 I7 P) R3 Z3 Q
- : ~7 p4 ~) `' l
- float previous_error = 0;4 S' D; m: S2 j2 a* N- B
- float integral = 0;
: D' m- `6 H6 d3 R* } - float derivative = 0;
+ a! |" x0 ^6 g) J) w - int Speed = 160;' m, u% S9 S' M) y4 G
- float output;
+ T# C3 }% n! _! `2 ~- y* H - ( L Y8 P) @9 V1 U1 W
- byte Left;$ H9 A2 }& f2 ~! e
) ]$ A$ E' q |5 t2 {" j( N3 R0 H. O6 s- void setup() $ J* ~! d& Z7 e) F9 {' B4 u) }/ |' a4 S2 ]
- { j9 u8 ^; [2 J# D/ h; P _ k
- //Set PWM 8KHz( T+ W! `- r/ F6 G3 V) C5 y
- TCCR1A = _BV(WGM10);8 C: ]! i5 M( y; ?( y: p& L4 n2 v
- TCCR1B = _BV(CS11) | _BV(WGM12);
1 C6 f0 Q- N7 E0 e. f% L - TCCR2A = _BV(WGM21) | _BV(WGM20);
7 U' N" m2 c! I2 P/ R5 G$ _ - TCCR2B = _BV(CS21);
/ n# V& w; i% @' W. o - Serial.begin(9600);
8 t6 d6 z1 N& o - buzzer.setpin(45);
( }5 n+ @# [0 O- | - }; H7 f8 Y5 b, S7 b
" [; |2 v2 I0 s! ]+ t% M- ?- void loop()3 H% R( {# D; f9 k. T9 w
- {3 n6 d) H( v' F. B) j& s' `
- while(!((lightsensor_1.read()) < (10)));1 Z0 c3 w5 ]: b3 r9 n2 ?8 S
- buzzer.tone(1047, 500);
" C6 A& ?) K4 U3 n; o+ U' U/ i - delay(500);
' l4 C. [8 D% J! w - do
3 g2 X ?8 T; D S( g; m! h8 d5 | - {
0 \" s& y0 d( ^ - int Err = getErr();7 H8 L$ L5 E0 l3 o% ?: d0 m s
- if(D1 == 1)0 s/ s' ~. E' J: {9 q
- {
/ u6 _* F: f0 Y i( s - Left = 1;
, K0 D7 g1 _) R9 c - }
5 U' b4 C" o& a; E0 M - if(D6 == 1): |+ O, X& O, S: h' K6 e
- {
: g. h# E' i- D1 Q, e/ M$ D - Left = 0;4 o$ V+ t1 C# ~0 R
- }) Y- w; o# i+ e' G% u8 j9 Q9 s8 s, t
- if(Err == 99)
9 ?/ b6 l1 Z L- T; @4 u' C* B6 i! d - {
! P) f: \9 Y* L2 i# V* e - if(Left == 1)
; T' [# y* f q |- D - {# Q4 H' ], y% d
- Speed -= 5; ?, Q+ G& U& K# [' y
- moto(0,Speed);
3 X |. N" [2 ~# q. E - do
2 L P: ]+ ^4 Y% ~' Y - {! d6 I0 q- C+ g' x v0 A9 n/ U0 I: M
- Err = getErr();4 R1 Q8 ~# ?: ~% ^+ L, z
- }while((D1+D6) == 0);
7 B5 v1 U) [+ l2 Q0 f% H- H - }
( {5 O0 Y1 n. c - else
! R& M, o3 a1 U4 _ - {
6 F, M3 B: ?: G- [0 M! U5 y9 ` - Speed -= 5;
1 e$ {. ~8 C; j, f - moto(Speed,0); $ }7 l3 s' {1 S# ^4 R( V7 G/ c. o
- do: L2 `# r5 Y9 t" @8 `6 k" n
- {" K( S! r' z, O6 |8 G$ k
- Err = getErr();
3 h5 I2 w9 N7 b/ E5 ^2 k7 S - }while((D1+D6) == 0);
% \; N. n: ?) ?. d# o - }
: b6 J3 n) l7 v( F$ X/ Q+ H+ q - }
, J' G; M/ I/ x( W- R3 s - else
! P- _4 `) p0 a8 S: ] - {& \4 P/ J* e( r F4 n! p! q% d+ ^
- if((Speed < 160) & (Err < 2)) Speed+=1; ! i6 ]% ~; V+ A$ d
- if((Speed > 100) & (Err > 2)) Speed-=2;
- ~9 i3 M- q7 w6 c) J - integral = integral + Err;
1 j Y2 K6 d' R& F& P5 x - derivative = Err - previous_error; q/ N; X3 p; u, G- T$ c
- output = Kp*Err + Ki*integral + Kd*derivative;. i7 u. r9 L! a& A4 |
- moto(int(Speed-output),int(Speed+output));
8 j6 ^4 m+ l$ G2 b - previous_error = Err;
1 c" Q4 E* C9 j) L1 I. m! L - }
; }' ~! f7 ]+ s* A, }" F1 p2 y - }while(!((lightsensor_2.read()) < (10)));
" J' }- C. }# L1 r! D f4 V4 u - moto(0,0);
% p7 m* @+ d. z- S1 y/ c! P - delay(500);, d$ U. V5 W+ `6 A; a
- buzzer.tone(262, 500);
) G: z/ |# z# g+ v4 M - }
0 y Y1 M) o! k( |: B, p
: s9 d$ b* v6 X. [- int getErr()
, r5 K0 e% @' e( z3 n6 D+ ~ - {
S6 o; f* V$ B, f - sData = linefollower.getValue();! Z2 W# r3 x g) c+ @' s6 C
- D1 = ~(sData>>0)&1;0 Z; t' d+ Z7 j8 T8 ~* s
- D2 = ~(sData>>1)&1;
; J5 |# C! @; P8 s& G/ L/ {( A, Y1 F - D3 = ~(sData>>2)&1;( N' Y$ _, d) P
- D4 = ~(sData>>3)&1;
' T* s" K) e" O/ P. v0 \" B! W; ^ - D5 = ~(sData>>4)&1;
. |0 h* f7 @/ w3 ] N3 i# G - D6 = ~(sData>>5)&1;
* \4 J- ^& y5 [ - int downD = D1+D2+D3+D4+D5+D6;
& l2 V- \; V9 e% k, G3 ?8 I - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);# f/ R8 I6 o9 s$ B
- if(downD == 0), ` _$ K: O9 g6 o. }: r
- {
% z7 q0 Y0 h% P - return 99;+ [/ ~9 H. }( `( o
- }
2 B$ }/ B) y6 n& O - else
S9 A: k2 e! o7 r f, z2 D- I4 Y& u1 T - {. @/ [' d2 @! V1 ~: o
- return upD/downD;$ v4 }0 m) g2 A. e' }7 G+ J
- }
) B6 a! l4 g0 J( H - }
2 F, R3 B( N N) z8 v! ?4 s j - : i F; G; [( _" p0 G) n/ n
- void moto(int Speed_L,int Speed_R)8 b3 j+ D7 y& R9 G
- {9 ? H1 [' N, L
- Encoder_2.setMotorPwm(Speed_L);. _' P7 }* R1 z, N
- Encoder_1.setMotorPwm(-Speed_R);
. {2 M* a' p+ t8 \) _5 u - }
複製代碼
/ ?% H7 G9 \! x$ H* N/ s( I3 y! mMeLineFollowerArray.cpp
' m: Z4 L+ a5 e- #include "MeLineFollowerArray.h"
+ Q- g* A. s7 W4 M! w
# l8 A3 p' L, o7 |/ @4 i& k9 H; L- #ifdef ME_PORT_DEFINED9 [! {* @) R% N
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)- K7 ^9 I* [, B8 y% o+ g
- {
; y2 z1 ]8 R/ s% k
' V; C3 Z; p0 q- Z4 H+ O+ \- }- r7 B9 M* ?0 _5 k$ z* X& J
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)2 p0 @9 j# Z( h! x! E
- {# T! B/ A5 I" l" H9 g
- _DataPin = mePort[port].s2;+ _7 E2 T( a& n
- pinMode(_DataPin, OUTPUT);
8 |* v2 E( t. i9 Y - digitalWrite(_DataPin, HIGH);
$ ~/ a! ^0 R- [# f3 d, d& g - }5 Z$ E, |' `/ i3 @
- #else // ME_PORT_DEFINED6 d* n! _3 s7 N8 [' k* Z3 p# b! W
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
" B! B4 ~4 t4 Q, t3 w - {8 _: g( ?5 b z8 G% W, @
- _DataPin = pin;2 C2 W9 I! z2 G0 J6 l5 q1 ^4 c
- pinMode(_DataPin, OUTPUT); " O+ X. c" y% P7 F- ]# l
- digitalWrite(_DataPin, HIGH);
9 l1 U. k) x" N+ y& N2 ?4 h- i - }8 |0 N2 m* L @- \0 ]# b
- #endif // ME_PORT_DEFINED9 r8 ]0 s j3 S e0 K) Y
- 7 ]+ m0 O4 y" I0 n
Y, W! f q, l) B- void MeLineFollowerArray::setpin(uint8_t pin)
) K* P- X3 p) t# _: A - {
$ G9 {9 I! O' |2 ~) N O9 U) p - _DataPin = pin;3 {6 R) g8 B' o! U3 E' B) y ?
- pinMode(_DataPin, OUTPUT); . E* C4 n. d: Z9 n5 J7 D3 X2 X* m
- digitalWrite(_DataPin, HIGH);
3 l- @$ q! r- z
$ X1 A: B s& I% _ M. o' I5 y! r- #ifdef ME_PORT_DEFINED
( }( F3 V- I K/ z% T# d# C - s2 = pin;
/ u. J8 J! I: A0 g9 D - #endif
! Q/ Y* f# d7 r" _ - }2 r8 M/ \% Z9 [- ^3 N4 S1 l
- ( x' F, i( j# U7 w' Y
- uint8_t MeLineFollowerArray::getValue()
# \& m o3 L; |- |9 B8 f. ?4 U - {
9 K, f2 q4 g/ f) \% A: m0 }: T8 V - uint32_t LOW_level_read_time;; E0 P( H5 t2 M+ |& I) k# w5 x6 s
- uint32_t HIGH_level_read_time;
* y8 t, A! v ?, H8 ] - uint32_t time_out_flag;
9 b0 s9 q0 r$ u$ x" _: v7 V - uint8_t Sensor_Data[3];
) O& {4 {; m3 m: J - static uint8_t old_data = 0xff;
# k5 O9 E' S- l% }( i" X, t8 x
; E3 D- a& d! V8 s- pinMode(_DataPin, OUTPUT);
# _% @9 U3 ^4 w4 B0 |- L: Y - digitalWrite(_DataPin, LOW);. n8 W/ j4 z+ {% N: B
- delayMicroseconds(980);; R; T; {+ W/ q
- digitalWrite(_DataPin, HIGH);+ J' m9 i! y; F" h
- " I% f% S$ _0 a# A- _; [
- pinMode(_DataPin, INPUT_PULLUP);$ C$ i# `1 K9 O3 [4 P- G
- delayMicroseconds(10);' R0 R& j5 Y" e* W- P
- ( L( q1 ]/ R7 m, o7 ^ A9 J
- time_out_flag = millis();
( E- w* d0 w. k& T* M0 X - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );' \4 U5 i- e& M, h+ a
- 9 J) X- F2 t+ M6 E
- LOW_level_read_time = micros();( L/ Q* h" i, m6 `; z
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out& \0 A; D' U& Z, Q
- {) q$ f; g9 Y" O
- return 0xff;
' V5 W; R( Q: @5 m/ C6 d7 R) ~$ _ - }
& n& _$ e$ F" o& q2 J - ) ]. [" Z s, {
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 g. G2 ]$ ^+ n3 D' H( V7 r8 M& `
- 7 b$ Y5 n+ X1 M6 K' B n
- HIGH_level_read_time = micros();
K5 p2 q: ~( u: n/ W$ Z+ U - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
, p' _( V) V5 `' v
6 y0 q: S4 A3 V$ S2 w9 n9 E- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
b1 `+ j0 K& b8 g0 R3 U: U$ Z3 w - {
: q, W! w& G5 h3 H% S' p2 t, o - return 0xff;
5 X. ^1 D+ f) c' ? - }2 @' `5 C# o/ P z- Y1 G$ C
- : g4 V7 i% M% x! k! w/ T+ J
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110)). ]4 A5 L: q. d) f
- {
. e: C( B: I1 @3 J1 e - return 0xff;3 V2 S( Y; {9 S2 ?) h
- }, l7 ~' f; R. W2 W
; g7 }- I1 E x# Y- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );- L" L S2 D7 m6 D6 T$ a
- LOW_level_read_time = micros();! Y$ y; v. R9 n4 b
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level9 X* m* |) |/ c, O0 H
- $ Z" j. ~' q* ~7 e# ^$ m
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out# O' q6 R- X7 Y6 w; j5 y
- {
3 }( t3 s& T. P- n - return 0xff;3 O$ c% v) @# U0 G% U+ q7 z1 s
- }$ }& y4 T2 E- J
- 2 M$ z+ t; K* @9 D
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
( T' M, X7 K% Z& Y9 b7 _1 r4 [1 u$ F2 z - {
- D6 z$ r# c. v% g4 y7 y0 I1 L - return 0xff;
Z7 B D( f" ~$ F/ |2 M - }# x6 F1 d0 r% J. F
- * _/ t4 e/ \' H/ e Y6 {2 p+ e
- for(uint8_t k=0; k<3; k++)
' N6 u5 _# U5 N6 ~" y/ ? - {1 l3 [; z! @9 D1 R
- Sensor_Data[k] = 0x00;
" N% i( ~% c1 n. Y* o L y2 O - " J! f# G8 k1 G3 h* l- `+ s
- for(uint8_t i=0;i<8;i++)% ^! q" r' `( d4 l9 N
- {
+ ^ z7 j' M1 h8 ?. j - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level N8 P( c4 s( m9 z
- HIGH_level_read_time = micros();
% K6 t/ [; W, e, R/ V - LOW_level_read_time = micros() - LOW_level_read_time;
5 c" b1 D( k- k' B6 } - : _( c E/ E0 H! X8 D
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
5 |7 m3 z9 l% F' `) b- U - {
- b+ Q& k3 ~5 i2 M r) M - return 0xff;$ U, f8 }" ^" A, A
- }
) P# a1 h9 @7 n( V1 u* F
' W4 `: I5 ]! o- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
* [6 W' D- L8 m; [ p/ R! s - LOW_level_read_time = micros();
5 A" n: q) _7 x# J4 p( T - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level* n) E: D/ a1 m6 ? U
- ) f9 _' @4 t: k; _# i
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
/ B$ x; R2 C9 z) ]9 K4 [) A$ p8 H - {3 B6 _% S E! F+ s1 V) y
- Sensor_Data[k] |= (0x80 >> i);9 J& P! g d" N: X( m. S* k$ Q
- }
/ w' W2 E6 c" y o - else if(HIGH_level_read_time >= 100)( F; N3 }8 w5 m" W; I, k5 D) O9 ?
- {
% t4 i5 A' N/ C: }* w9 z- a - return 0xff;
! { u; n% y/ |& D1 s - }
: l; ~$ M) x$ c+ r) k- S- t
, w6 u# d8 X2 ]3 ]# B- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)+ g3 g4 r4 @9 { S+ L- d$ g( @
- {* a) d3 b$ O# J$ d ^- P, J+ m0 f0 G
- return 0xff;
+ Y" B( j- G+ ~4 j e6 B+ f - }; V p# o9 J2 }1 {4 C8 e# I- q
- }, g' {/ Q0 |+ h% R0 {+ q1 a+ m# w
- }( I; `! T8 f! h7 s
: I& S5 K$ b% i& X) f: k- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level. T8 n0 r# H6 N' X
- HIGH_level_read_time = micros();3 u& |# l+ x* K2 k6 A8 j
- LOW_level_read_time = micros() - LOW_level_read_time;
' q- l8 |& ]. f. Q0 b! [/ q' R5 r
7 ^+ z% u/ D, Z+ Y$ v- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )3 L+ ]+ Y# L2 Q
- {; u- U% `' m# P- g
- return 0xff;; d p1 M1 ~9 r. j* B' t: X3 S$ _
- }
" l8 P9 \0 T4 d3 ?: l
5 r- ^" P/ O8 q+ ^2 r- pinMode(_DataPin, OUTPUT);8 q! a* o, Q! f9 ], t6 l& a) B3 t2 ~
- digitalWrite(_DataPin, HIGH);
; W7 d6 h S7 f& _- p* O X$ g. a - $ [+ `5 S) l) d( R
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
! G3 Y: \" H* H. h6 }1 | - {) R8 j2 F/ X4 R* D9 Q( Q3 M
- old_data = Sensor_Data[0];5 ?) Z, V8 E$ S: F# V' q( q
- return Sensor_Data[0];
( H6 d: x, O0 Z2 u1 U9 H - }
6 v( ?8 A' T! W( W0 O8 f - else
. a0 k5 Y5 i! p9 [: W - {7 z9 i5 m) q8 \' X5 |# Y+ e, o5 z
- return old_data;! c5 e5 V" Q* g' q
- }# O: a- T; ~3 `: U
- }
" }/ [! y6 p4 K
複製代碼 ; T4 `( g+ n" {1 a3 e& s$ s% m
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
. ~& p3 R N1 T* e7 o U
0 P2 g. h% w( Y: l: P2 i* {8 G. c0 t1 R2 C |
|