blob: bdcd07ef74d57347bdee8be07639a4bd2f79abf0 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <unistd.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5
Brian Silvermanf665d692013-02-17 22:11:39 -08006#include "aos/common/logging/logging.h"
Brian Silverman3204dd82013-03-12 18:42:01 -07007#include "aos/atom_code/init.h"
brians343bc112013-02-10 01:53:46 +00008
9#include "frc971/queues/GyroAngle.q.h"
10
11#define M_PI 3.14159265358979323846264338327
12
13using frc971::sensors::gyro;
14
15int main(){
16 aos::Init();
17 int fd = open("/dev/aschuh0", O_RDONLY);
18 int rate_limit = 0;
19 if (fd < 0) {
20 LOG(ERROR, "No Gyro found.\n");
21 } else {
22 LOG(INFO, "Gyro now connected\n");
23 }
24
25 while (true) {
26 int64_t gyro_value;
27 if (read(fd, (void *)&gyro_value, sizeof(gyro_value)) != sizeof(gyro_value)) {
28 LOG(ERROR, "Could not read gyro errno: %d\n", errno);
29 if (errno == ENODEV || errno == EBADF) {
30 close(fd);
31 while (1) {
32 usleep(1000);
33 fd = open("/dev/aschuh0", O_RDONLY);
34 if (fd > 0) {
35 LOG(INFO, "Found gyro again\n");
36 break;
37 }
38 }
39 }
40 continue;
41 }
42 rate_limit ++;
43 if (rate_limit > 10) {
44 LOG(DEBUG, "Gyro is %d\n", (int)(gyro_value / 16));
45 rate_limit = 0;
46 }
47 gyro.MakeWithBuilder().angle(gyro_value / 16.0 / 1000.0 / 180.0 * M_PI).Send();
48 }
49
50 aos::Cleanup();
51}