-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathporttracker.cpp
292 lines (240 loc) · 7.67 KB
/
porttracker.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
/************************************************************************/
/* */
/* P R O J E K T A V A L O N */
/* */
/* porttracker.cpp Checks the Port Numbers for the EPOS */
/* */
/* Author Stefan Wismer */
/* wismerst@student.ethz.ch */
/* */
/************************************************************************/
/**
* This checks all the serial ports if it can find an EPOS,
* reads the serial number and writes to the store which
* EPOS is which.
**/
// General Project Constants
#include "avalon.h"
// General Things
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// General rtx-Things
#include <rtx/getopt.h>
#include <rtx/main.h>
#include <rtx/error.h>
#include <rtx/timer.h>
#include <rtx/thread.h>
#include <rtx/message.h>
#include <DDXStore.h>
#include <DDXVariable.h>
// Specific Things
#include "epos/epos.h"
#include "can/can.h"
#include "ports.h"
#include "RudderMotor.h"
/**
* Some Function prototype
* */
bool file_exists(const char * filename);
/**
* Global variable for all DDX object
* */
DDXStore store;
DDXVariable dataPorts;
// These are not acutally a rudder motor, just a generic motor.
RudderMotor motor;
/**
* Storage for the command line arguments
* */
const char * varname = "ports";
const char * consumerHelpStr = "This checks which port is which EPOS";
/**
* Command line arguments
*
* */
RtxGetopt producerOpts[] = {
{"portsname", "Store-Variable where the ports are",
{
{RTX_GETOPT_STR, &varname, ""},
RTX_GETOPT_END_ARG
}
},
RTX_GETOPT_END
};
/**
* Working thread, does nothing.
* */
void * rudderdrive_thread(void * dummy)
{
/*
rudderTarget rudder;
Rudderstate rudderstate;
int feedback = 0;
bool left_pressed = false, right_pressed = false;
rudderstate.ref_rudder = 0;
dataRudderState.t_writefrom(rudderstate);
while (1) {
// Read the next data available, or wait at most 10 seconds
if (dataRudder.t_readto(rudder,10.0,1))
{
// Get old Encoder values and joystick data from the store
// dataRudderState.t_readto(rudderState,0,0);
dataRudderState.t_readto(rudderstate,0,0);
// Go there!!
if(rudderside == AV_RUDDER_LEFT) {
motor.move_to_angle(1, rudder.degrees_left, rudderstate.ref_rudder, feedback);
rudderstate.degrees_rudder = feedback / AV_RUDDER_TICKS_PER_DEGREE;
}
else if(rudderside == AV_RUDDER_RIGHT) {
motor.move_to_angle(1, rudder.degrees_right, rudderstate.ref_rudder, feedback);
rudderstate.degrees_rudder = feedback / AV_RUDDER_TICKS_PER_DEGREE;
}
// Reset Options
if(rudder.resetleft_request && rudderside == AV_RUDDER_LEFT && !left_pressed)
{
rtx_message("Resetting left rudder ... ");
rudderstate.ref_rudder = rudderstate.degrees_rudder;
left_pressed = true;
}
else if(!rudder.resetleft_request && rudderside == AV_RUDDER_LEFT)
{
left_pressed = false;
}
if(rudder.resetright_request && rudderside == AV_RUDDER_RIGHT && !right_pressed)
{
rtx_message("Resetting right rudder ... ");
rudderstate.ref_rudder = rudderstate.degrees_rudder;
right_pressed = true;
}
else if(!rudder.resetleft_request && rudderside == AV_RUDDER_RIGHT)
{
right_pressed = false;
}
// Bring Encoder and reference values to Store
dataRudderState.t_writefrom(rudderstate);
} else if (dataRudder.hasTimedOut()) {
// Timeout. Probably no joystick connected.
rtx_message("Timeout while reading Rudder-Target data.\n");
} else {
// Something strange happend. Critical Error.
rtx_error("Critical error while reading data");
// Emergency-Stop
rtx_main_signal_shutdown();
}
}
*/
return NULL;
}
// Error handling for C functions (return 0 on success)
#define DOC(c) {int ret = c;if (ret != 0) {rtx_error("Command "#c" failed with value %d",ret);return -1;}}
// Error handling for C++ function (return true on success)
#define DOB(c) if (!(c)) {rtx_error("Command "#c" failed");return -1;}
// Error handling for pointer-returning function (return NULL on failure)
#define DOP(c) if ((c)==NULL) {rtx_error("Command "#c" failed");return -1;}
int
main (int argc, const char * argv[])
{
// RtxThread * th;
int ret;
int nofound = 0;
char device[99];
int found_so_far = 0;
Ports ports = {0,0,0};
// Process the command line
if ((ret = RTX_GETOPT_CMD (producerOpts, argc, argv, NULL, consumerHelpStr)) == -1) {
RTX_GETOPT_PRINT (producerOpts, argv[0], NULL, consumerHelpStr);
exit (1);
}
rtx_main_init ("Porttracker Main", RTX_ERROR_STDERR);
// Open the store
DOB(store.open());
// Register Datatypes
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), Ports));
// Connect to rudder-target-variable, and create variables for the target-data
DOB(store.registerVariable(dataPorts, varname, "Ports"));
// INIT-SEQUENCE EPOS-MOTORS
rtx_message("EPOS - Serial-Port Tracker...");
int i = 0;
while(i < AV_NUMBER_OF_SERIAL_PORTS)
{
if(nofound > 10)
{
break;
}
sprintf(device, "/dev/ttyUSB%d", i);
printf("Testing device %s...\n", device);
if(!file_exists(device))
{
i++;
nofound++;
continue;
}
if(!motor.init(1, device))
{
printf("%s has no EPOS\n",device);
}
else
{
printf("%s has a EPOS\n",device);
switch(found_so_far)
{
case 0:
printf("Rudder left found at %s\n",device);
dataPorts.t_readto(ports,1.0,1);
ports.rudderright = i;
dataPorts.t_writefrom(ports);
break;
case 1:
printf("Rudder right found at %s\n",device);
dataPorts.t_readto(ports,1.0,1);
ports.rudderleft = i;
dataPorts.t_writefrom(ports);
break;
case 2:
printf("Sail found at %s\n",device);
dataPorts.t_readto(ports,1.0,1);
ports.sail = i;
dataPorts.t_writefrom(ports);
break;
default:
printf("ERROR - something went wrong!!");
// TODO: Process this error!
return 0;
}
found_so_far++;
}
i++;
}
// Bring to store (all at once)
printf("Writing to store...\n");
//////////////
// This Program needs no working thread, so this section becomes
// pointles...
// ///////////
/*
// Start the working thread
DOP(th = rtx_thread_create ("Ruddermotor driver thread", 0,
RTX_THREAD_SCHED_OTHER, RTX_THREAD_PRIO_MIN, 0,
RTX_THREAD_CANCEL_DEFERRED,
rudderdrive_thread, NULL,
NULL, NULL));
// Wait for Ctrl-C
DOC (rtx_main_wait_shutdown (0));
rtx_message_routine ("Ctrl-C detected. Shutting down Rudder-Motor Driver...");
// Terminating the thread
rtx_thread_destroy_sync (th);
*/
// The destructors will take care of cleaning up the memory
return (0);
}
bool file_exists(const char * filename)
{
if (FILE * file = fopen(filename, "r"))
{
fclose(file);
return true;
}
return false;
}