blob: 7dbb804b35466f687ad3164279257dd14a7b43f9 [file] [log] [blame]
Sindy Tan9e89a882024-06-29 16:13:59 -07001#ifndef MOTORS_BUTTON_BOARD_H_
2#define MOTORS_BUTTON_BOARD_H_
3
4#include "motors/peripheral/adc.h"
5#include "motors/peripheral/can.h"
6#include "motors/usb/cdc.h"
7#include "motors/usb/hid.h"
8#include "motors/util.h"
9
10#define NUM_ENCODERS 2
11#define ENCODER_COUNTS_PER_REV 0x1000
12#define ENCODER_MOD (ENCODER_COUNTS_PER_REV - 1)
13#define ENCODER_RESET_TIME_MS 500
14#define ENCODER_FILTER_EXPONENT 6 // depth of IIR filtering where val 2^n
15#define MAX_FILTER_DELTA \
16 200 // reset encoder filter when the input value exceeds filtered value by
17 // this amount
18#define MAX_DUTY_CYCLE_DELTA 500
19#define BUTTON_BOARD_COUNT 3
20#define PROCESSOR_ID_COUNT 3 // number of driver stations
21#define PRINT_OFFSETS 0 // used to debug the zero's of the joystick
22#define SCALED_VALUE_MAX 0xFFFF
23
24namespace frc971 {
25namespace motors {
26
27struct JoystickAdcReadings {
28 uint16_t analog0, analog1, analog2, analog3;
29};
30
31enum abs_Measurement_State {
32 INIT_ABS, // Absolute position of encoder on startup, which is treated as
33 // center
34 START_PERIOD, // Period reading needs to stay for to be considered stable.
35 // Rise-to-rise time of PWM
36 WAIT_PERIOD_DONE,
37 START_WIDTH, // Rise-to-fall time of PWM
38 WAIT_WIDTH_DONE
39};
40
41typedef struct {
42 uint16_t enc0_trim;
43 uint16_t enc1_trim;
44} ENCODER_TRIMS_S;
45
46typedef struct {
47 abs_Measurement_State state;
48 uint32_t period;
49 uint32_t width;
50 uint32_t dutycycle;
51 uint32_t last_erronious_dutycycle; // Tracks out-of-range measurements so
52 // that duty cycle can be reset
53 bool intialized;
54} ABS_POSITION_S;
55
56typedef struct {
57 uint16_t angle;
58 uint16_t offset;
59 uint16_t resetTimer_ms;
60 uint16_t filtered_duty_cycle;
61 uint32_t angle_filter;
62 // The encoder value at center.
63 uint16_t enc_trim;
64 // The tared min and max values of the limits.
65 uint16_t enc_min;
66 uint16_t enc_max;
67} ENCODER_DATA_S;
68
69// Identifies which board is used.
70typedef struct {
71 uint32_t board_id; // There are three boards, one for each Teensy
72 uint32_t
73 processor_index; // There are two processors, one for each driver station
74 uint32_t can_id0;
75 uint32_t can_id1;
76} BOARD_CONFIG_S;
77
78typedef struct {
79 uint32_t buttons;
80 uint16_t enc0;
81 uint16_t enc1;
82 uint16_t abs0;
83 uint16_t abs1;
84 uint16_t abs2;
85 uint16_t abs3;
86} MEASUREMENT_DATA_S;
87
88class DriverStation2 {
89 public:
90 DriverStation2() = default;
91 int Run();
92 // Number of bytes per CAN packet
93 static constexpr uint16_t kReportSize = 1 * 5 + 2;
94
95 private:
96 BOARD_CONFIG_S board_config;
97
98 // Contains the while loop to read inputs, pack data, and send it via CAN and
99 // USB
100 void SendJoystickData(teensy::HidFunction *joystick0,
101 teensy::HidFunction *joystick1,
102 teensy::HidFunction *joystick2,
103 uint32_t processor_index);
104 void EnableLeds();
105 void EnableCan();
106 void EnableEncoders();
107 void EnableQD(LittleFTM *ftm, int encoder);
108 void EnableGlitchFilter();
109 void EnableButtons();
110 void DisableLeds();
111 void AdcInitJoystick();
112 JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &);
113 void ComposeReport(char report[][kReportSize],
114 MEASUREMENT_DATA_S *bbMeasurements, uint8_t board_id,
115 int can_1_board, int can_2_board);
116 int ReadQuadrature(int encoderNum, uint16_t *encoderAngle);
117 int MeasureAbsPosition(uint32_t encoder_id, ABS_POSITION_S *abs_position);
118 int DetermineEncoderValues(ENCODER_DATA_S *enc, ABS_POSITION_S *absAngle,
119 int encoderNum, uint16_t resetTime_ms);
120 void ZeroMeasurements(MEASUREMENT_DATA_S *bbMeasurements, uint32_t board);
121 int UpdateMeasurementsFromCAN(MEASUREMENT_DATA_S *bbMeasurements,
122 uint8_t *canRX_data);
123 void PackMeasurementsToCAN(MEASUREMENT_DATA_S *bbMeasurements,
124 uint8_t *canTX_data);
125 uint32_t ReadButtons();
126
127 int MeasurementsToJoystick(MEASUREMENT_DATA_S bbMeasurement);
128};
129} // namespace motors
130} // namespace frc971
131
132#endif // MOTORS_BUTTON_BOARD_H_