forked from Matthias-Wandel/imgcomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_stepper.c
583 lines (490 loc) · 16.7 KB
/
timer_stepper.c
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//----------------------------------------------------------------------------------
// Experimental code for driving steppers based on what imgcomp saw
// Ignore this module
//----------------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN // To keep windows.h bloat down.
#ifdef OLD_WINSOCK
#include <winsock.h>
#else
#include <winsock2.h>
#endif
#include <conio.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/net.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>
#include <memory.h>
typedef unsigned char BYTE;
typedef unsigned char UCHAR;
typedef unsigned short USHORT;
typedef unsigned long ULONG;
typedef int SOCKET;
typedef int BOOL;
#define FALSE 0
#define TRUE 1
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
typedef fd_set FD_SET;
typedef struct timeval TIMEVAL;
#endif
#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0
#define ICMP_MIN 8 // minimum 8 byte icmp packet (just header)
#define MAGIC_ID 0xf581 // Magic value to identify pings from this program.
#define MAGIC_PORTNUM 7777 // Magic port number for UDP pings (this is the port Joe picked for his app)
#define UDP_MAGIC 0x46c1
//-------------------------------------------------------------------------------------
// Structure for a test UDP packet.
typedef struct {
int Ident;
int Level;
int xpos;
int ypos;
int IsAdjust;
}Udp_t;
//-------------------------------------------------------------------------------------
#define STATUS_FAILED 0xFFFF
#define DEF_PACKET_SIZE 32
#define MAX_PACKET 1024
struct sockaddr_in dest;
static SOCKET sockUDP;
static int UdpPayloadLength = -1;
//--------------------------------------------------------------------------
// Form and send a UDP packet
//--------------------------------------------------------------------------
void SendUDP(int Pos, int IsDelta)
{
int datasize;
int wrote;
Udp_t Buf;
memset(&Buf, 0, sizeof(Buf));
Buf.Ident = UDP_MAGIC;
Buf.Level = 1000;
Buf.xpos = Pos;
Buf.ypos = 0;
Buf.IsAdjust = IsDelta;
datasize = sizeof(Udp_t);
wrote = sendto(sockUDP,(char *)&Buf, datasize, 0,(struct sockaddr*)&dest, sizeof(struct sockaddr_in));
if (wrote == SOCKET_ERROR){
perror("UDP sendto failed");
#ifndef _WIN32
printf("On Linux, This eventually happens if the destination port is unreacable and Uping is not root\n");
#endif
exit(-1);
}
if (wrote < datasize ) {
fprintf(stdout,"Wrote %d bytes of %d\n",wrote, datasize);
}
printf("Sent UDP packet, x=%d\n",Pos);
}
//----------------------------------------------------------------------------------
// Command line help...
//----------------------------------------------------------------------------------
void Usage(char *progname)
{
fprintf(stderr,"Usage:\n");
fprintf(stderr,"%s [host] [pos]\n",progname);
fprintf(stderr,"Options:\n");
exit(-1);
}
void RunStepping(void);
void ReadTimer(void);
int PosRequested;
int DeltaRequested;
//--------------------------------------------------------------------------
// Main
//--------------------------------------------------------------------------
int main(int argc, char **argv)
{
#ifdef _WIN32
WSADATA wsaData;
#endif
struct hostent * hp;
unsigned int addr=0;
unsigned short PortNum = MAGIC_PORTNUM;
char * HostName = NULL;
char String[500];
ReadTimer();
return 0;
memset(String, 0, sizeof(String));
if (argc > 1){
HostName = argv[1];
}
if (argc > 2){
DeltaRequested = 0;
if (argv[2][0] == '-') DeltaRequested = 1;
if (argv[2][0] == '+'){
DeltaRequested = 1;
argv[2]++;
}
sscanf(argv[2],"%d",&PosRequested);
}
#ifdef _WIN32
if (WSAStartup(MAKEWORD(1,1),&wsaData) != 0){
fprintf(stderr,"WSAStartup failed: %d\n",GetLastError());
ExitProcess(STATUS_FAILED);
}
#endif
//-------------------------------------------------------------------
// Resolve the remote address
memset(&dest,0,sizeof(struct sockaddr_in));
if (HostName){
hp = gethostbyname(HostName);
if (hp == NULL){
// If address is not a host name, assume its a x.x.x.x format address.
addr = inet_addr(HostName);
if (addr == INADDR_NONE) {
// Not a number format address either. Give up.
fprintf(stderr,"Unable to resolve %s\n",HostName);
exit(-1);
}
dest.sin_addr.s_addr = addr;
dest.sin_family = AF_INET;
}else{
// Resolved to a host name.
memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
dest.sin_family = hp->h_addrtype;
}
}else{
// No host name specified. Use loopback address by default.
printf("No host name specified. Using 'loopback' as target\n");
addr = inet_addr("127.0.0.1");
dest.sin_addr.s_addr = addr;
dest.sin_family = AF_INET;
}
dest.sin_port = htons(PortNum);
//-------------------------------------------------------------------
// Open socket for reception of regular UDP packets.
{
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(PortNum);
if (HostName){
// Bind to a different UDP port if just running it for sending a UDP
// packet because we need the port free for the listening side running
// on the same computer.
local.sin_port = htons(PortNum+1);
}
sockUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockUDP == INVALID_SOCKET){
perror("couldn't create UDP socket");
exit(-1);
}
if (bind(sockUDP,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR) {
#ifdef _WIN32
int err;
err = WSAGetLastError();
if (err != WSAEADDRINUSE){
perror("bind() failed with error");
WSACleanup();
exit(-1);
}else{
printf("Listen port already in use\n");
}
#else
perror("bind failed");
exit(-1);
#endif
}
}
//-------------------------------------------------------------------
if (HostName){
// Just send a UDP packet.
SendUDP(PosRequested, DeltaRequested);
}else{
printf("listening on UDP\n");
RunStepping();
}
return 0;
}
//--------------------------------------------------------------------------
// Check if new UDP packet is here
//--------------------------------------------------------------------------
int CheckUdp(int * NewPos, int * IsDelta)
{
char recvbuf[MAX_PACKET];
FD_SET rws;
int bread;
struct sockaddr_in from;
int fromlen = sizeof(from);
int a;
{
// Check for stuff that might come back.
TIMEVAL timeout;
FD_ZERO(&rws);
FD_SET(sockUDP, &rws);
#ifndef _WIN32
FD_SET(STDIN_FILENO, &rws);
#endif
timeout.tv_sec = 0;
timeout.tv_usec = 500;
a = select(10, &rws, NULL, NULL, &timeout);
if (a == 0){
// There is no received data.
return FALSE;
}
}
// Check the UDP socket.
if (FD_ISSET(sockUDP, &rws)){
memset(recvbuf, 0, sizeof(recvbuf));
bread = recvfrom(sockUDP,recvbuf,MAX_PACKET,0,(struct sockaddr*)&from, &fromlen);
if (bread == SOCKET_ERROR){
// This happens after we sent something out to another machine and the port is
// unavailable. Under Linux, but not under windows, we subsequently get a socket
// error - probably on account of the ICMP port unreachable packet that comes back.
perror("UDP socket error");
#ifndef _WIN32
printf("(could be caused by destination port unreachable)\n");
#endif
}else{
{
Udp_t * Udp;
Udp = (Udp_t *) recvbuf;
printf("UDP from %s ", inet_ntoa(from.sin_addr));
printf("x=%d y=%d %s\n", Udp->xpos, Udp->ypos, Udp->IsAdjust ? "Adj":"");
*NewPos = Udp->xpos;
*IsDelta = Udp->IsAdjust;
}
}
return TRUE;
}
return FALSE;
}
#ifndef _WIN32
//-----------------------------------------------------------------------------------
// This program based on "How to access GPIO registers from C-code on the Raspberry-Pi
// Example program
// Blinks camerae LED on raspberry Pi model B+ and Raspberry Pi 2 model B
//-----------------------------------------------------------------------------------
#define RASPBERRY_PI 1
#if RASPBERRY_PI == 2
// it's a rapsberry pi 2. Peripherals are mapped in a different location.
#define BCM2708_PERI_BASE 0x3f000000 // For Raspberry pi 2 model B
#endif
#if RASPBERRY_PI == 1
#define BCM2708_PERI_BASE 0x20000000 // For raspberry pi model B+
#endif
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define TIMER_BASE (BCM2708_PERI_BASE + 0x3000) /* GPIO controller */
//#include <stdio.h>
//#include <stdlib.h>
//#include <fcntl.h>
//#include <sys/mman.h>
//#include <unistd.h>
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
int mem_fd;
void *gpio_map;
// I/O access
volatile unsigned *gpio;
volatile unsigned *timerreg;
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
#define GPIO_SET2 *(gpio+8) // same as GPI_SET macro, but for GPIO 32 and higher
#define GPIO_CLR2 *(gpio+11) // same as GPI_SET macro, but for GPIO 32 and higher
#define GET_GPIO(g) (*(gpio+13)&(1<<g)) // 0 if LOW, (1<<g) if HIGH
#define GPIO_PULL *(gpio+37) // Pull up/pull down
#define GPIO_PULLCLK0 *(gpio+38) // Pull up/pull down clock
void setup_io();
#include <fcntl.h>
#include <sys/mman.h>
//--------------------------------------------------------------------------------
// Set up a memory regions to access GPIO
//--------------------------------------------------------------------------------
void setup_io()
{
// open /dev/mem
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}
/* mmap GPIO */
gpio_map = mmap(
NULL, //Any adddress in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
TIMER_BASE //Offset to GPIO peripheral
);
close(mem_fd); //No need to keep mem_fd open after mmap
if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map);//errno also set!
exit(-1);
}
gpio = (volatile unsigned *)gpio_map;
}
#define STEP_ENA (1<<21) // GPIO21: Enable
#define STEP_DIR (1<<26) // GPIO26: Direction
#define STEP_CLK (1<<20) // GPIO20: Clock
#endif
int CurrentPos = 0;
int IsEnabled = FALSE;
int Direction = 0;
int StepDelay = 5000;
int CurrentSpeed = 0;
int SpeedTarget;
#define ABS(a) (a > 0 ? a : -(a))
void RunStepping(void)
{
int CurrentSpeed, LastSpeed;
// Set up gpi pointer for direct register access
setup_io();
INP_GPIO(21); // must use INP_GPIO before we can use OUT_GPIO
OUT_GPIO(21);
INP_GPIO(26);
OUT_GPIO(26);
INP_GPIO(20);
OUT_GPIO(20);
GPIO_SET = STEP_ENA | STEP_DIR;
GPIO_CLR = STEP_CLK | STEP_ENA;
GPIO_SET = STEP_ENA;
GPIO_CLR = STEP_CLK;
PosRequested = 0;
CurrentSpeed = LastSpeed = 0;
CurrentPos = 600; // Just to have some initial work.
for (;;){
int ToTarget;
int PosRecvd;
int IsDelta;
if (CheckUdp(&PosRecvd, &IsDelta)){
//printf("UDP pos request: %d\n",PosRecvd);
//PosRecvd = PosRecvd * 9;
PosRecvd = PosRecvd * 32*4;
if (!IsDelta){
// New motion position to aim for.
PosRequested = PosRecvd;
}else{
// Adjust the reference position (fix offsets)
CurrentPos -= PosRecvd;
}
}
GPIO_CLR = STEP_CLK;
ToTarget = PosRequested - CurrentPos;
// Compute speed to try to get to rapm up to, also causing
// slowdown ad approaching target.
if (ToTarget){
SpeedTarget = (int)(sqrt(ABS(ToTarget))*4000);
if (SpeedTarget > 400000) SpeedTarget = 400000;
if (ToTarget < 0) SpeedTarget = - SpeedTarget;
}else{
SpeedTarget = 0;
}
{
// Limit acceleration / deceleration
int SpDiff, SpAdjust;
SpDiff = SpeedTarget-CurrentSpeed;
SpAdjust = SpDiff;
if (StepDelay > 2000) StepDelay = 2000;
//printf("sdely=%d\n",StepDelay);
if (SpAdjust > StepDelay) SpAdjust = StepDelay*2;
if (SpAdjust < -StepDelay) SpAdjust = -StepDelay*2;
CurrentSpeed += SpAdjust;
}
// Come to a halt.
if (ToTarget == 0 && ABS(CurrentSpeed) < 10000){
CurrentSpeed = 0;
}
// Potentially reverse direction output.
if (CurrentSpeed > 0 && LastSpeed <= 0) GPIO_CLR = STEP_DIR;
if (CurrentSpeed < 0 && LastSpeed >= 0) GPIO_SET = STEP_DIR;
if (CurrentSpeed == 0){
if (ToTarget == 0){
// Speed 0. Came to a stop on target
GPIO_CLR = STEP_ENA;
}
usleep(2000);
StepDelay = 2000;
}else{
if (LastSpeed == 0){
// Was stopped. Restart.
GPIO_SET = STEP_ENA;
usleep(1000);
}
StepDelay = 10000000/ABS(CurrentSpeed);
if (StepDelay > 2000) StepDelay = 2000;
// Send out one pulse.
GPIO_SET = STEP_CLK;
usleep(StepDelay);
CurrentPos += CurrentSpeed > 0 ? 1 : -1;
}
//printf("ToTarget = %d Speed=%d st=%d\n",ToTarget, CurrentSpeed,SpeedTarget);
//if (ToTarget == 0 && CurrentSpeed == 0) exit(0);
LastSpeed = CurrentSpeed;
}
}
char OutString[100];
double input, output;
void ReadTimer(void)
{
setup_io();
int time1,time2,a, cycles;
printf("timer is: \n%d\n%d\n",*(timerreg+1),*(timerreg+1));
printf("timer is: \n%d\n%d\n",*(timerreg+1),*(timerreg+1));
for (a=0;a<=50;a++){
time1 = *(timerreg+1);
usleep(a);
//sleep(1);
if (0){
int PosRecvd;
int IsDelta;
CheckUdp(&PosRecvd, &IsDelta);
}
time2 = *(timerreg+1);
printf("usleep %d: ticked %d\n",a,time2-time1);
if (a >=10) a += 4;
if (a >=100) a += 5;
}
printf("looking for delays...\n");
for (cycles=0;;cycles++){
int DelayBins[15];
int StartTime;
int t1,t2,diff;
int missing;
int longest ;
memset(DelayBins, 0, sizeof(DelayBins));;
missing=longest=0;
t2 = StartTime = *(timerreg+1);
for (;;){
t1 = *(timerreg+1);
diff = t1-t2;
if (diff > longest) longest = diff;
if (diff >= 1){
missing += diff-1;
for (a=0;a<15;a++){
diff >>= 1;
if (diff == 0){
DelayBins[a] += 1;
break;
}
}
}
t2 = t1;
if (t2-StartTime > 1000000){
break;
}
}
diff = 1;
printf("%5d ",missing);
for (a=0;a<15;a++){
printf("%4d",DelayBins[a]);
diff <<= 1;
}
printf(" l=%d\n",longest);
}
}