|
; A4 @3 O& A z7 n
- #include <Arduino.h>
: @4 Y5 K$ s( l4 u( w& ^ - #include <MeAuriga.h>3 n7 Y6 l6 F* e$ X& o7 v3 a
- #include "MeLineFollowerArray.h"
- ~6 ^6 P& d/ t: w" ?, G
) M+ n. H4 P/ t* ~5 E( ^# b7 I; T" I- MeEncoderOnBoard Encoder_1(SLOT1);+ L8 _. A) q; M! X- m- f
- MeEncoderOnBoard Encoder_2(SLOT2);
9 b/ D7 b* R% k' | - MeLightSensor lightsensor_1(12);8 n8 P. B J3 E4 C
- MeLightSensor lightsensor_2(11);4 T6 e4 U! f7 B9 D0 M
- MeBuzzer buzzer;& A( K, X$ m/ Q3 F1 K+ {0 f
- MeLineFollowerArray linefollower(PORT_6);5 n; j1 G7 O! g- s
# l7 U7 U4 H. D- #define Error1 1& P8 n+ f: |: F1 ~4 c
- #define Error2 2
! c, v' `9 O2 Q6 p - #define Error3 3
) q! s3 M2 q: G. B6 ~1 U
# m3 c4 O6 t! l! u' Y- #define Kp 15- Y' y! N1 S! h4 u* T! s. }; K0 P
- #define Ki 0.15
* N" Q5 Q7 b6 }6 e0 V. D1 \1 s - #define Kd 0.03
/ g5 ~* \( w4 {" c% ]* Z
$ ?& r: |! J- X, p( i, P8 v- uint8_t sData;' d0 n. o$ Q) U2 x
- uint8_t D1;
0 }$ ?, s2 c2 w9 b1 Q - uint8_t D2;5 ?* C4 j8 r0 J/ @
- uint8_t D3;
! B v2 h" g' i- K! H+ A% C. { - uint8_t D4;6 u. K1 @* R% }- \
- uint8_t D5;4 N; H# W/ m. f0 j' g
- uint8_t D6;; I: \ c+ z1 {! g- A& ?
* R* R- ?2 Y+ A" g7 V0 a- float previous_error = 0;" H9 s- U+ Z' P1 a* u8 q
- float integral = 0;
5 J; j! e) z8 ~! T( C% J - float derivative = 0;* V3 n* Y S" x& S' i% c" E- a6 O
- int Speed = 160;1 ~3 c1 x' V- y) W }
- float output;
! n1 L6 A/ S0 N |5 R7 W+ S
6 p ]0 t6 \ l/ |: i& b- byte Left;1 u0 k1 p( z! G& U
- 4 r( b2 ^8 m/ @/ s& F7 B: P# P
- void setup() ( L9 {; I( E. l! l" A, E) s
- {
* `$ r2 I E; g/ I. L- P - //Set PWM 8KHz
! v. K3 V. v' s% u5 v5 ~ - TCCR1A = _BV(WGM10);
6 y7 L3 Z5 m3 T7 d - TCCR1B = _BV(CS11) | _BV(WGM12);
. a9 W. \. g( b4 b - TCCR2A = _BV(WGM21) | _BV(WGM20);
+ ~& C' U; I. ^3 g - TCCR2B = _BV(CS21);: S$ m+ ?' ]) |& e5 C) [
- Serial.begin(9600);3 O4 X3 E b! U" T6 c
- buzzer.setpin(45);
9 R( o# B8 F! Q0 j. y% o3 [7 Y, U - }: Z' ]6 P' ?; c& r. f
- 2 C4 Q) |9 I/ K2 }5 l
- void loop(); b' ]7 l3 z* y3 z8 h5 [
- {2 E& u! `, I: f! x- j
- while(!((lightsensor_1.read()) < (10)));
/ _+ m6 a9 ]* e4 L& R - buzzer.tone(1047, 500);' D1 @( e& x3 ~/ K; T0 M
- delay(500);
7 H D1 C2 h4 e1 u. l2 v8 h - do
2 _4 i5 Y1 Q; P0 z - {
, X1 f+ ]% U, V) U - int Err = getErr();; Q9 m- `( S: p G/ ]
- if(D1 == 1)9 h3 e0 q- r1 B" m( b& p7 A
- {8 v: w0 H4 C8 b0 O
- Left = 1;
( b5 d A: C7 ` - }
* L) P/ V" d0 h/ P' H1 c, b - if(D6 == 1)
, }9 Z2 g" B5 u4 P) ^/ Z - {
' m" p5 u4 H- p2 u$ P6 a7 |! \ - Left = 0;
4 A S% l7 X! w - }
, D$ S6 R' @; n# Z5 u0 G) G - if(Err == 99)" y/ [/ b- {! H& {
- {. \. n/ u- [1 _3 d) b+ P% X
- if(Left == 1)
' Z8 z+ A& z0 u; [$ Z- `9 K - {
' x8 |4 {& U1 E- V - Speed -= 5;
. s1 M$ W8 E S2 S, T9 ]; M4 Z - moto(0,Speed); A7 f$ J5 B0 j) {
- do& r" N$ m M5 Q6 A
- {; H0 O5 P' j- m2 y; l
- Err = getErr();
3 W, n: s, l( r - }while((D1+D6) == 0);6 K# w3 v7 a2 I+ C. V1 j3 `
- } M, f0 ]2 r+ I
- else
9 X: p3 R: R8 y. H+ x - {
|3 c& W, V" a9 e - Speed -= 5;
9 \ M7 P8 k% _. o - moto(Speed,0);
, D2 f5 z5 q1 s v3 z - do7 R5 }8 c; |+ w) x
- {: s# F9 U) L8 X/ N* X
- Err = getErr();
% R* T, L: s4 g0 } - }while((D1+D6) == 0); Y% i7 D7 L" V& }2 [
- }' D+ F2 x4 d1 v/ b `
- }5 ?. X% a) ? Q9 C
- else3 o- @) H% V9 H) A! i5 Y6 j$ U
- {
0 d; e) a* r, }( a - if((Speed < 160) & (Err < 2)) Speed+=1;
" E9 X. L3 V4 s" E) d - if((Speed > 100) & (Err > 2)) Speed-=2;
( B- B2 [0 n& L, o% d0 R - integral = integral + Err;
# V W7 O0 ~+ P - derivative = Err - previous_error;
( x- u" Y# }( B. M6 M5 ^) G - output = Kp*Err + Ki*integral + Kd*derivative;
+ {. `; Z2 v0 d, r2 r! g - moto(int(Speed-output),int(Speed+output));( \2 I" j) p2 T) I8 B
- previous_error = Err;
( u# k! D# }2 ~0 V7 e. G2 n* f' W - }' ]- I- A6 P# u) U. C2 ?: M
- }while(!((lightsensor_2.read()) < (10)));( g" N4 U" J ~" x- z; m. p7 M
- moto(0,0);
$ C0 _: R7 B7 i - delay(500);
: S, r7 ~* n! C - buzzer.tone(262, 500);5 m. W% Q j7 N7 c' @
- }
* }9 D/ S. l7 g) N* k4 t+ Y - ' e3 i b) S" C& _8 E
- int getErr()
9 N+ s! {. [, C, p. b - {
, R4 D; m$ J7 s' f. K5 M2 [6 m" { - sData = linefollower.getValue();, L+ U* Q; \' Q& ~7 q- X
- D1 = ~(sData>>0)&1;( z6 s1 f/ C4 u) y
- D2 = ~(sData>>1)&1;% E: T2 z" k2 Y6 y ^5 Z% B* O1 x
- D3 = ~(sData>>2)&1;
L, b' ?5 ], C- `' ~ - D4 = ~(sData>>3)&1;
& S9 X# \* z- }: `+ a - D5 = ~(sData>>4)&1;0 }1 V# V3 a9 ^/ M) Z) S
- D6 = ~(sData>>5)&1;
" z1 w$ ^' f" l7 H6 d/ y* E" { - int downD = D1+D2+D3+D4+D5+D6;: k1 }9 j: O6 Z+ {
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);6 X/ R! Z" D0 e# F+ Q
- if(downD == 0)
: V: o; {: f" `0 b& d - {$ M3 q* Y7 T# O; m/ U& {! K
- return 99;
/ [* c, y4 ]2 W& w6 o - }
& e; R# Y/ _* a# J; Q - else$ a( R$ J* Z3 T0 P7 Y3 b
- {
* _8 w' f% } O9 \ - return upD/downD;
% Z# f- M! J7 N: s$ ?) S! z - }. {9 @0 O6 E9 T2 m3 A$ U
- }' f8 r0 e) M" F$ a$ V
x( i$ L' O/ N c* d; Q2 d- void moto(int Speed_L,int Speed_R)6 r$ Q1 R0 y) h2 i
- {
: h2 e" B. c# O - Encoder_2.setMotorPwm(Speed_L);$ l6 |8 c4 y+ @. E
- Encoder_1.setMotorPwm(-Speed_R);
8 i" s/ B O9 x - }
複製代碼 4 \2 H9 I, l4 H& h, x
MeLineFollowerArray.cpp
8 d7 C& B8 H9 l. z- #include "MeLineFollowerArray.h"
+ H3 ^% Y+ L/ [ - 1 J- ]* j* M* T' s
- #ifdef ME_PORT_DEFINED7 E/ V" \! u$ R H
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
6 s4 G* e0 j7 h+ \/ d- C- e/ v9 O4 @* E - {
7 ?! `% Z8 \. v' ~$ q5 k
- v5 r. ^% `. {) ]- }7 d$ e- h; T3 r& M6 H* }3 S- [$ x
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
6 c& h8 B \4 @% q* ] - {5 g0 Q, F& G% b5 j( o0 C
- _DataPin = mePort[port].s2;' R! y. O) Q8 B; s; I" m
- pinMode(_DataPin, OUTPUT);
3 H$ J/ ]8 ?$ m) E. O' O - digitalWrite(_DataPin, HIGH);
7 ^4 c$ p7 K: r4 b - }
8 k) ~& W- [! K# c. g8 U1 ]7 n7 U, l, n - #else // ME_PORT_DEFINED
: a( M- r6 R, O. N. j' \ c - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
4 D& v8 S$ Z* x; a - {4 y" t. t; n& w" J
- _DataPin = pin;& o9 C- U$ G# k/ ^
- pinMode(_DataPin, OUTPUT);
2 y0 w+ X, v+ U1 N* ~7 V+ n - digitalWrite(_DataPin, HIGH);: v. B# i$ ^" w; F! e7 [
- }
4 [: A. m) e( X2 T8 b+ m u3 W, j1 A - #endif // ME_PORT_DEFINED
; G$ d {$ j# R/ n0 ~+ R - 1 F3 W" q3 Z3 d# S- u/ D" h
- + \6 B0 Q; t0 Q
- void MeLineFollowerArray::setpin(uint8_t pin)" S. e( f2 C; W a2 r! b
- {, w, ~. x: T! e
- _DataPin = pin;; ?( L, H& L: j, U$ T" Z
- pinMode(_DataPin, OUTPUT); # b/ y% m/ e6 ]8 I- o8 S' t3 N9 W6 T: x
- digitalWrite(_DataPin, HIGH);$ a# k1 I% G, m$ C6 r
- 6 s* M2 Q4 ?+ ~' y3 V
- #ifdef ME_PORT_DEFINED3 o- \6 t" n* q; ~4 Q: @9 G1 y
- s2 = pin;: f. ~7 D! C& }- B# ~6 l% q" s+ d* \
- #endif5 q' Y% _& r+ ^, H1 W$ q
- }
) }/ b" H1 y; `# S: K" ~' Q' P
5 }8 a, U4 i5 a- uint8_t MeLineFollowerArray::getValue()
, W' t t; F4 d" ?. w+ Q$ | - {
* w* M+ S2 P5 u - uint32_t LOW_level_read_time;
+ ?% s& w5 E" Z- a% \ - uint32_t HIGH_level_read_time;
" S% j4 F4 P' X0 R6 }# h) ^ - uint32_t time_out_flag;7 K, H0 _. ]$ I! f( Y
- uint8_t Sensor_Data[3];
) O' H7 F! y- [% `3 } - static uint8_t old_data = 0xff;# @+ C0 d, c5 }* p7 N k; E+ ]
- 2 v9 M* \' o) X2 N
- pinMode(_DataPin, OUTPUT);
0 X7 S* A0 g% ^8 {# r0 H - digitalWrite(_DataPin, LOW);
: D( m" ?8 I, ?+ Y6 J) [ - delayMicroseconds(980);' ^7 p# H; ~2 b- [
- digitalWrite(_DataPin, HIGH);
$ |% Q: A B* h6 [( z0 a - 9 ~- q6 @8 y6 x9 L
- pinMode(_DataPin, INPUT_PULLUP);
2 G; x% s/ W5 Z% _% v - delayMicroseconds(10);' f% {" O3 j- ^+ s" C$ @* a
- $ W9 s* K: Q) x3 I6 q
- time_out_flag = millis();
0 H% }% K9 L' D, o# n% p0 h - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );+ a' s/ L) [3 B* Z* K/ S; G' l
- 0 ?7 N% `2 m+ o2 |' @" |
- LOW_level_read_time = micros();
) F2 G* O) Z7 E W6 y! `0 v( H - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- [" w- z% C% R: U, O: P) e
- {+ P, p# D, U- I7 C' a/ _: @
- return 0xff;
8 ~$ M& F* T% [) o( @ - }# o& |# P" g5 w5 K) R
- : s$ f+ n- n X) s8 k4 a
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );. F: P9 U- X4 E. m" V; j" s2 H
- % z$ V1 Z9 t$ M Q
- HIGH_level_read_time = micros();
* g6 n2 k% W( N; }* B' q- R - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
: o6 [, p: ?1 K - 0 p/ P8 c/ u# `" {9 e0 o
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
0 N6 a% Q$ f+ Y6 ?; U" [ - {
! {& R: `; G$ v0 E2 D$ e - return 0xff;; ]+ K. r% r! k1 T U
- }- N( F+ t& p6 Z2 T* n9 ^
, n! Z! X" k' W$ q- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110)), ^6 T& W- i0 N
- {
6 k K2 w* T9 p& B" }# M - return 0xff;
R$ @( x4 e: A* q - }
# P- n8 y' t. A/ y4 g, T
7 O5 u% q" @! i: c: ^) ?- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
$ B; v$ j/ h7 i4 p( F. k* ?# M: T - LOW_level_read_time = micros();
' x( i' F7 k+ d/ m+ {" T - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
3 I: _ U* A: Q4 W' l* `
# J' o, g b: M2 m% F" @- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
9 n/ ^, y8 M0 D6 X, u - {
$ q0 X! S) J4 U p - return 0xff;; r4 b9 Q) u* h. N3 L. S
- }) U" O& b& o' [
- V/ q( c1 O4 Y" u
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))" }5 Q( ~2 x! C$ Y9 c7 n
- {
: P) P8 a- v5 ^, m$ Z, A, a - return 0xff;6 S& E* u) E1 c% q
- }
- g0 }9 s! z Y9 \* C
, Q7 q+ X# `. I& I* ]" Q- for(uint8_t k=0; k<3; k++)
; {" ]$ o8 c2 D$ j3 i; J- \- I - {
( V8 T/ H f" { - Sensor_Data[k] = 0x00;2 _: C, S) }/ ]$ @& q
- - ^' y' n; w9 U$ G% o6 N
- for(uint8_t i=0;i<8;i++)
" i% q' o/ i7 r; Q5 E; b. F1 w - {
' G# Z5 n( ?2 [! x6 C - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
$ q) p) m/ }/ y; s4 V - HIGH_level_read_time = micros();
7 o" u r; b. D) F8 k! Q5 _ - LOW_level_read_time = micros() - LOW_level_read_time;) r3 t3 \% j" @) p4 m( R$ J$ b8 ?
7 V; g) P$ g# p2 C' C( ~- z2 {5 y4 B- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
' f& s( e0 C; ~! P, p: Z - {
4 M0 B" R) E5 I5 K0 x S# H - return 0xff;# H% l: q0 e$ m3 p$ W3 b! M
- }2 W- B) q$ w& J# b) f. J
- 9 F+ G7 b+ U1 r( K& r; f
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );& _, y- c8 R3 o3 @' h
- LOW_level_read_time = micros();
' M' F+ A& g3 ^( c - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level9 D- W" E* Z" V
" @: t9 c- J" K# U7 D- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 14 q9 m) C+ ^8 s1 Q1 Q* f+ ^; D6 d) o
- {7 Z/ ]: j/ T% K1 |& {
- Sensor_Data[k] |= (0x80 >> i);
# w2 \0 p- ~" i$ J - }0 f- I* H- [5 @# r
- else if(HIGH_level_read_time >= 100)
1 H; V& @) D! T' M" E$ e9 H - {! N1 h, l- a0 w& |. D6 p
- return 0xff;) C2 j2 b& G' ^- J
- }
6 x# c* f' N5 ^7 N
. {6 j4 K, T, X4 n4 l/ x- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
, E, G% o+ _$ O* t7 }" c - {
5 I+ ~2 k, T" D' X- ~9 C3 T0 V7 d6 l - return 0xff;9 p, h7 b) m3 x6 S) b5 W# q7 e! o
- }; I! C4 `- |$ T& T
- }
5 Y# }* ?& l: w8 L( s - }/ Y/ a1 S7 t' N" ]" t
\ B7 B1 w& h0 Z$ K/ @) |- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
6 Q& G3 [, r$ U! t/ {) M - HIGH_level_read_time = micros();
# r. B( `, g- Z7 N2 g' }5 ? - LOW_level_read_time = micros() - LOW_level_read_time;( {5 @2 V6 E6 ^- E: F0 ]
- - o2 ?) z4 D$ p2 d3 T( |
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
% y0 |% o' O. f, }) k - {. `9 [3 W& Z6 c
- return 0xff;- a5 Z6 p8 ~5 s% w
- }
" _3 a! e( J& n+ l0 e, ?9 J
. l, C6 u& C. D. e( O( J- L+ ~5 G- pinMode(_DataPin, OUTPUT);
9 O5 q3 d" x, o; [. C& q* R. U - digitalWrite(_DataPin, HIGH);# d4 ]$ g% ~2 N6 a- l! p& p+ e
- / e) J6 k$ @ h$ [: j/ h1 A/ q
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))$ B- {! c( Q; X* K& Z) f
- {
5 x2 A- B% \7 F2 S - old_data = Sensor_Data[0];' u6 g7 e9 ]0 H
- return Sensor_Data[0];. F4 R7 w2 r' O7 G3 E
- }8 h8 [- H# g3 A3 ~! O; U
- else# c7 R9 v* f+ d+ _) Y
- {
, [) y) a$ F! y& X. H( l - return old_data;) _$ Q7 Z# b5 ?! ~. ]: Y
- }
1 z4 @9 L1 Q$ e2 z - }
; X4 w8 P% Q) M# m0 X$ W& R" N
複製代碼
. h5 S& e" d* Q$ J- N$ wMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
# i$ [7 Y# h) h: s; g
' x2 ?5 Z0 v: G x# O V& }4 G" g |
|