Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 1 | #include "frc971/input/sensor_packer.h" |
| 2 | |
| 3 | #include <arpa/inet.h> |
| 4 | |
| 5 | #include "aos/common/inttypes.h" |
Brian Silverman | c6aa51a | 2013-03-15 17:06:27 -0700 | [diff] [blame] | 6 | #include "aos/common/time.h" |
| 7 | |
| 8 | #include "frc971/control_loops/index/index.h" |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 9 | |
| 10 | using ::aos::MutexLocker; |
| 11 | |
| 12 | namespace frc971 { |
| 13 | |
| 14 | SensorPacker::SensorPacker() : lencoder(1, 2), rencoder(3, 4) { |
| 15 | lencoder.Start(); |
| 16 | rencoder.Start(); |
| 17 | |
| 18 | printf("frc971::SensorPacker started\n"); |
| 19 | } |
| 20 | |
Brian Silverman | c6aa51a | 2013-03-15 17:06:27 -0700 | [diff] [blame] | 21 | void SensorPacker::ReadEncoder(EncoderReadData *data) { |
| 22 | int32_t value = data->counter->Get(); |
| 23 | { |
| 24 | ::aos::MutexLocker locker(data->sync); |
| 25 | *data->output = value; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void SensorPacker::TopDiscEdgeReader() { |
| 30 | while (true) { |
| 31 | top_disc_posedge_output_->source()-> |
| 32 | WaitForInterrupt(kInterruptTimeout); |
| 33 | int32_t position = index_counter_->Get(); |
| 34 | { |
| 35 | aos::MutexLocker locker(&top_disc_edge_sync_); |
| 36 | top_disc_posedge_position_ = position; |
| 37 | ++top_disc_posedge_count_; |
| 38 | } |
| 39 | top_disc_negedge_output_->source()-> |
| 40 | WaitForInterrupt(kInterruptTimeout); |
| 41 | position = index_counter_->Get(); |
| 42 | { |
| 43 | aos::MutexLocker locker(&top_disc_edge_sync_); |
| 44 | top_disc_negedge_position_ = position; |
| 45 | ++top_disc_negedge_count_; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void SensorPacker::BottomDiscEdgeReader() { |
| 51 | static const aos::time::Time kBottomDiscNegedgeSleep = |
| 52 | aos::time::Time::InSeconds( |
| 53 | control_loops::IndexMotor::kBottomDiscIndexDelay); |
| 54 | while (true) { |
| 55 | bottom_disc_->source()->WaitForInterrupt(kInterruptTimeout); |
| 56 | if (bottom_disc_->Get()) { |
| 57 | aos::time::Time start = aos::time::Time::Now(); |
| 58 | { |
| 59 | aos::MutexLocker locker(&bottom_disc_edge_sync_); |
| 60 | ++bottom_disc_negedge_count_; |
| 61 | } |
| 62 | aos::time::SleepUntil(start + kBottomDiscNegedgeSleep); |
| 63 | int32_t position = index_counter_->Get(); |
| 64 | { |
| 65 | aos::MutexLocker locker(&bottom_disc_edge_sync_); |
| 66 | bottom_disc_negedge_wait_position_ = position; |
| 67 | ++bottom_disc_negedge_wait_count_; |
| 68 | } |
| 69 | } else { |
| 70 | { |
| 71 | aos::MutexLocker locker(&bottom_disc_edge_sync_); |
| 72 | ++bottom_disc_posedge_count_; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Brian Silverman | 3204dd8 | 2013-03-12 18:42:01 -0700 | [diff] [blame] | 78 | void SensorPacker::PackInto(sensor_values *values) { |
| 79 | values->lencoder = htonl(-lencoder.GetRaw()); |
| 80 | values->rencoder = -htonl(-rencoder.GetRaw()); |
| 81 | } |
| 82 | |
| 83 | } // namespace frc971 |