; b w- P6 [9 P4 k
#include <Arduino.h>) E3 R( d8 v$ ~; P
#include <MeAuriga.h>3 \6 m7 M# l( y+ X9 f" p
#include "MeLineFollowerArray.h"
8 d% X; {) G0 \! i: t
W) Z0 `1 R% ^5 u' k3 ] MeEncoderOnBoard Encoder_1(SLOT1);
+ t/ u" b2 h' m5 h MeEncoderOnBoard Encoder_2(SLOT2);4 @$ c* U) x) |7 Z/ M4 W" h
MeLightSensor lightsensor_1(12);
8 x6 o3 N3 G8 D. f5 ` MeLightSensor lightsensor_2(11);
3 _+ _. W0 O$ L& }5 m! [/ t& r8 W MeBuzzer buzzer;
1 Q- x5 O5 d! [4 D* M3 N: k! M MeLineFollowerArray linefollower(PORT_6);
, L& t7 H0 p, F0 F" d7 o / Q% I v3 U, T
#define Error1 10 R: H$ o6 W, t& S( v" t
#define Error2 2
- J9 O( S0 `1 T9 P #define Error3 3
7 U4 K# u8 q) I5 y2 p7 r " a6 U% m) s$ Y) |4 W; Z: {- l
#define Kp 152 P5 X6 }# j y2 r) V! a& e6 R
#define Ki 0.15
& j6 O+ M; Z* Y; P2 s8 @ #define Kd 0.037 P @: h; w: o
8 A6 x9 y* j5 v Y
uint8_t sData;% I3 O4 f1 d5 N. z) @; E4 i$ [7 u
uint8_t D1;6 T" y! t5 E+ H4 t
uint8_t D2;( _/ w1 P5 D- g
uint8_t D3;& h N1 F- z# |% P
uint8_t D4;
# Z0 E! V V* F; G; O1 t uint8_t D5;
% ^9 U/ }7 n. }% ]; ~) [ uint8_t D6;
6 @8 I' [" Z/ a
: |; h+ r4 I* j. ]9 v9 @ float previous_error = 0;$ z- J4 Y% k# q/ c. E2 \ u0 }8 I
float integral = 0;/ c0 s( H3 \# x* T- l. w5 z
float derivative = 0;
! b7 G+ b2 {6 W. u! }# E& f int Speed = 160;
) c5 s, ], X- e/ E- ` float output;
- l8 P$ N& Z! K* G6 F " Z) _( Q- y2 v) @
byte Left;
# F5 [" u3 a3 s" C
( m7 G' F( c C" T2 _ M1 ~ void setup()
! s1 A: A6 K% G0 Z1 ?3 s5 L! A5 n {& M$ a0 O3 ` g# i7 l
//Set PWM 8KHz9 G6 v. F/ Z8 p
TCCR1A = _BV(WGM10);
$ l x, @1 r; M, B TCCR1B = _BV(CS11) | _BV(WGM12);2 ]& p* G& v+ a1 Y) o: B
TCCR2A = _BV(WGM21) | _BV(WGM20);
7 I) A# m$ b Y6 ]% x) T& { TCCR2B = _BV(CS21);
7 e" g' P2 E2 M( G Serial.begin(9600);
& v B+ p9 Z: ]" ?0 @5 F4 Z8 o buzzer.setpin(45);6 S! `5 D5 e0 @0 J8 e/ c
}
4 H5 ?+ Q: ]+ Y% A3 i* H . s# u" | U- i0 W. {1 D
void loop()2 B6 P) R4 ~7 ~3 T6 i2 A0 M1 s: B
{
( n+ L% e! O$ a9 x: u# w, p: m while(!((lightsensor_1.read()) < (10)));
2 N1 h- D/ q; c buzzer.tone(1047, 500);& l$ z: Y- k$ N2 g
delay(500);
+ }3 w5 [3 |( v do
& ~' r6 _3 R! T. i {
! m' h/ \9 d4 D. ~8 @ Y int Err = getErr();
9 @0 Q, i1 T& X7 j0 u1 q4 S if(D1 == 1)
5 |) ?+ G( l# }8 t {
, K' C& J1 W1 l7 |# c& ? Left = 1;4 u, g$ v6 |6 C Z. m8 q3 n
}
/ o! W* `; d# n- U) t+ s2 A if(D6 == 1)
& z. i8 m4 f3 b8 `) T O: L {
/ L' n* s- o% v6 j Left = 0;
. |7 N5 T! j' m2 B& k }
- F" ~1 J# u2 I2 c! e% g T if(Err == 99)' B1 {: A/ F1 c
{( P* N6 C& v% P1 C, n- B
if(Left == 1)
& A! ]2 t. c# s {
, A! H6 g/ h; G: o+ J$ k Speed -= 5;+ ?% R' \' N, ^1 C9 E
moto(0,Speed);
1 l/ A) t) M1 j' _9 u3 ]& n do& [ s. f; _& M; z# ~
{3 {3 U1 ]" L8 i" B5 C
Err = getErr();
' f4 H. f7 @# a0 D }while((D1+D6) == 0);
7 j$ a, r; @$ [+ ]8 A }
7 A7 \5 d y7 ? else% t8 { r% E" t
{
/ S8 m; ?* B5 ]) p Speed -= 5;& Z, e( o9 X, s* X8 S
moto(Speed,0);
( p/ j7 I% y! r: a; J do9 j# X y( n. k7 ` Y* t. I
{5 ~# B( R5 H& H7 @" y) G3 o: h' A Y
Err = getErr();: ~* x3 }. V9 s: l7 R# u
}while((D1+D6) == 0);
( f5 j6 ^8 ~! |$ \ }- i# W6 I. c2 S8 I$ Z2 F; I8 h
}% s1 @" c% e# Y' f2 \- F% S
else# o: `* A6 C/ n8 g% Q2 @% Y/ _
{. ?% @" W; p0 ]- w6 l* n E; \' e
if((Speed < 160) & (Err < 2)) Speed+=1;
6 n: ^/ d. K) S, n5 d0 l# j if((Speed > 100) & (Err > 2)) Speed-=2;
) m8 l: [9 s: F4 k) _: ^ integral = integral + Err;$ ~. Z: O0 |- }& W
derivative = Err - previous_error;3 j( k; B& F$ Z) D# }3 J
output = Kp*Err + Ki*integral + Kd*derivative;3 T# t1 D* c) e$ j3 S- P
moto(int(Speed-output),int(Speed+output));
5 |' |5 O$ Y* p% C previous_error = Err;
" M1 T }6 C4 K1 I }) L4 W$ D* a& h4 T' o
}while(!((lightsensor_2.read()) < (10)));1 O# `- C' c4 m8 g5 S
moto(0,0);
: x# m& h% {( g delay(500);6 q }! ~2 L! u- A0 z# _: ^
buzzer.tone(262, 500);
% }$ X& W) L! j1 L }
8 |/ w9 ?9 c2 J / l0 ~ g5 B& y% o/ l
int getErr()8 p7 c5 w& p. n% [
{
; P+ T" F* _* X9 ] sData = linefollower.getValue();
) s u, m! j0 v8 m D0 L D1 = ~(sData>>0)&1; } {8 ~6 ]. G
D2 = ~(sData>>1)&1;
" ]0 l1 c2 m. a5 e3 q D3 = ~(sData>>2)&1;( P3 `, I$ U5 t2 n S- T
D4 = ~(sData>>3)&1;
! n* [6 ~* [ D6 _- q# g0 V3 z D5 = ~(sData>>4)&1;
; b3 g! O( R' k0 n. n( c D6 = ~(sData>>5)&1;. ~! A+ A3 x9 W9 w
int downD = D1+D2+D3+D4+D5+D6;! ~, S, p9 Q( L4 A/ O7 L
int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
8 x+ w3 M: K- {# N { if(downD == 0)
5 _5 @+ F2 r8 [/ s! y {
# ]+ x7 u: K" } return 99;
N& d& e3 Q- \8 P' e1 Z4 B }
* ~" w$ z0 e9 J else
; C! |; H3 A" Z& | k: p" Q5 t {
( x4 ]( X8 A. j. |/ k' ~, G/ Y3 z return upD/downD;( z' G! D5 z% m8 v- Z7 k) ?
}. y' ]- e4 {9 K4 {
}/ a$ d- P- B2 I+ G& z
# @& B* t& c' {) f0 @; @ void moto(int Speed_L,int Speed_R)/ Z' S. z" a- ]% O. l
{, m+ a8 L s8 j+ v2 P4 y9 r, ~
Encoder_2.setMotorPwm(Speed_L);9 w% m; [" }" w2 I* L& b6 T
Encoder_1.setMotorPwm(-Speed_R);6 f# u& {! x2 Q1 D, H6 M0 ^0 H
} 複製代碼 - l( b' ` e# x3 L# i+ g+ ?; V
MeLineFollowerArray.cpp
% m t' m, U% f! {* z) f7 u #include "MeLineFollowerArray.h"3 w3 ?; K: G$ E, ^2 ]; c
8 _7 ]' @- Q2 h) |# S #ifdef ME_PORT_DEFINED1 i* ]0 z& g C% @0 u: @
MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
5 ]8 C$ d) v8 I% {6 s7 x8 V0 ~0 g {8 A- e6 c* w; F
! y" `9 u2 [8 c( t/ f* R
}
. t/ o/ x* k( l MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
6 j5 S4 g h8 I0 H+ V. r3 @9 l {) J+ }4 W- U9 K7 D( M( [
_DataPin = mePort[port].s2;
% s3 p. S. R6 _% a9 B7 N5 i$ M/ f+ _0 b pinMode(_DataPin, OUTPUT); # `% ]5 S% t( Y \$ ^3 b4 }" U
digitalWrite(_DataPin, HIGH);5 v: r- f( l$ j+ P" j0 ?
}$ W3 y' m+ f- A) V
#else // ME_PORT_DEFINED
; ^7 x; S6 d7 C9 w6 |* i) B MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)# D1 q, n( [9 v1 A
{/ K9 {# k E* I5 H
_DataPin = pin;
4 y. i: r- \: J; O" r pinMode(_DataPin, OUTPUT);
R7 w; Y3 U3 i4 @ digitalWrite(_DataPin, HIGH);
2 V/ v5 \5 e7 H! b6 Q. Y! G4 W }- [4 Z' }! }$ m5 T7 Z) C4 `
#endif // ME_PORT_DEFINED
' {. u; M- n E$ A5 v* X : X- m4 h2 l7 B5 R' M
* H5 {0 p Q7 s' r( o8 y
void MeLineFollowerArray::setpin(uint8_t pin). W) X# [5 T" S
{
0 v/ U9 G: X( k+ C+ g _DataPin = pin;' _$ e- R, n3 s8 T* c, r9 j; }; W
pinMode(_DataPin, OUTPUT); ( |. W2 n7 s6 F" E$ k
digitalWrite(_DataPin, HIGH);
9 K% c' k% |7 I+ k% ]) W# M
) V: m9 @/ V9 z #ifdef ME_PORT_DEFINED
3 K; ^ G# a; C0 f7 B3 h s2 = pin; {7 v- ]3 T! B- `# @. M. p- g
#endif
9 E M, O8 Y, y" u }
5 h- k: ?6 s* k {; b / T' F4 J# e6 e8 o1 D
uint8_t MeLineFollowerArray::getValue()
' h3 g! d7 D) t/ X$ x. `9 g) c {
. T) O+ I( \4 N4 y! m% w uint32_t LOW_level_read_time;2 f% m) a3 f: e# e1 ]
uint32_t HIGH_level_read_time;
9 D+ V" s9 K& ?5 ? uint32_t time_out_flag;$ g, V7 r6 H6 b3 \& [- u
uint8_t Sensor_Data[3];7 V4 ?! C3 R1 u
static uint8_t old_data = 0xff;' I- O! _, F0 e$ y7 _) q1 J3 I
+ N, b* s! P/ _; {& i$ \
pinMode(_DataPin, OUTPUT);
. j5 t! r$ D0 T8 O. p( b digitalWrite(_DataPin, LOW);
1 Q- F1 T: ^$ H7 a3 I" \7 i delayMicroseconds(980); W" ~0 U9 Z7 d& |% Z. G
digitalWrite(_DataPin, HIGH);: C% Y4 b' c$ ?5 `0 b" ^/ {
. B& i& H* z+ }" k: f0 I
pinMode(_DataPin, INPUT_PULLUP);7 Y; H+ S( t7 m! u& F8 s
delayMicroseconds(10);2 V8 j9 k0 q% m) F9 k( n! s
g a. m1 P, x1 N4 r# ^ time_out_flag = millis();
% b& b4 w: M7 w while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( i) D" l7 I# {- z- b5 n/ R/ k/ h
3 H9 s& n. t" _7 M2 u2 v) Z LOW_level_read_time = micros();
k9 v7 S1 c, H2 S- i- i5 p if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out0 C# _$ Y" N1 R: r( s9 B
{7 h+ s4 M ~) `! ~- F
return 0xff;
$ }/ B& u& M' f! n3 S }
9 O' j, j4 ~* L5 R) k0 g : M8 Q c$ K0 H# v
while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
$ g; e% R' D3 W+ F! W 6 p c, S) `+ z+ G! e) l8 F! o
HIGH_level_read_time = micros();! o& r1 _2 a8 U, a
LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level: _) {0 ? U8 E( P9 r' ~
* ^: V4 W$ Z4 `- T' M7 H if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out( Z1 s) E, F9 V( {& V
{+ Q5 N2 w1 y) `2 p y& B0 l# |2 s) q; P- u
return 0xff;
4 x7 i0 q9 o0 U$ O4 C1 o# K }
; ~+ f; C8 \3 r4 h1 F* c4 d5 ]+ [# C
( x: s; Y" _4 H* P, g if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
" p3 H8 n9 N! i. E+ b4 F" d {& X) N! ?" o* ^; d; o8 N) O
return 0xff;1 D; \1 I+ U7 Z$ q
}
7 P$ Z9 d* Z" H v" F
/ z8 P2 s6 f2 X" t! B while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
X1 b1 k& I# u7 b LOW_level_read_time = micros();9 @* H3 M' t6 d
HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level: `) s. P1 Q* u: u: }4 C$ r5 M0 w
: K# l% g V% M
if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out4 N( c1 S4 @% O- w4 r
{' H1 K7 R. V2 q, E7 \ w, q0 }- v
return 0xff;; D+ Q& c. W4 E6 V
}! a1 G! _3 s( C- N3 C5 H
9 ?1 z8 [! ?- j if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
! J0 x1 p7 j( Z( J$ }, T {; m; V4 `# D# {7 q k
return 0xff;
( O' Z1 R- d# X* y }) u( H5 y0 b+ j/ }8 s
$ z) P7 e, |2 Z0 F9 ^- O$ A* q for(uint8_t k=0; k<3; k++)) I' R: U$ h& I
{8 j' T& T, ~! F: V% ]/ e) V
Sensor_Data[k] = 0x00;
* P+ S$ }0 t, o3 T/ b! C 3 L0 Y1 s* u/ b- S+ V1 u
for(uint8_t i=0;i<8;i++)% {2 W Q t s, B: h# X
{( |; u, W- G$ y3 _: m! a+ S; a( e
while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
- G7 @2 d1 e, W7 |& c5 @ HIGH_level_read_time = micros();
6 N+ f9 {. X! { G LOW_level_read_time = micros() - LOW_level_read_time;! }; s( w2 i! s( m: f3 q
~4 k& n/ l$ U2 F2 z$ N; V
if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
6 f# e5 V% R' \1 s {
3 n h+ [0 c# E" u5 f return 0xff;7 n3 C# P- t4 `% @5 |' T
}0 p* V$ B' p( w9 }9 K) [9 P
- P. |' d3 N) k$ n
while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );$ ^4 y# y7 L4 i! d3 ?& m
LOW_level_read_time = micros();( q) k- f0 w/ @
HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
7 T. f& K$ D( ?- P9 S- V6 V: h+ E7 e + }2 z, r% Z! U H) z
if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 14 p% K: V5 L% n. t7 `
{5 D' S1 Z0 ^/ }/ K+ o- n
Sensor_Data[k] |= (0x80 >> i);
& x' N/ z/ w6 | j0 B }$ o; {1 u. @0 d% T! \- h m
else if(HIGH_level_read_time >= 100)
2 x& M7 E A# I; q% K* { {. L7 J1 u; c' d
return 0xff;# _; H. d$ D) P
}
: A. t7 J( i" H8 K: @
" {; t+ ]& J9 o2 s% Z8 j/ p if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)8 J& U2 s9 {6 Q$ Y, [1 e# \
{ ]5 ~* G7 u! a
return 0xff;
! E M$ Q! u! B: q Y/ f. y }
3 K" v1 g& l" L1 }; y9 _9 | }% N: s v6 c; T* s2 s' n& K
}
& E* h. f! b- G. ~
# d+ ^' z& p5 G while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level+ E F% {. c5 G( l5 Z
HIGH_level_read_time = micros();
% V# ]# N$ v+ z& R) g; t LOW_level_read_time = micros() - LOW_level_read_time; Y0 s( a& ?* F% B5 T. W
$ ], l1 d& ]# I, {& K
if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )% h) L/ {( \, g' m! w/ _5 [0 b8 l
{
. l8 H. q- e# b& `) L% l" b return 0xff;6 Y+ {- a+ F2 ^ }( b+ b
}
9 D8 k( R- j( H
1 u, |, `1 j; D# f8 e2 z pinMode(_DataPin, OUTPUT);
0 J+ D$ H4 V; m1 B2 D7 S5 U. W& Z digitalWrite(_DataPin, HIGH);
: H& r& f! P% B$ o ) n0 Y5 Q% e4 C1 [
if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
" V/ X3 o' ^" c2 a k' \9 Z {
& ~3 O7 x! P/ j$ t$ Y, Y/ ^& @3 B$ M; N old_data = Sensor_Data[0];5 h y* P% t6 ^. J. I7 y, W
return Sensor_Data[0];
% s" C' L- m0 w8 h+ b }
2 c* B* l8 O! A u. W z6 c else- \ t3 {4 w9 E: K, C
{
& f5 W8 d6 N" l return old_data;1 B. U N5 `* R
}
7 g0 l" {5 v, {0 q }
" u8 n) ^ w1 i0 v% j- O" ?: a 複製代碼
2 K7 x3 p$ ~: T0 B4 |+ k/ b MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
7 \# \0 X$ T) n( s* r/ I! P
$ e# l: I7 i4 S- {