|
|
, Q7 [; J/ n, J0 U: u l- #include <Arduino.h>$ \% M3 V, Y, D4 ]8 r3 N0 ^% A
- #include <MeAuriga.h>* Q* X/ e* ~% L6 H3 ?- e
- #include "MeLineFollowerArray.h"
$ ^7 }1 E' P7 e" H - 9 }: B- b% x, n8 ]7 W# i2 k
- MeEncoderOnBoard Encoder_1(SLOT1);
8 T9 E4 U4 U, n8 Y - MeEncoderOnBoard Encoder_2(SLOT2);
) E% L! g; f, ^: R6 J: w9 t - MeLightSensor lightsensor_1(12);8 W5 j5 ^3 ?: ^- P
- MeLightSensor lightsensor_2(11);: Z+ D! l* Y! V" r+ h" c9 @+ [
- MeBuzzer buzzer;
+ C9 F8 g1 A7 _; c - MeLineFollowerArray linefollower(PORT_6);
5 J9 I7 f/ R; |" f1 \$ V
/ r% T/ }& }, ]% b8 @) [- #define Error1 1
1 E4 b- h/ Q/ J- ~ - #define Error2 2
6 O: q3 o6 g9 [! S: @1 A - #define Error3 35 g( Z' W- Y6 c% |( Q
: H$ s' @) K* j' x* n% P' W/ V# D: O- #define Kp 15
8 s- a+ u6 n( ~& a } - #define Ki 0.15* u! f) L3 ^/ J
- #define Kd 0.03( M2 p7 v: U1 g J/ {/ O ^* i
! ^8 H! J- P) H0 q9 T7 f# V* [$ X- uint8_t sData;1 [; f4 o3 S9 ^& @# z
- uint8_t D1;
! Q( A9 {( y' ^& C$ [" d5 q - uint8_t D2;
$ f7 s" w) P8 q( D/ e1 C6 d, j - uint8_t D3;
9 U, Z( c3 C$ ]% @9 r; g7 X - uint8_t D4;" }+ Y" U3 [2 Z- A, u- k$ {
- uint8_t D5;) Z' V3 b# l# }9 G7 w. R+ ~
- uint8_t D6;: q# Y+ y/ z! c( n' a/ X! ^
( q; k4 A% W2 e9 u% Q- r- float previous_error = 0; G# z* Q# x! C) t# Q6 x( ]1 L4 U
- float integral = 0;; @ G9 L7 q: L$ Z
- float derivative = 0;
) j/ @8 J! h0 r* r# i - int Speed = 160;7 t q# U9 w1 t5 V' r" ~
- float output;7 B/ U& n, V, d4 |
" J s1 A* T; o. a) e- byte Left;
3 B; u. ?+ }8 |7 e2 D - , \% D% \; S" A# H0 y+ e' n
- void setup()
. x, W" A! x( q5 O - {
$ W& }7 J$ E0 v. {/ p! i3 k9 g- A - //Set PWM 8KHz
& ?4 l: w) M" X) b( V8 O" ` - TCCR1A = _BV(WGM10);- {# _; P+ L2 [# r3 \
- TCCR1B = _BV(CS11) | _BV(WGM12);- A2 _, V8 x A$ {( m2 ~7 V# }$ A
- TCCR2A = _BV(WGM21) | _BV(WGM20);
$ h W7 I) b/ N4 x# r$ a - TCCR2B = _BV(CS21);
$ t# O& L+ n& N& p" y& `' { - Serial.begin(9600);2 c' a# v8 a. r/ z% _& i6 y$ {' \
- buzzer.setpin(45);
3 M7 _. g" i( e1 x- i, I - }1 v3 X0 B8 K6 `" U- n( z
/ |3 D6 n2 m0 A- g6 a3 h- void loop()
6 K2 l! R6 X% u( ? t+ F" G - {
- A$ O: R' f3 I" e* m - while(!((lightsensor_1.read()) < (10)));
7 X7 s" P# p1 g9 I s - buzzer.tone(1047, 500);0 d" u% Y1 W7 Y8 I) v; x, |5 E4 r
- delay(500);
0 A- q P' S& n - do3 @: S: D' P9 t7 ]
- {
3 l: k, b5 n5 ~ - int Err = getErr();
. J9 b+ {' s+ z+ v7 s6 E - if(D1 == 1)0 Y, f1 g1 s, b' z
- {
( F6 | ^2 m# D* l V. R - Left = 1;
$ g3 {" i6 Q% s O: d - }
( C6 j9 }. P6 t! L: c - if(D6 == 1)
5 S' ?+ C$ A2 _: b, Q8 X2 ? - {
* D7 b S0 a+ K8 r4 m - Left = 0;) k" g, t- p i3 `: K8 h
- }# W( I+ f" S9 m f b* m7 h7 I. h
- if(Err == 99)/ @" b( k# W, U' C) ~* M0 _4 e2 f7 i
- {6 T" e+ ^0 _: C% w* O9 V
- if(Left == 1)
6 s; i3 _ K& ?" o/ U - {
7 C! M( E- s% I" S - Speed -= 5;
- a {3 v' q- g) T - moto(0,Speed);
) S4 P) G0 |, `, e- X4 `4 ?8 n0 l - do
% [ t/ @4 j& t8 P; K - {
" u. }! J5 _% E0 p0 C3 |0 s. V$ Q' d - Err = getErr();
4 e9 ?4 n/ [' Z4 G' N- _ - }while((D1+D6) == 0);
) I, W5 ` Q; G$ B% I - }
5 s& Y! i* ^4 [: A+ D; ^7 ]& M8 R - else: w* y/ }3 E; m2 @5 k* U- I$ |
- {
; ?# s. w P2 b# T- m - Speed -= 5;
$ U+ M$ K& [0 B7 j9 F - moto(Speed,0);
4 | G1 R: v3 w0 i+ K - do( s# h$ y# F9 Y4 V
- {( `+ I& e* u1 V$ x k
- Err = getErr();% l, s; j/ b& U' H7 w2 L+ g" Q
- }while((D1+D6) == 0);/ U- u8 C( U3 V! R, T: m% [6 i) P- l
- }) k* |7 J7 z- k) {# ], b
- }2 N* E. U! R2 i' U3 X9 H! q9 R+ y
- else- p8 R$ H' V! M, g0 H( J+ S8 H
- {
5 k1 {4 ]3 {, _( T. F - if((Speed < 160) & (Err < 2)) Speed+=1;
( ~% h1 V# Y+ ~$ p) \ - if((Speed > 100) & (Err > 2)) Speed-=2;
1 z- t4 ]0 M- K7 F - integral = integral + Err;; ~* c6 y) S2 H; M: l
- derivative = Err - previous_error;
5 B1 ` K2 |1 U- l% U' a7 P - output = Kp*Err + Ki*integral + Kd*derivative;7 j/ c y: [) X7 M
- moto(int(Speed-output),int(Speed+output));, y+ G+ k* f5 V% F
- previous_error = Err;& d9 f! C& C) P3 R3 J$ ]( T0 g
- }
# D: k1 E+ J6 y P$ i/ f - }while(!((lightsensor_2.read()) < (10)));
2 X2 |: Y* [8 B$ l! H: [ - moto(0,0);
2 O" u9 q* s& _' `5 _3 D - delay(500);8 f0 z8 `1 D, C
- buzzer.tone(262, 500);
+ \+ e; C1 }4 S3 t! _8 P - }
# q& o2 Z- a7 L( i" H1 A3 {
; p3 i* C& a, i% }6 \ S" S- int getErr()
% c0 H& U* k E0 z2 ` ^ - { 9 ~+ C! C1 B- Y" V" l+ C
- sData = linefollower.getValue();2 C, Q0 |& e( d G% E
- D1 = ~(sData>>0)&1;' o# X: c+ G4 c
- D2 = ~(sData>>1)&1;
5 U( r# ~7 @ q, W( \ - D3 = ~(sData>>2)&1;
, @2 O9 H: ^% {) O$ r6 ]4 w - D4 = ~(sData>>3)&1;8 l5 J7 Q% E9 j7 A; d6 h' A7 a7 G: S
- D5 = ~(sData>>4)&1;
; f+ p7 K6 q# N+ @' ^ - D6 = ~(sData>>5)&1;3 U, B2 \% L8 r6 i3 z5 I
- int downD = D1+D2+D3+D4+D5+D6;
( e2 V. A$ ?' s. }+ I& \8 ] - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);7 k- P, |9 s8 n- m4 \
- if(downD == 0)
. S- t, _2 G8 a5 B9 h - {
- ~6 y! o: f! F% [0 ?& B+ N - return 99;
, f; n/ Y6 W: {* \# l1 v9 n, t* L - }; ], |! k; M c8 F6 F$ J
- else. V9 e- k5 [& B4 {" d& m. l: I
- {+ a+ a" r' Z4 ]8 I, }
- return upD/downD;! x: J% U7 Y, M: ` K5 F
- }. L' J1 i- H. f
- }
4 O2 m. P% F& Z1 X, a0 r - 8 I' k. |; [1 I$ T8 c
- void moto(int Speed_L,int Speed_R)
- G2 t+ O& h- N4 {8 U. |' V6 j- N - {* C2 o" v! A+ q2 J
- Encoder_2.setMotorPwm(Speed_L);) x' E5 O7 e2 f4 E7 ~) e
- Encoder_1.setMotorPwm(-Speed_R);
6 \, b8 C' S7 O+ z9 v+ X - }
複製代碼
6 v$ ^+ N: Y8 X! l0 K& x5 t5 {MeLineFollowerArray.cpp2 j7 r2 O, h7 Q' \- y! F
- #include "MeLineFollowerArray.h"- Q3 A/ J5 m& i: B5 X. v/ \/ U
- ! O$ A1 B" m& E8 f5 _
- #ifdef ME_PORT_DEFINED3 |" `! e- l, Y
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)& k" w1 h2 ~3 }7 y# P* i h
- {
! V& a, f+ k( n7 J2 _8 B - ; b& ] [* ?! t# Q% h7 l
- }
( o2 l* D+ j8 V0 X2 v4 N- e2 V# E - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
! q' @0 @7 h& L! q - {
5 W; f3 o) q1 E5 _( y1 @; Q* g6 a - _DataPin = mePort[port].s2;
2 q- G9 m8 p& W - pinMode(_DataPin, OUTPUT);
: Y% t. Z) ^% j$ U - digitalWrite(_DataPin, HIGH);1 _" k7 ~) m* N) A7 j. V; i' O7 S% y
- }
7 F7 P1 _$ K+ t: E7 v& V/ e - #else // ME_PORT_DEFINED
3 b- W. p- d# Q6 E& t/ W6 y - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
5 r: [* t# J7 `# z - {% ?% S% [* e6 e
- _DataPin = pin;
/ I+ T; W5 Y h. ^7 \; m - pinMode(_DataPin, OUTPUT);
3 ^# K$ r0 I9 E- K0 f5 g/ G - digitalWrite(_DataPin, HIGH);% t7 I7 W+ B) t2 X. ]$ S0 d6 @
- }0 K; P5 p! G" E9 T% }
- #endif // ME_PORT_DEFINED2 [& ~/ ?8 B) v! X5 y
! y& Y, L6 \6 p2 J4 t0 |
4 f1 v- A' l6 o0 z" u! V- void MeLineFollowerArray::setpin(uint8_t pin)- g# w1 X- w. }: C; {
- {8 I: K+ _' E5 K6 x* Z
- _DataPin = pin;8 d* f F* Y8 q( R& u
- pinMode(_DataPin, OUTPUT); $ G- Y0 N0 G% B$ z( H, ~5 z* S$ j
- digitalWrite(_DataPin, HIGH);
, K6 `& D% F; y& h - ( { ~9 K* I/ P! J t' H
- #ifdef ME_PORT_DEFINED
- e" l: D" U* T7 C - s2 = pin;
1 \- D4 D6 y( l1 P" s6 }7 b$ U - #endif* j; e. R: `( v% {, P- o8 P! U
- }6 \2 F2 |9 {) c( l7 ^, z
- + W; w% T) n# a ?$ l8 j
- uint8_t MeLineFollowerArray::getValue(). I5 r/ g, ]0 O" C
- {" J: ~% m( P& y; W- Y' c% ]
- uint32_t LOW_level_read_time;3 t6 q" F: c0 [7 Q7 [6 S% ^
- uint32_t HIGH_level_read_time;$ m4 }( ^, y, ?, F: V8 I F. d
- uint32_t time_out_flag;
7 F. s% {+ t1 Y/ s1 N" c+ ` - uint8_t Sensor_Data[3];
5 v5 u9 o. k) U1 x - static uint8_t old_data = 0xff;" q6 B# X. j: p, @2 c
' O/ @# ~8 N5 O7 Z- pinMode(_DataPin, OUTPUT);
' _8 R. Y, [4 o. h - digitalWrite(_DataPin, LOW);$ [$ X1 j0 f, ]5 P# @
- delayMicroseconds(980);; z/ _6 g$ K8 i+ x
- digitalWrite(_DataPin, HIGH);
7 n) Q4 E. g; x
( b% ~' u' z1 L0 E5 ]& e# C( t- pinMode(_DataPin, INPUT_PULLUP);0 A7 Z4 @* F; r
- delayMicroseconds(10);
5 V3 }% n" ]% K4 |
6 `8 J4 T: y: @5 K2 p; n- time_out_flag = millis();
. g$ S% L, |2 z1 U5 Q, V - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );: e0 T2 {* @6 @" T. S0 [, m
- ) ^, `% f/ D* X$ _0 W
- LOW_level_read_time = micros();
! \0 r$ N+ Y9 Q5 H - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
- g0 O5 `( t- y7 e/ N - {4 s8 R, C8 ~; i
- return 0xff;
! b/ A4 a. K8 e, X: k& m! \ - }
4 @: M8 E/ g- X: E' r - - Z8 n# U: d7 z
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );3 f; t3 s; f# s7 F& h
- : N. f; b; u( N% [3 I* _5 C8 N
- HIGH_level_read_time = micros();, \2 h* j0 }/ L/ h# |& G6 @
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level) Y: N) j. `# w* ^* S8 C
- 4 v# C$ n. d& X, ?6 _3 m7 Q
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
% W) _2 `4 c! _5 u. E! F" ~ - {. I% q4 X: X7 q$ \4 B) \3 O" G
- return 0xff;+ @7 e# o; |4 n& o7 M$ H0 X
- }
7 L+ t$ j' A) i. c3 x* A9 p1 F - , O' j# c8 P$ l6 E& }
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
% V4 a& |% _2 D5 ~1 ~ - {( e0 I& S6 q. X$ p, Q
- return 0xff;: P$ P: g' V5 y* g
- }
9 t, r4 u: B$ O0 ^9 h: y
6 f# t5 c- l& q6 M5 E1 \* C- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );4 R; j8 a4 r, u. _( @6 k' L2 L
- LOW_level_read_time = micros();* Y* ~" ^$ P9 ]
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
; B5 ?/ l" Q' _5 ^0 |2 @. K3 F! Y - 7 [' G2 [0 |: }* j9 d4 U6 o: ?
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
* i I' U8 ~+ N* M1 n- m - {
( @" e. V8 M$ ^6 \; {( Q - return 0xff;$ @2 O( r; V2 o t b3 K# b+ [
- }
( e7 q0 V h3 Q2 ^+ _1 }/ X - 6 g7 l: p3 Q5 w, D/ b) P* ?
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))' H4 Z$ T3 i8 n5 d- W$ m R
- {
; X# m, o& Y4 \3 o! M) w. w - return 0xff;
' n: z& ?; m; ^& C - }6 S% c& M% }2 T* i
- 6 U( r% a0 |8 H
- for(uint8_t k=0; k<3; k++)
7 C, J9 Q- _2 r - {
- M8 e" s4 G5 V6 s) W - Sensor_Data[k] = 0x00;
6 t+ y. G$ y6 n3 ?
% U. D' `, U* m6 q0 N' V- for(uint8_t i=0;i<8;i++)
6 r8 `. J# s8 X( D5 @ - {" }* {' x' X9 j
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level5 J# S. t- \ ]1 ~+ m/ E$ V
- HIGH_level_read_time = micros();
- T% }. m1 s9 A4 X7 F) X& L0 V4 j - LOW_level_read_time = micros() - LOW_level_read_time;; r9 w" T5 i4 Z+ X
4 f) i N& J- l- y. {% g6 B- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
9 |$ A; K4 [4 u - {) y/ G: | \* { K( k
- return 0xff;# M7 n; J8 D" Q
- }
$ T8 V9 ^+ l. ^* b6 J6 B% |5 e5 { - 4 [5 c% i1 v6 W: L% M0 Y
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );2 h9 }) Z. F; M) N' a9 k
- LOW_level_read_time = micros();& O5 Z6 J( z& [* u. R1 y
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
0 ]% _- `" z: w2 N( A+ v
% v" S. B3 H+ O' l O- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1( U; `+ y* y6 a: T3 o! J
- {
" y' K0 z- A& B* o* d" d2 G - Sensor_Data[k] |= (0x80 >> i);
! ^) p5 |7 I- E0 @+ I" H - }7 ^' s) C& O5 G) y( |
- else if(HIGH_level_read_time >= 100)
2 a/ K% h |5 R) X+ W - {
* n# c) U, x& Z: S7 b - return 0xff;: A0 K9 k; `8 F+ U6 ^! w n' \
- }/ |9 y4 t) y C0 X) V
- 7 x* _/ L" r: N, o) \/ @) d9 N: B
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
& q9 e0 f) n N/ Q4 Q f - {+ {+ k, y9 |7 M b
- return 0xff;. E6 J5 g. w1 d# V
- }& L! l3 d( P' c
- }7 `0 g$ _ o' I+ G
- }2 A' r: L' S$ z( D; t6 m
; ]/ U2 d. Y. m! X: J- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
# q3 x& t+ ?, g) A9 B# O - HIGH_level_read_time = micros();0 A8 P' X% E: y b
- LOW_level_read_time = micros() - LOW_level_read_time;! ^# q% U+ `5 a k" u
3 W r% ^6 B0 p$ P* t3 I- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )! I2 k- ]; Y% G, [3 P7 t
- {- N" }% z* ?' E" f ~ R
- return 0xff;& h7 t/ F a9 C5 Z1 x- m8 n
- }
, @0 X. N6 r' \0 t
. E2 q( m& G( {: e( H9 |- pinMode(_DataPin, OUTPUT);
/ s) ^9 i+ _6 R& q; L. J- O3 t - digitalWrite(_DataPin, HIGH);
& [/ E: m. O* h3 X' o
5 ? H0 ]+ I7 P: J- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
# }: Y$ i# d0 [5 v8 f% m! K6 _" G - {% {3 `6 b2 u9 R* L1 a1 |( C
- old_data = Sensor_Data[0];
1 {# d s' p8 B1 G - return Sensor_Data[0];1 v; n; e- c, K9 Z: E! O
- }8 i3 }& d$ ?: a/ x8 U; k
- else
( b3 U; C7 u( D1 T+ S0 i - {
8 b1 b/ D" v2 X/ K; C - return old_data;5 u) O- Q% K j5 @5 l
- }
1 O; I" v" |! l - }* v) h5 y2 g4 q/ |
複製代碼 " g! w. u4 [; i
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
5 `& K! Q7 H' a! D# o' z
3 C% h8 G) a* x4 I# a |
|