|
|
- _5 _: C1 f n, r4 t( A. l: v, |- #include <Arduino.h>
$ s& k& r) E, g8 b, o - #include <MeAuriga.h>
/ `: p0 w5 l* a Y* { - #include "MeLineFollowerArray.h"- K$ {) U+ H, a- K5 Z
- 6 q$ X- G4 D9 H) L
- MeEncoderOnBoard Encoder_1(SLOT1);
( @, A0 {4 r( ^6 E- Z* { - MeEncoderOnBoard Encoder_2(SLOT2);
3 L% y" ~5 c! U5 } - MeLightSensor lightsensor_1(12);
6 C5 G/ s X* f- s/ Y" ` - MeLightSensor lightsensor_2(11);
9 l7 z, C) F/ p1 F s8 z0 B - MeBuzzer buzzer;2 j8 ?1 s, |8 R& N$ l+ I
- MeLineFollowerArray linefollower(PORT_6);' ]5 N' d9 h: H5 X: |
* ^3 o5 H) {: Q3 o( z- #define Error1 1# e% @! ]# N" _0 d
- #define Error2 2, V! k7 N4 r4 `7 T( u8 S6 K3 ?* ^3 t
- #define Error3 3. r! L# ]/ |; H2 \( k
- 7 }* o- R( `8 e( [1 J
- #define Kp 15
% i0 ?6 T! M' [2 {) Y2 d - #define Ki 0.15, s, p1 ?3 t8 H+ Y! K
- #define Kd 0.03
9 M* W1 R: C) l( Q; H# y1 e
- p2 r! v# |( o: X9 }- uint8_t sData;2 @6 {& H, k: A' U
- uint8_t D1;
' n9 v6 e" S- X1 w8 f - uint8_t D2;* r/ z7 D0 X% E5 }2 u4 R' { U
- uint8_t D3;+ s5 X1 @( t+ `- @ A* p
- uint8_t D4;
% a: E$ k- F6 H - uint8_t D5;
0 V) a/ K. f; S, x' B) Z - uint8_t D6;
. d5 r$ a8 q, g, p: U+ v3 ~ - " f- @/ p6 e7 S' f0 }
- float previous_error = 0;/ S: I& o' y$ E
- float integral = 0;
, Y2 h; G1 V9 i# [; u - float derivative = 0;
) T! f4 s. F r* j( `9 V9 m- v - int Speed = 160;
, m: `1 g' Q2 \# C3 E - float output;" o6 t, Z% i7 m# ~, H
) C% k3 A7 ?0 t$ n! M) \- byte Left;
! q! t. b( D# l! C5 W0 A6 ~5 j
- P2 m0 D* V9 v+ v- void setup() * R' o! K* }2 D R" n
- {
0 j8 I3 k. k$ s1 S - //Set PWM 8KHz4 L& o6 Z8 s, ?# e; T" C
- TCCR1A = _BV(WGM10);
- H+ Z0 I0 C* K# ` - TCCR1B = _BV(CS11) | _BV(WGM12);; }$ J8 H; G: k2 { L
- TCCR2A = _BV(WGM21) | _BV(WGM20);
; C. Z9 u% i W& ]& I6 p/ g- p - TCCR2B = _BV(CS21);& ~7 e, x" W0 `- V) X+ m/ j3 Q
- Serial.begin(9600);# ~& B" @: L- B% U0 m
- buzzer.setpin(45);
' T9 |* `, P6 N' F - }
2 n# Y" c% K* l: F) P
( ~0 Z" b( J5 \. g0 t6 j6 Y- void loop()7 A0 L' a! i- w! ]7 H" Z) P8 r4 y! g
- {
; j0 |3 ?. O0 ]8 [' V6 g - while(!((lightsensor_1.read()) < (10)));8 B$ o& `( S7 c- \! I: g2 a2 B4 c
- buzzer.tone(1047, 500);( G( O+ i; p$ e0 {! g' F
- delay(500);
- i w+ e p' x, U9 u% q. b/ N - do4 b! m+ Z& T" K/ y' {
- {
' {7 h3 r4 a3 A* w8 k - int Err = getErr();# j- h& L* [ R1 u) x1 a
- if(D1 == 1), r0 a4 s. d: i- J: t% |9 z
- {4 a. Z3 t3 P. j2 w
- Left = 1;
3 x* B0 | d) c z o1 [- w$ o. _ - }/ H s; e! z; W" c! [9 R
- if(D6 == 1)1 b1 J& P( X6 n9 N0 N( t0 ?
- {- v, ?$ S8 Z+ A
- Left = 0;5 u6 _2 K$ b; J% e$ Q8 f
- }$ L1 t8 w; m( V3 G4 P+ b. E/ I
- if(Err == 99)
# R7 c; q# W, @0 @ - {1 U, K# q* }8 i6 n0 L+ C- \0 u6 ?; w
- if(Left == 1)
, {/ r; h8 Q# y! L+ s: h - {
4 `- I5 F7 ?' P* D - Speed -= 5;
# ~2 T2 g$ ~ e: m! ^5 M5 Z2 r - moto(0,Speed);- v2 Y A5 t- `- R. i
- do3 c+ Z A w2 H4 I9 W
- {% I) P/ d- p; B6 ?- @
- Err = getErr();7 q7 g4 Y" Y2 x
- }while((D1+D6) == 0);
) l" l4 m Z# {3 e - }
% j" E% t5 A/ x: \# h' u) ^* |+ J - else# A9 s9 u9 T) a @, \* {6 D
- {
W8 K# o/ p+ c# T. }% N - Speed -= 5;/ F& o3 c" S& e6 j( w3 T
- moto(Speed,0); 6 B/ M/ R9 Z5 h2 G# [8 P; N3 \
- do
& I# o+ Q6 Q) V7 r - {
! C( q% @5 R0 Z+ J5 W - Err = getErr();' |' V8 ~/ u H' z* q: p
- }while((D1+D6) == 0);0 {* ]' p9 [* W/ x, H! H
- }
9 y' ~2 ?: x$ I/ G' h/ L - }) ?' _. E$ I2 f4 l
- else
( r& B; y7 G+ U' W! R - {: F( w7 X! D, t) \' g. m
- if((Speed < 160) & (Err < 2)) Speed+=1;
/ |, I! o! j! Q/ H b - if((Speed > 100) & (Err > 2)) Speed-=2;
- `4 Z7 Y6 \0 E2 v+ { - integral = integral + Err;0 r7 N5 m8 G2 z1 n* S
- derivative = Err - previous_error;$ s; e/ @! p6 v. i
- output = Kp*Err + Ki*integral + Kd*derivative;6 V. c7 [( _1 L9 g, b
- moto(int(Speed-output),int(Speed+output));
* V8 }. H |* P - previous_error = Err;- y' v6 r4 m. f" m4 @% B
- }
- s! K9 {) K8 {- a3 N5 | - }while(!((lightsensor_2.read()) < (10)));
2 L/ o$ W1 J' ] - moto(0,0);
) b0 R) {9 ^8 A" U: F# N - delay(500);/ k% n- Z' k3 W N% Y
- buzzer.tone(262, 500);
/ @4 n- f1 ~; l( |$ X9 N - }) v1 R+ @' y7 D* c! o
+ V/ L5 m& U# x$ F2 R# p* r- int getErr()1 u" k4 f4 L% p1 }. H2 R
- { ' T$ |2 z; E" u# P, r. P$ V
- sData = linefollower.getValue();( c1 ~/ Z8 K# H& U D3 f
- D1 = ~(sData>>0)&1;7 Q* n4 G/ S1 d
- D2 = ~(sData>>1)&1;* ^' n4 Z9 S/ ?6 ^( F# z
- D3 = ~(sData>>2)&1;3 X# t& a. _& n9 I
- D4 = ~(sData>>3)&1;7 t( Z! R+ K/ i
- D5 = ~(sData>>4)&1;
' A6 Z) q# P$ K! p: _1 F" r - D6 = ~(sData>>5)&1;
# v! _7 ?8 C% X. }! [* C - int downD = D1+D2+D3+D4+D5+D6;
% Q+ K& S: a, V+ R6 \ - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);# Y4 T& f) a M4 E- t
- if(downD == 0)
t5 Q) o) s1 J - {
/ i9 `" O3 l7 ]: I2 d - return 99; D1 t o6 i. \8 N: r; n3 [+ I
- }' V! R, Z$ F* e2 t0 f
- else( G: z$ \1 E3 E5 {- ^8 }! v
- {
5 f- h! | F6 w$ C0 Q- j - return upD/downD;
7 b4 X; u( s% ^ - }
( u1 l/ k% q( a4 I' { - }; [" L8 g& h' a" [+ Q. y$ ~
' B" a/ F' V. j% } B# F1 y& \5 D8 m1 A- void moto(int Speed_L,int Speed_R)
* J3 i' ^: M0 l9 g - {# i% _$ B7 ]: [% n
- Encoder_2.setMotorPwm(Speed_L);
' _7 Z# R3 e' F - Encoder_1.setMotorPwm(-Speed_R);+ @ h6 f5 X" `
- }
複製代碼 . ^# x) l2 T, C7 Z5 a+ S. `
MeLineFollowerArray.cpp
; x! L- E8 D' r8 F/ @/ Q- #include "MeLineFollowerArray.h"' d' X, v, V! [2 o" t; i/ _1 i
- 0 d% f: ?# s- [: |- n: H
- #ifdef ME_PORT_DEFINED
f& a" [( e# D1 U& V - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
/ g$ a: ~9 M6 P' F; s - {
7 M3 P) r' [$ L+ O
" z3 Z+ H; a) V+ |/ Q- }; I7 v" g) ?& T/ d# ^) S m1 ?- {
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
0 S9 J N- H% E3 j% V3 d- Q* z - {6 C+ D6 M: P h$ A$ @
- _DataPin = mePort[port].s2;
6 [" \/ c* [7 G( l' h - pinMode(_DataPin, OUTPUT);
! f% |: X# p7 P) g+ x, t - digitalWrite(_DataPin, HIGH);
( S* ~9 p( P, a - }
' w+ a( o$ S. q0 n' O3 Y - #else // ME_PORT_DEFINED% H; F* n/ ]' ?$ g4 B
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
9 r! m+ M4 B+ a5 c/ p - {: L$ \1 y. `# W! o) Q3 G+ m3 t
- _DataPin = pin;
! |& s/ }2 y/ g4 \) b3 w3 b - pinMode(_DataPin, OUTPUT); 9 ^$ i- R+ _. v7 E5 F
- digitalWrite(_DataPin, HIGH);4 ]* ]* q! G u! F3 o
- }
+ L% g9 p. o) f/ P' @" L - #endif // ME_PORT_DEFINED' x5 q; W; }" e8 G8 x
- 3 i, \. M/ g$ J" f; s. Z1 v5 ]* C. u
! j! B& i: k! o- void MeLineFollowerArray::setpin(uint8_t pin): s6 q3 u; F# x' l# J! B5 [0 |/ |
- {. Q+ t- d' q: y) U. X
- _DataPin = pin;0 w$ b1 `- b v; G" d
- pinMode(_DataPin, OUTPUT);
A0 s0 n8 }- C" W3 F0 Q5 f+ l, P- x - digitalWrite(_DataPin, HIGH);
+ f: ~; s8 O* t5 l$ A$ F6 u - 8 B9 D' ^: M4 s
- #ifdef ME_PORT_DEFINED
; q; m8 [# P- o5 R/ l8 S4 V: t0 i - s2 = pin;2 F! r" x4 z g8 D: G& T" m
- #endif
; W( t+ g% J$ h - }
3 k' P& }! s, i" t
' m. j) ^( ?2 r( F0 x. }- uint8_t MeLineFollowerArray::getValue()
% f" U6 L" y) y! J1 q - {
~( ~& `4 }: c - uint32_t LOW_level_read_time;, v u0 {0 q$ Z* @2 K. y
- uint32_t HIGH_level_read_time;) y4 N1 T9 w: P% L) S. X" H! T! Q4 t- U
- uint32_t time_out_flag;' P7 j9 N+ v. |/ o/ f
- uint8_t Sensor_Data[3];
1 R* {; ^7 V% `- w - static uint8_t old_data = 0xff;, Z! E9 Q- ]/ D# ~/ |4 }% C
- $ G3 Z6 x- C$ }1 b4 k& f
- pinMode(_DataPin, OUTPUT);
7 T; U( Z5 @* L- @- I - digitalWrite(_DataPin, LOW);! y1 R4 U+ _5 e+ T& o9 L0 I0 ^
- delayMicroseconds(980);, \% |* u- V+ {1 {5 Y" S z
- digitalWrite(_DataPin, HIGH);
7 h/ `9 R# s3 G, S e
; F% ]6 R1 ?7 y, c9 P( d- pinMode(_DataPin, INPUT_PULLUP);
; i: \4 Y' x/ j) b7 ~ - delayMicroseconds(10);7 b5 Q2 X1 _, q+ O2 y8 ?
- 9 t- s$ b+ n8 w1 Q
- time_out_flag = millis();2 d4 c3 ~; t T, W' g
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 F7 a& d4 g# e, J2 I$ i - ) d% W8 N/ c/ y; \5 a" e5 Z) v
- LOW_level_read_time = micros();
2 y* U* f$ ` T" p - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out+ ]$ l: J( L" ~% J* w
- {2 ^& N! _5 i7 P) t% |6 N
- return 0xff;3 [6 w( R% f: ?% y4 v* ?: u( r' ]( ]
- }
2 y o; G( ~+ A5 c! Z3 V - ; I. U5 \" l7 M" w
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
' {, D* @' I+ ?- z. r5 P& Z& A2 w - 1 z; V! ?: Z& D8 p% Z
- HIGH_level_read_time = micros();
; N* G/ f+ q p2 o3 c$ L) T; ] - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
1 T' t0 E; }: Y0 v. Y. c5 K - $ Q1 g: A3 e* h7 m# L
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out" l, ?! I/ ?" S, {+ {
- {
# c- k* u6 I! U" Y - return 0xff;1 u) V: `9 F7 [
- }
* G6 t2 [: `: F. o) ]8 A/ |
6 a! l$ y4 [ z* b/ h+ A- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
5 ]- u8 R0 O% a5 J* {5 ?6 l - {% U# u# c" O3 ~7 y2 J" t
- return 0xff;) |; l- Q+ g) v, Y; q8 k
- } z' y R# B3 a. w6 G$ E$ w
- 0 P0 C9 i, h+ v( Z* j
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
! D: ?5 I N) { - LOW_level_read_time = micros();
% ^) Z, i* V9 F' F- b/ q+ }3 j - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
' z# Z/ N) |( r" r$ M; d - 5 `( H+ \4 ?9 z4 J
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
; A: {( K: }1 E0 U2 n4 ` - {5 q( W9 L6 h: X: l
- return 0xff;7 c1 x5 R% b! A0 |
- }5 L3 `# ^4 M$ r* u) T
- 6 t; ?+ i1 t; F, }% b3 e$ U" T
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
2 _! T' v' L. A. N! b+ H* Q* e9 o - {
6 A* J2 B1 J5 _2 I+ N - return 0xff;+ W8 v. w% W$ l; w6 [
- }
/ `8 {% |* A2 V2 u
' h5 G& e& u9 [- for(uint8_t k=0; k<3; k++)
( n; X$ I% i6 R6 |- Y) p2 V! b ] - {% G+ g7 L- Q8 A# N2 R2 X9 \
- Sensor_Data[k] = 0x00;
: T. q3 Z+ _" |- n! K3 z3 Z5 W - 6 q4 Y) ~0 o* ?4 D8 u5 \( I
- for(uint8_t i=0;i<8;i++)7 Y# ]0 k( d' Q8 [
- {- ~( f% O! r$ e# x5 A& ~
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level' J0 l% [! ?% z* Q* @5 }
- HIGH_level_read_time = micros();8 L! {" V6 p$ o" d8 V0 n; f
- LOW_level_read_time = micros() - LOW_level_read_time;
& S0 O4 l9 B$ H9 g - * V- v4 [- Z- [' v
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )1 K1 s0 ^" ? B
- {. w2 @; D/ Z2 e0 D0 j N( P
- return 0xff;
, X2 P% N2 S4 l3 C) | - }6 Z- F. s& j$ I1 A; M
2 }$ T5 U* u9 W$ _9 l0 ~; [- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
# |: q Z r# q" |; ?6 j) A - LOW_level_read_time = micros();
3 n4 t+ Z1 P: W( J: U! r - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
( n% y( L; V+ u6 Q4 N - * h. W P4 m, z2 o7 u
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
6 a) w4 h% ^* b - {
1 e8 B, ]2 K& |- \ - Sensor_Data[k] |= (0x80 >> i);
# z9 T( u/ U, J0 V! W - }
7 K" I( s8 @# T - else if(HIGH_level_read_time >= 100)
9 w- m' U' m* I - {# `! S; [- M. ^ a1 C
- return 0xff;% R- V+ C+ P5 u. g6 S) H9 \
- }
1 D& ^0 q- [8 M* V- D0 d: C - 3 C" B0 \" k' B2 U, o# Z1 @- S
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
' F& Q& z3 o1 L" z7 f, s# Q( H - {
) O7 w& y) y0 E H8 ~. O, a& y - return 0xff;& I; h% q3 U6 B
- }
0 ~ U# \5 |+ A! V R: J - }! L9 Q7 i% j6 P
- }
9 b1 E, l- P( D5 A - ; C6 r2 S" ]; S7 _) ]) C, F
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
# M- L; R2 o' v/ k: l - HIGH_level_read_time = micros();
# t$ S) z8 f8 S5 |; A - LOW_level_read_time = micros() - LOW_level_read_time;5 G, l; Y$ A' t
- - B: Y' K0 z# D
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
( x; n; e6 \# L! x - {1 A3 H( `& l. W) a4 z9 _
- return 0xff;
! d: F5 z" k) q" i: t* S% s - }+ v9 d" v- U' V( v# h, U
# h! Z+ v2 ?1 y: M- pinMode(_DataPin, OUTPUT);8 \4 x. g1 `, ?4 c7 D3 p/ N9 g
- digitalWrite(_DataPin, HIGH);
' M8 W! C) b" P- [' o
2 S# A& p* j0 W+ j- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))" I5 d4 w- F, W( x% |% u
- {
v( M D: M3 q* U' p$ q: Y - old_data = Sensor_Data[0];
" O6 ?4 A; I1 P: i - return Sensor_Data[0];
, K* B r2 f9 c0 m9 K, V4 Z" o - }
8 C) t7 o9 ^/ d: u) X4 @8 ? - else" t& y; [, b; Q5 @- k) t
- {1 R6 k( |( X+ F5 ]
- return old_data;. I* u; G2 r1 ?, O: p9 d5 h
- }
0 `. h( S7 _3 E7 u9 t2 z - }
# q+ C. n$ o7 D! F
複製代碼 0 s1 s2 X5 s9 n
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
6 G7 B9 `) S4 r" L* K0 n) j4 j0 {6 D; F0 k) a8 {
|
|