File: mod_gyro.c

package info (click to toggle)
spl 1.0~pre6-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,364 kB
  • ctags: 2,070
  • sloc: ansic: 16,614; yacc: 3,182; sh: 299; makefile: 167; xml: 156
file content (347 lines) | stat: -rw-r--r-- 9,493 bytes parent folder | download | duplicates (3)
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
/*
 *  SPL - The SPL Programming Language
 *  Copyright (C) 2006 Clifford Wolf <clifford@clifford.at>
 *  Copyright (C) 2007 Raphael Langerhorst <raphael@raphael.g-system.at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  mod_gyro.c: Gyroscope 3D ADC board.
 */

/**
 * SPL Gyroscope Sensor Module
 *
 * This Module provides an interface to the Gyroscope Sensor Board.
 * The board has 16 channels and detects 3D translation and rotation.
 * Compass sensors are also on board.
 *
 * Some board specific documentation:
 *
 * The sensor board can be connected through RS232 or USB.
 * The board has 16 channels.
 * To read a certain number of channels once, send:
 *   1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F or G
 * as a character (note: from SPL use integers 1 to 16).
 * The result comes back as 2 bytes per channel, unsigned, MSB first.
 * (note: in SPL the result is an array with numbers)
 *
 * For example reading 3 channels can be done by sending '3'.
 * Then a string with 6 characters can be read with 2 bytes for each channel.
 * (note: in SPL it is done by sending 3 and receiving an array with 3 values).
 *
 *
 * This module translates the integers 10 to 16 to the characters 'A' to 'G'
 * before sending to the board.
 *
 * Received data is translated into an array of values. The number of elements
 * corresponds to the number of channels requested.
 *
 * You can use gyro_prefetch_state to request a certain number of channels
 * without waiting for the result. Then some other processing can be done
 * before calling gyro_state which will read the prefetched state and thus
 * return faster. However, using this method the time at which the values
 * are valid is when calling gyro_prefetch_state, not gyro_state.
 * In most cases the difference can be ignored unless there is heavy
 * processing done between prefetch_state and state calls.
 *
 * Note: due to an unknown reason the first read result is bad and discarded
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

#include <poll.h>
#include <errno.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "spl.h"
#include "compat.h"


extern void SPL_ABI(spl_mod_gyro_init)(struct spl_vm *vm, struct spl_module *mod, int restore);
extern void SPL_ABI(spl_mod_gyro_done)(struct spl_vm *vm, struct spl_module *mod);

/**
 * Opens the serial (tty) device for communication.
 *
 * The single argument must be the full path to the device file.
 *
 * The return value is the file descriptor (fd) for the serial device.
 * On failure, undef is returned.
 *
 * This file descriptor must be provided to all other gyro functions as first argument.
 */
// builtin gyro_open(devicename)
static struct spl_node *handler_gyro_open(struct spl_task *task, void *data UNUSED)
{
	char* tty_name = spl_clib_get_string(task);
	if (tty_name == NULL || strlen(tty_name) <= 0)
	{
		spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Gyro devicename not given\n");
		return 0;
	}
	int fd = open(tty_name, O_RDWR|O_NDELAY|O_NOCTTY);
	if (fd <= 0)
	{
		spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Could not open gyro device\n");
		return 0;
	}

	struct termios tty;
	tcgetattr(fd, &tty);
	cfsetospeed(&tty, (speed_t)B9600);
	cfsetispeed(&tty, (speed_t)B9600);
	tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
	tty.c_iflag =  IGNBRK;
	tty.c_lflag = 0;
	tty.c_oflag = 0;
	tty.c_cflag |= CLOCAL | CREAD;
	tty.c_cflag &= ~CRTSCTS;
	tty.c_cc[VMIN] = 1;
	tty.c_cc[VTIME] = 5;
	tty.c_iflag &= ~(IXON|IXOFF|IXANY);
	tty.c_cflag &= ~(PARENB | PARODD);
	tty.c_cflag &= ~CSTOPB;

	tcsetattr(fd, TCSANOW, &tty);

	return SPL_NEW_INT(fd);
}

char gyro_translate_channels_native(int channels)
{
	switch(channels)
	{
		case 1:
			return '1';
		case 2:
			return '2';
		case 3:
			return '3';
		case 4:
			return '4';
		case 5:
			return '5';
		case 6:
			return '6';
		case 7:
			return '7';
		case 8:
			return '8';
		case 9:
			return '9';
		case 10:
			return 'A';
		case 11:
			return 'B';
		case 12:
			return 'C';
		case 13:
			return 'D';
		case 14:
			return 'E';
		case 15:
			return 'F';
		case 16:
			return 'G';
		default:
			return '1';
	}
}

/**
 * Sends a request to the device to read the state of given number of channels.
 * This function does not wait for the result to be ready and returns immediately.
 *
 * Thus it can be used to avoid having to wait for the result when using gyro_state only.
 */
// builtin gyro_prefetch_state(fd,channels)
static struct spl_node *handler_gyro_prefetch_state(struct spl_task *task, void *data UNUSED)
{
	int fd = spl_clib_get_int(task);
	int channels = spl_clib_get_int(task);
	if (fd <= 0 || channels <= 0 || channels > 16)
	{
		spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Invalid fd or channels parameters\n");
		return 0;
	}
	char c = gyro_translate_channels_native(channels);
	if (write(fd,&c,1) == -1)
	{
		spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Error writing to gyro device file descriptor\n");
	}

	return 0;
}

int gyro_poll_tty(int fd, int timeout)
{
  struct pollfd pfd;
  pfd.fd = fd;
  pfd.events = POLLIN;
  pfd.revents = 0;
  
  int bytes_ready = poll(&pfd,1,timeout);
  
  if ((pfd.revents & POLLIN) > 0 && bytes_ready == 1)
    return 1;
  else
    return 0;
}

/**
 * Reads given number of channels from device.
 * Returns an array with the read values.
 */
// builtin gyro_state(fd,channels)
static struct spl_node *handler_gyro_state(struct spl_task *task, void *data UNUSED)
{

	int fd = spl_clib_get_int(task);
	int channels = spl_clib_get_int(task);
	// 1) attempt to read given number of channels
	// 2) if read fails, drop any partial result and reread current state

	if (fd <= 0 || channels <= 0 || channels > 16)
	{
		spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"gyro device file descriptor not given or number of channels invalid\n");
		return 0;
	}
	char state[channels * 2]; // channels a 2 byte, no \0 needed 

	int result = 0;
	int writecount = 0;

	while (result < channels * 2)
	{
		int new_result = 0;

	 	if (gyro_poll_tty(fd,30) == 0)
		{
			if (writecount <= 1)
			{
				// write again
				char c = gyro_translate_channels_native(channels);
				result = write(fd,&c,1); 

				if (result == -1)
				{
					spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Error writing to gyro device\n");
					return 0;
				}
				result = 0;
				writecount++;
			}
			else
			{
				spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Could not read state from gyro device\n");
				return 0;
			}
		}

		if ( (new_result = read(fd, state + result, 2*channels - result)) != -1 && new_result != 0 )
		{
			result += new_result;
		}
		else
		{
			spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Error writing to gyro device\n");
			return 0;
		}
	}

	// state array filled now, build state array -- see mod_wscons for nested nodes
	struct spl_node *state_array = spl_get(0);
	int i;
	char channel_num[10];
	int value;
	for (i = 0; i < channels; i++)
	{
		sprintf(channel_num,"ch%d",i);

		// TODO: check if this is correct (done)
		// docs say: 2 bytes per channel, unsigned, MSB first
		// Steuerzeichen: 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G
		value = state[2*i] << 8;
		value += state[2*i+1];

		spl_create(task, state_array, channel_num,  SPL_NEW_INT(value),  SPL_CREATE_LOCAL);
	}

	// read pending junk
	while (gyro_poll_tty(fd,1) == 1)
	{
		char t;
		read(fd,&t,1);
	}

	return state_array;
}

/**
 * Closes the serial device.
 */
// builtin gyro_close(fd)
static struct spl_node *handler_gyro_close(struct spl_task *task, void *data UNUSED)
{
	int fd = spl_clib_get_int(task);
	close(fd);
	return 0;
}



// the SIGIO handler doesn't do anything in particular,
// it's enough to wake up the process from task_sleep()
void gyro_sigio_handler()
{
	//spl_report(SPL_REPORT_RUNTIME, task,"sigio\n");
	//printf("sigio\n");
}

static struct spl_node *handler_gyro_sigio(struct spl_task *task, void *data UNUSED)
{
	struct sigaction sigstruct;
	sigstruct.sa_sigaction = 0;
	sigstruct.sa_handler = gyro_sigio_handler;
	sigstruct.sa_flags = 0;
	if (sigaction(SIGIO,&sigstruct,0)==-1)
	{
		spl_report(SPL_REPORT_RUNTIME|SPL_REPORT_WARNING, task,"Could setup SIGIO handler in gyro module\n");
	}
	return 0;
}


void SPL_ABI(spl_mod_gyro_init)(struct spl_vm *vm, struct spl_module *mod UNUSED, int restore UNUSED)
{
  spl_clib_reg(vm, "gyro_open", handler_gyro_open, 0);
  spl_clib_reg(vm, "gyro_prefetch_state", handler_gyro_prefetch_state, 0);
  spl_clib_reg(vm, "gyro_state", handler_gyro_state, 0);
  spl_clib_reg(vm, "gyro_close", handler_gyro_close, 0);
  spl_clib_reg(vm, "gyro_wake_on_sigio", handler_gyro_sigio, 0);
}

void SPL_ABI(spl_mod_gyro_done)(struct spl_vm *vm UNUSED, struct spl_module *mod UNUSED)
{
  return;
}