-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z_Tracing_LSA.ino
198 lines (187 loc) · 4.15 KB
/
Z_Tracing_LSA.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <avr/io.h>
#include <avr/interrupt.h>
volatile int x, y;
int setpoint = 35, error = 0, error2 = 0;
float motorSpeed = 0, motorSpeed2 = 0, Kp = 0.6;
float pwm1 = 0, pwm2 = 0, pwm3 = 0;
bool flag1 = 1, flag2 = 1;
#define m1pwm 10
#define m1dir 9
#define m2pwm 12
#define m2dir 11
#define m3pwm 7
#define m3dir 8
void USART_Init3(unsigned int baud)
{
// set baud rate
UBRR3H = (unsigned char)(baud >> 8);
UBRR3L = (unsigned char) baud;
// enable reciever and transmitter
UCSR3B = (1 << TXEN3) | (1 << RXEN3);
// set frame format: 8data, 2 stop bit
UCSR3C = (1 << USBS3) | (3 << UCSZ30);
}
void USART_Init2(unsigned int baud)
{
// set baud rate
UBRR2H = (unsigned char)(baud >> 8);
UBRR2L = (unsigned char) baud;
// enable reciever and transmitter
UCSR2B = (1 << TXEN2) | (1 << RXEN2);
// set frame format: 8data, 2 stop bit
UCSR2C = (1 << USBS2) | (3 << UCSZ20);
}
unsigned char USART_RECIEVE2(void)
{
//wait for data to be recieved
while (!(UCSR2A & (1 << RXC2)))
;
//get and recieve data from buffer
return UDR2;
}
unsigned char USART_RECIEVE3(void)
{
//wait for data to be recieved
while (!(UCSR3A & (1 << RXC3)));
//get and recieve data from buffer
return UDR3;
}
void x_recieve(int x)
{
pwm1=0;
// Calculate the deviation from position to the set point
error = x - setpoint;
if (error > 15)
{
error = 15;
}
if (error < -15)
{
error = -15;
}
motorSpeed = Kp * error;
pwm3 = 60 - motorSpeed;
pwm2 = 60 + motorSpeed;
pwm2 = constrain(pwm2, 20, 80);
pwm3 = constrain(pwm3, 20, 80);
if (x > 35 && x <= 70)
{
pwm1 = 15 + motorSpeed;
digitalWrite(m1dir, HIGH);
}
else if (x < 35)
{
pwm1 = 15 + motorSpeed;
digitalWrite(m1dir, LOW);
}
else if (x == 35)
{
pwm1 = 0;
digitalWrite(m1dir, HIGH);
}
flag1 = 0;
flag2 = 1;
digitalWrite(m2dir, LOW);
digitalWrite(m3dir, HIGH);
analogWrite(m1pwm, pwm1);
analogWrite(m2pwm, pwm2 );
analogWrite(m3pwm, pwm3);
Serial.print(" x= ");
Serial.print(x);
Serial.print(" y= ");
Serial.print(y);
Serial.print(" pwm2= ");
Serial.print(pwm2);
Serial.print(" pwm3= ");
Serial.print(pwm3);
Serial.print(" pwm1= ");
Serial.print(pwm1);
Serial.print(" error");
Serial.print(error);
Serial.println();
}
void y_recieve(int y)
{
pwm3 = 0;
analogWrite(m3pwm, pwm3);
// Calculate the deviation from position to the set point
error2 = y - setpoint;
if (error2 > 15)
{
error2 = 15;
}
if (error2 < -15)
{
error2 = -15;
}
motorSpeed2 = Kp * error2;
pwm1 = 60 + motorSpeed2;
pwm2 = 60 - motorSpeed2;
pwm2 = constrain(pwm2, 20, 80);
pwm1 = constrain(pwm1, 20, 80);
digitalWrite(m2dir, HIGH);
digitalWrite(m1dir, LOW);
analogWrite(m1pwm, pwm1);
analogWrite(m2pwm, pwm2 );
flag2 = 0;
flag1 = 1;
Serial.print(" x= ");
Serial.print(x);
Serial.print(" y= ");
Serial.print(y);
Serial.print(" y_pwm2= ");
Serial.print(pwm2);
Serial.print(" y_pwm1= ");
Serial.print(pwm1);
Serial.print(" y_pwm3= ");
Serial.print(pwm3);
Serial.print(" y_error2");
Serial.print(error2);
Serial.println();
}
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
Serial2.begin(9600);
pinMode(m1pwm, OUTPUT);
pinMode(m2pwm, OUTPUT);
pinMode(m3pwm, OUTPUT);
pinMode(m1dir, OUTPUT);
pinMode(m2dir, OUTPUT);
pinMode(m3dir, OUTPUT);
digitalWrite(m1dir, LOW);
digitalWrite(m2dir, LOW);
digitalWrite(m3dir, LOW);
analogWrite(m1pwm, 0);
analogWrite(m2pwm, 0);
analogWrite(m3pwm, 0);
}
void loop() {
x = USART_RECIEVE2();
y = USART_RECIEVE3();
if (x >= 0 && x <= 70 && y == 255)
{x_recieve(x);}
if (y >= 0 && y <= 70 && x == 255)
{y_recieve(y);}
if (x == 255 && y == 255 && flag1 == 0)
{ pwm1 = 30;
pwm2 = 30;
pwm3 = 0;
digitalWrite(m1dir, HIGH);
digitalWrite(m2dir, LOW);
analogWrite(m1pwm, pwm1);
analogWrite(m2pwm, pwm2);
analogWrite(m3pwm, pwm3);
}
else if (x == 255 && y == 255 && flag2 == 0)
{
pwm1 = 0;
pwm3 = 30;
pwm2 = 30;
digitalWrite(m3dir, LOW);
digitalWrite(m2dir, HIGH);
analogWrite(m3pwm, pwm3);
analogWrite(m2pwm, pwm2);
analogWrite(m1pwm, pwm1);
}
}