blob: 873c33bf402ea28968f9dca5a2c189703f7f4f0f [file] [log] [blame]
Brian Silverman3204dd82013-03-12 18:42:01 -07001#include "frc971/input/sensor_unpacker.h"
2
3#include <arpa/inet.h>
Brian Silverman687f5242013-03-16 13:57:59 -07004#include <math.h>
Brian Silverman3204dd82013-03-12 18:42:01 -07005
6#include "aos/common/inttypes.h"
7
Austin Schuh48319c52013-03-19 06:22:46 +00008#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman687f5242013-03-16 13:57:59 -07009#include "frc971/control_loops/wrist/wrist_motor.q.h"
10#include "frc971/control_loops/angle_adjust/angle_adjust_motor.q.h"
11#include "frc971/control_loops/index/index_motor.q.h"
12#include "frc971/control_loops/shooter/shooter_motor.q.h"
Brian Silverman3204dd82013-03-12 18:42:01 -070013
Brian Silverman687f5242013-03-16 13:57:59 -070014#ifndef M_PI
Brian Silverman3204dd82013-03-12 18:42:01 -070015#define M_PI 3.14159265358979323846
Brian Silverman687f5242013-03-16 13:57:59 -070016#endif
Brian Silverman3204dd82013-03-12 18:42:01 -070017
18using ::frc971::control_loops::drivetrain;
Brian Silverman687f5242013-03-16 13:57:59 -070019using ::frc971::control_loops::wrist;
20using ::frc971::control_loops::angle_adjust;
21using ::frc971::control_loops::shooter;
22using ::frc971::control_loops::index_loop;
Brian Silverman3204dd82013-03-12 18:42:01 -070023
24namespace frc971 {
25namespace {
26
27inline double drivetrain_translate(int32_t in) {
28 // TODO(2013) fix the math
29 return static_cast<double>(in) / (256.0 * 4.0 * 44.0 / 32.0) *
30 (3.5 * 2.54 / 100.0 * M_PI);
31}
32
Brian Silverman687f5242013-03-16 13:57:59 -070033inline double wrist_translate(int32_t in) {
34 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*quad*/) *
35 (14.0 / 50.0 * 20.0 / 84.0) /*gears*/ * (2 * M_PI);
36}
37
38inline double angle_adjust_translate(int32_t in) {
39 static const double kCableDiameter = 0.060;
40 return -static_cast<double>(in) / (256.0 /*cpr*/ * 2.0 /*2x*/) *
41 ((0.75 + kCableDiameter) / (16.61125 + kCableDiameter)) /*pulleys*/ *
42 (2 * M_PI);
43}
44
45inline double shooter_translate(int32_t in) {
46 return -static_cast<double>(in) / (32.0 /*cpr*/ * 4.0 /*quad*/) *
47 (15.0 / 34.0) /*gears*/ * (2 * M_PI);
48}
49
50inline double index_translate(int32_t in) {
51 return -static_cast<double>(in) / (128.0 /*cpr*/ * 2.0 /*2x*/) *
52 (1.0) /*gears*/ * (2 * M_PI);
53}
54
Brian Silverman3204dd82013-03-12 18:42:01 -070055} // namespace
56
57SensorUnpacker::SensorUnpacker() {}
58
59void SensorUnpacker::UnpackFrom(sensor_values *values) {
Brian Silverman687f5242013-03-16 13:57:59 -070060 for (size_t i = 0;
61 i < sizeof(values->encoders) / sizeof(values->encoders[0]); ++i) {
Brian Silverman3204dd82013-03-12 18:42:01 -070062 values->encoders[i] = ntohl(values->encoders[i]);
63 }
64
65 // TODO(aschuh): Convert to meters.
Brian Silvermanc6aa51a2013-03-15 17:06:27 -070066 const double left_encoder = drivetrain_translate(
67 values->drive_left_encoder);
68 const double right_encoder = drivetrain_translate(
69 values->drive_right_encoder);
Brian Silverman3204dd82013-03-12 18:42:01 -070070 drivetrain.position.MakeWithBuilder()
71 .left_encoder(left_encoder)
72 .right_encoder(right_encoder)
73 .Send();
Brian Silverman687f5242013-03-16 13:57:59 -070074
75 wrist.position.MakeWithBuilder()
76 .pos(wrist_translate(values->wrist_position))
77 .hall_effect(!values->wrist_hall_effect)
78 .calibration(wrist_translate(values->wrist_edge_position))
79 .Send();
80
81 angle_adjust.position.MakeWithBuilder()
82 .angle(angle_adjust_translate(values->angle_adjust_position))
83 .bottom_hall_effect(!values->angle_adjust_bottom_hall_effect)
84 .middle_hall_effect(!values->angle_adjust_middle_hall_effect && false)
85 .bottom_calibration(angle_adjust_translate(
86 values->angle_adjust_bottom_edge_position))
87 .middle_calibration(angle_adjust_translate(
88 values->angle_adjust_middle_edge_position))
89 .Send();
90
91 shooter.position.MakeWithBuilder()
92 .position(shooter_translate(values->shooter_encoder))
93 .Send();
94
95 index_loop.position.MakeWithBuilder()
96 .index_position(index_translate(values->index_encoder))
97 .top_disc_detect(!values->top_disc)
98 .top_disc_posedge_count(values->top_disc_posedge_count)
99 .top_disc_posedge_position(index_translate(
100 values->top_disc_posedge_position))
101 .top_disc_negedge_count(values->top_disc_negedge_count)
102 .top_disc_negedge_position(index_translate(
103 values->top_disc_negedge_position))
104 .bottom_disc_detect(!values->bottom_disc)
105 .bottom_disc_posedge_count(values->bottom_disc_posedge_count)
106 .bottom_disc_negedge_count(values->bottom_disc_negedge_count)
107 .bottom_disc_negedge_wait_position(index_translate(
108 values->bottom_disc_negedge_wait_position))
109 .bottom_disc_negedge_wait_count(values->bottom_disc_negedge_wait_count)
110 .Send();
Brian Silverman3204dd82013-03-12 18:42:01 -0700111}
112
113} // namespace frc971