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 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 6 | #include "aos/common/logging/logging.h" |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 7 | #include "aos/atom_code/init.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
| 9 | #include "frc971/queues/GyroAngle.q.h" |
| 10 | |
| 11 | #define M_PI 3.14159265358979323846264338327 |
| 12 | |
| 13 | using frc971::sensors::gyro; |
| 14 | |
| 15 | int 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 | } |