-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
376 lines (349 loc) · 10.2 KB
/
main.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include "pico/stdlib.h"
#include <string>
#include <string.h>
#include <iostream>
#include "hardware/adc.h"
#include "hardware/pwm.h"
#include "hardware/irq.h"
#include "stepper.pio.h"
using namespace std;
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
char inbuff[100];
int buffcounter = 0;
bool dataavailable = false;
string inString = "";
string outstr = "";
#define STM_EN 15
float fullRevolutionAngle = 360.0;
int directionchangeDelay = 50; // ms
/****************************************************/
struct MECHPART
{
int StepsToTake = 0;
bool activedir = true;
bool dirchange = true;
float ActiveAngle = 0.0; // 360/res
int DIR_PIN = 0;
int STEP_PIN = 0;
int HOME_PIN = 0;
int FULLSTEP = 800;
int directionchangeDelayCounter = 0;
PIO stm_pio = pio0;
int stm_sm;
};
struct MECHPART stepperMotor1;
void setupPIO();
void setupGPIO();
uint32_t construct32bits(int pulsecnt);
void moveMECHPART(MECHPART *mpart);
void enableStepper(bool act);
void getStepperSteps(MECHPART *mpart, float degree, int dir);
void readtobuffer();
void processinData();
float countAngle(float currentAng, float counterval, bool dir);
bool changeMotorDirection(MECHPART *mpart);
static inline void put_steps(uint32_t steps);
int main()
{
stdio_init_all();
setupGPIO();
setupPIO();
int changeDegreeCounter = 0;
float degPos[7] = {45, 315, 225, 90, 180, 135, 0.5};
int activedegindex = 0;
while (true)
{
if ((time_us_64() / 1000) - changeDegreeCounter > 2000 && activedegindex < 7)
{
activedegindex += 1;
changeDegreeCounter = time_us_64() / 1000;
}
getStepperSteps(&stepperMotor1, degPos[activedegindex], 0);
moveMECHPART(&stepperMotor1);
readtobuffer();
processinData();
}
}
bool changeMotorDirection(MECHPART *mpart)
{
if (mpart->activedir != mpart->dirchange && mpart->directionchangeDelayCounter == 0) // first change detected
{
if (pio_sm_is_tx_fifo_empty(mpart->stm_pio, mpart->stm_sm))
{
mpart->directionchangeDelayCounter = (time_us_64() / 1000);
}
return false;
}
else if (mpart->activedir != mpart->dirchange && mpart->directionchangeDelayCounter != 0)
{
if (((time_us_64() / 1000) - mpart->directionchangeDelayCounter) > directionchangeDelay) // pre change delay
{
mpart->activedir = mpart->dirchange;
gpio_put(mpart->DIR_PIN, !mpart->activedir);
mpart->directionchangeDelayCounter = (time_us_64() / 1000);
}
return false;
}
else if (mpart->activedir == mpart->dirchange && mpart->directionchangeDelayCounter != 0)
{
if (((time_us_64() / 1000) - mpart->directionchangeDelayCounter) > directionchangeDelay) // post change delay
{
mpart->directionchangeDelayCounter = 0;
return true;
}
return false;
}
return true;
}
void setupPIO()
{
// stepper 1 pio
uint stm_offset = pio_add_program(stepperMotor1.stm_pio, &stepper_1_program);
stepper_1_program_init(stepperMotor1.stm_pio, stepperMotor1.stm_sm, stm_offset, stepperMotor1.STEP_PIN, 10000, true);
}
void setupGPIO()
{
stepperMotor1.DIR_PIN = 17;
stepperMotor1.stm_sm = 1;
stepperMotor1.STEP_PIN = 16;
stepperMotor1.FULLSTEP = 800;
// stepper enable pin
gpio_init(STM_EN);
gpio_set_dir(STM_EN, GPIO_OUT);
// gpio_put(STM_EN, 0);
// wheel 1 stepper dir pin
gpio_init(stepperMotor1.DIR_PIN);
gpio_set_dir(stepperMotor1.DIR_PIN, GPIO_OUT);
// gpio_put(stepperMotor1.DIR_PIN, 1);
// inbuilt led
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
}
uint32_t construct32bits(int pulsecnt)
{
uint32_t outbits = 0b0;
for (size_t i = 0; i < pulsecnt; i++)
{
outbits = (outbits << 1) | 0b1;
}
return outbits;
}
void moveMECHPART(MECHPART *mpart)
{
if (mpart->StepsToTake > 0 && pio_sm_is_tx_fifo_empty(mpart->stm_pio, mpart->stm_sm))
{
if (changeMotorDirection(mpart))
{
int remBits = 32;
if (mpart->StepsToTake < 32)
{
remBits = mpart->StepsToTake;
}
gpio_put(LED_PIN, 1);
uint32_t BitRes;
BitRes = construct32bits(remBits);
put_steps(BitRes);
mpart->StepsToTake -= remBits;
}
}
else
{
gpio_put(LED_PIN, 0);
}
}
float countAngle(float currentAng, float counterval, bool dir)
{
float anglecounter = currentAng;
if (dir) // count up
{
if ((counterval + anglecounter) > 360)
{
anglecounter = (counterval + anglecounter) - 360;
}
else
{
anglecounter += counterval;
}
}
else
{ // count down
if ((anglecounter - counterval) < 0)
{
anglecounter = 360 + (anglecounter - counterval);
}
else
{
anglecounter -= counterval;
}
}
return anglecounter;
}
void getStepperSteps(MECHPART *mpart, float degree, int dir) //(degree position, steppernumber, direction to move (0-fastest route,1-forward,2-backward))
{
if (degree > 0 && abs(degree - mpart->ActiveAngle) != 360 && degree <= 360)
{ // no negative angle
float mStepRes = fullRevolutionAngle / float(mpart->FULLSTEP);
float precRange = 0.55;
bool mostRecentDir = mpart->dirchange;
int stepsToTake = 0;
int i = 0;
int j = 0;
bool ibool = true;
bool jbool = false;
// while (mpart->StepsToTake != 0)
// {
// moveMECHPART(mpart);
// }
if (mpart->StepsToTake != 0) // take care of unreached position
{
//printf("remsteps %d activeangle %f dir %d", mpart->StepsToTake, mpart->ActiveAngle, mpart->activedir);
for (size_t k = 0; k < mpart->StepsToTake; k++)
{
if (mpart->dirchange == ibool)
{
mpart->ActiveAngle = countAngle(mpart->ActiveAngle, mStepRes, false);
}
else if (mpart->dirchange == jbool)
{
mpart->ActiveAngle = countAngle(mpart->ActiveAngle, mStepRes, true);
}
}
mpart->StepsToTake = 0;
//printf(" //adjangle %f \n", mpart->ActiveAngle);
}
float i_tempdeg = mpart->ActiveAngle;
float j_tempdeg = mpart->ActiveAngle;
if (dir == 0 || dir == 1)
{
// degDelta = abs(i_tempdeg - degree);
while ((abs(i_tempdeg - degree) / mStepRes) > precRange)
{
i_tempdeg = countAngle(i_tempdeg, mStepRes, true);
i += 1;
// degDelta = abs(i_tempdeg - degree);
}
}
if (dir == 0 || dir == 2)
{
// degDelta = abs(j_tempdeg - degree);
while ((abs(j_tempdeg - degree) / mStepRes) > precRange)
{
j_tempdeg = countAngle(j_tempdeg, mStepRes, false);
j += 1;
// degDelta = abs(j_tempdeg - degree);
}
}
if (i != 0 && j != 0)
{
if (i <= j)
{ // move forward to angle
stepsToTake = i;
mpart->ActiveAngle = i_tempdeg;
mpart->StepsToTake += stepsToTake;
mpart->dirchange = ibool;
}
else
{ // move backward to angle
stepsToTake = j;
mpart->ActiveAngle = j_tempdeg;
mpart->StepsToTake += stepsToTake;
mpart->dirchange = jbool;
}
}
else if (i > 0)
{ // move forward to angle
stepsToTake = i;
mpart->ActiveAngle = i_tempdeg;
mpart->StepsToTake += stepsToTake;
mpart->dirchange = ibool;
}
else if (j > 0)
{ // move backward to angle
stepsToTake = j;
mpart->ActiveAngle = j_tempdeg;
mpart->StepsToTake += stepsToTake;
mpart->dirchange = jbool;
}
enableStepper(true);
//printf(" calcDir %d \n", mpart->dirchange);
}
}
void enableStepper(bool act)
{
if (act)
{
gpio_put(STM_EN, 0);
}
else
{
gpio_put(STM_EN, 1);
}
}
void connecttoPC(string str)
{
inString = str;
outstr = inString;
// printf(outstr.c_str());
if (inString != "")
{
if (inString.find("moveto=") == 0)
{
inString.replace(inString.find("moveto="), 7, "");
getStepperSteps(&stepperMotor1, stof(inString), 0);
printf("activeangle %f stepstotake %d dir %d \n", stepperMotor1.ActiveAngle, stepperMotor1.StepsToTake, stepperMotor1.activedir);
inString = "";
}
else
{
inString = "";
}
}
}
void readtobuffer()
{
char chr = getchar_timeout_us(0);
if (chr != 255)
{
if (chr == '*')
{
dataavailable = true;
}
// cout << chr << buffcounter << std::endl;
if (buffcounter < sizeof(inbuff))
{
inbuff[buffcounter] = chr;
buffcounter += 1;
}
else
{
buffcounter = 0;
}
}
else
{
dataavailable = false;
}
}
void processinData()
{
if (dataavailable)
{
// cout << inbuff << std::endl;
int tempcnt = 0;
while (inbuff[tempcnt] != '*')
{
inString += inbuff[tempcnt];
tempcnt += 1;
}
// cout << inString << std::endl;
connecttoPC(inString);
buffcounter = 0;
dataavailable = false;
memset(inbuff, 0, sizeof(inbuff));
}
}
static inline void put_steps(uint32_t steps)
{
pio_sm_put_blocking(pio0, 1, steps);
}