blob: 865b25bc3703525aa2efe7c3322944f40f43d128 [file] [log] [blame]
Brian Silverman3204dd82013-03-12 18:42:01 -07001#include "frc971/input/sensor_packer.h"
2
3#include <arpa/inet.h>
4
5#include "aos/common/inttypes.h"
Brian Silvermanc6aa51a2013-03-15 17:06:27 -07006#include "aos/common/time.h"
7
8#include "frc971/control_loops/index/index.h"
Brian Silverman3204dd82013-03-12 18:42:01 -07009
10using ::aos::MutexLocker;
11
12namespace frc971 {
13
14SensorPacker::SensorPacker() : lencoder(1, 2), rencoder(3, 4) {
15 lencoder.Start();
16 rencoder.Start();
17
18 printf("frc971::SensorPacker started\n");
19}
20
Brian Silvermanc6aa51a2013-03-15 17:06:27 -070021void 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
29void 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
50void 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 Silverman3204dd82013-03-12 18:42:01 -070078void SensorPacker::PackInto(sensor_values *values) {
79 values->lencoder = htonl(-lencoder.GetRaw());
80 values->rencoder = -htonl(-rencoder.GetRaw());
81}
82
83} // namespace frc971