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