-
Notifications
You must be signed in to change notification settings - Fork 0
/
krad_control_receiver.c
279 lines (202 loc) · 7.4 KB
/
krad_control_receiver.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <malloc.h>
#include <time.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sched.h>
#include <unistd.h>
#include <stdint.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>
#include <phidget21.h>
typedef struct krad_receiver_St krad_receiver_t;
struct krad_receiver_St {
int sd;
char *data;
int port;
struct sockaddr_in local_address;
struct sockaddr_in remote_address;
};
krad_receiver_t *krad_receiver_create (int port) {
krad_receiver_t *krad_receiver = calloc(1, sizeof(krad_receiver_t));
krad_receiver->data = calloc(1, 2048);
strcpy ((char *)krad_receiver->data, "KC");
krad_receiver->port = port;
krad_receiver->sd = socket (AF_INET, SOCK_DGRAM, 0);
memset((char *) &krad_receiver->local_address, 0, sizeof(krad_receiver->local_address));
krad_receiver->local_address.sin_family = AF_INET;
krad_receiver->local_address.sin_port = htons (krad_receiver->port);
krad_receiver->local_address.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind (krad_receiver->sd, (struct sockaddr *)&krad_receiver->local_address, sizeof(krad_receiver->local_address)) == -1 ) {
printf("bind error\n");
exit(1);
}
return krad_receiver;
}
void krad_receiver_destroy (krad_receiver_t *krad_receiver) {
close (krad_receiver->sd);
free (krad_receiver->data);
free (krad_receiver);
}
int CCONV AttachHandler(CPhidgetHandle ADVSERVO, void *userptr)
{
int serialNo;
const char *name;
CPhidget_getDeviceName (ADVSERVO, &name);
CPhidget_getSerialNumber(ADVSERVO, &serialNo);
printf("%s %10d attached!\n", name, serialNo);
printf("%s %10d attached!\n", name, serialNo);
return 0;
}
int CCONV DetachHandler(CPhidgetHandle ADVSERVO, void *userptr)
{
int serialNo;
const char *name;
CPhidget_getDeviceName (ADVSERVO, &name);
CPhidget_getSerialNumber(ADVSERVO, &serialNo);
printf("%s %10d attached!\n", name, serialNo);
return 0;
}
int CCONV ErrorHandler(CPhidgetHandle ADVSERVO, void *userptr, int ErrorCode, const char *Description)
{
printf("Error handled. %d - %s\n", ErrorCode, Description);
return 0;
}
int CCONV PositionChangeHandler(CPhidgetAdvancedServoHandle ADVSERVO, void *usrptr, int Index, double Value)
{
// printf("Motor: %d > Current Position: %f\n", Index, Value);
return 0;
}
//Display the properties of the attached phidget to the screen. We will be displaying the name, serial number and version of the attached device.
int display_properties(CPhidgetAdvancedServoHandle phid)
{
int serialNo, version, numMotors;
const char* ptr;
CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
CPhidgetAdvancedServo_getMotorCount (phid, &numMotors);
printf("%s\n", ptr);
printf("Serial Number: %10d\nVersion: %8d\n# Motors: %d\n", serialNo, version, numMotors);
return 0;
}
int krad_receiver_run (krad_receiver_t *krad_receiver) {
int rsize;
int result;
int ret;
double curr_pos;
const char *err;
double Accel, maxVel;
int servo;
double min_pos, max_pos;
float middle_pos, pos_range;
int num_axes, num_buttons, num_balls, num_hats;
int endit;
float x, y;
float val;
CPhidgetMotorControlHandle motoControl = 0;
CPhidgetAdvancedServoHandle servo_controller;
//create the motor control object
CPhidgetMotorControl_create(&motoControl);
CPhidget_open((CPhidgetHandle)motoControl, -1);
printf("Waiting for MotorControl to be attached....");
if((result = CPhidget_waitForAttachment((CPhidgetHandle)motoControl, 10000)))
{
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}
servo_controller = 0;
endit = 0;
servo = 0;
val = 0;
min_pos = 76.0;
max_pos = 149.0;
//servo = 7;
//min_pos = 0.0;
//max_pos = 233.0;
//create the advanced servo object
CPhidgetAdvancedServo_create(&servo_controller);
//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
CPhidget_set_OnAttach_Handler((CPhidgetHandle)servo_controller, AttachHandler, NULL);
CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo_controller, DetachHandler, NULL);
CPhidget_set_OnError_Handler((CPhidgetHandle)servo_controller, ErrorHandler, NULL);
//Registers a callback that will run when the motor position is changed.
//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
CPhidgetAdvancedServo_set_OnPositionChange_Handler(servo_controller, PositionChangeHandler, NULL);
//open the device for connections
CPhidget_open((CPhidgetHandle)servo_controller, -1);
//get the program to wait for an advanced servo device to be attached
printf("Waiting for Phidget to be attached....");
if((result = CPhidget_waitForAttachment((CPhidgetHandle)servo_controller, 10000)))
{
//endwin();
CPhidget_getErrorDescription(result, &err);
printf("Problem waiting for attachment: %s\n", err);
return 0;
}
//Display the properties of the attached device
display_properties(servo_controller);
//Set up some initial acceleration and velocity values
CPhidgetAdvancedServo_getAccelerationMax(servo_controller, servo, &Accel);
CPhidgetAdvancedServo_setAcceleration(servo_controller, servo, Accel - (Accel/3));
CPhidgetAdvancedServo_getVelocityMax(servo_controller, servo, &maxVel);
CPhidgetAdvancedServo_setVelocityLimit(servo_controller, servo, maxVel - (maxVel/3));
middle_pos = (min_pos + max_pos) / 2.0;
pos_range = max_pos - middle_pos;
curr_pos = middle_pos;
CPhidgetAdvancedServo_setPosition (servo_controller, servo, curr_pos);
CPhidgetAdvancedServo_setEngaged(servo_controller, servo, 1);
CPhidgetMotorControl_setAcceleration (motoControl, 0, 10.00);
while (1) {
ret = recvfrom(krad_receiver->sd, krad_receiver->data, 2000, 0, (struct sockaddr *)&krad_receiver->remote_address, (socklen_t *)&rsize);
if (ret == -1) {
printf("failed recvin udp\n");
printf( "errrr: %s\n", strerror( errno ) );
return 1;
}
printf ("got packet! %s\n", krad_receiver->data);
val = atof (krad_receiver->data + 3);
if (krad_receiver->data[2] == 'S') {
CPhidgetAdvancedServo_setPosition (servo_controller, servo, val);
}
if (krad_receiver->data[2] == 'T') {
CPhidgetMotorControl_setVelocity (motoControl, 0, val);
CPhidgetMotorControl_setVelocity (motoControl, 1, val);
}
}
printf("endit is %d\n", endit);
CPhidgetAdvancedServo_setEngaged(servo_controller, servo, 0);
//since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
printf("Closing servo...\n");
CPhidget_close((CPhidgetHandle)servo_controller);
CPhidget_delete((CPhidgetHandle)servo_controller);
printf("Closing motor...\n");
CPhidget_close((CPhidgetHandle)motoControl);
CPhidget_delete((CPhidgetHandle)motoControl);
//all done, exit
return 0;
}
int main(int argc, char* argv[]) {
krad_receiver_t *krad_receiver;
if (argc != 2) {
printf ("%s [port]", argv[0]);
exit (1);
}
krad_receiver = krad_receiver_create (atoi(argv[1]));
krad_receiver_run (krad_receiver);
krad_receiver_destroy (krad_receiver);
return 0;
}