blob: a8585b0ab90ce6d106a408c18ad177425df485df [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 {
Sindy Tan9e89a882024-06-29 16:13:59 -070042 abs_Measurement_State state;
43 uint32_t period;
44 uint32_t width;
45 uint32_t dutycycle;
46 uint32_t last_erronious_dutycycle; // Tracks out-of-range measurements so
47 // that duty cycle can be reset
48 bool intialized;
49} ABS_POSITION_S;
50
51typedef struct {
52 uint16_t angle;
53 uint16_t offset;
54 uint16_t resetTimer_ms;
55 uint16_t filtered_duty_cycle;
56 uint32_t angle_filter;
57 // The encoder value at center.
58 uint16_t enc_trim;
59 // The tared min and max values of the limits.
60 uint16_t enc_min;
61 uint16_t enc_max;
62} ENCODER_DATA_S;
63
64// Identifies which board is used.
65typedef struct {
66 uint32_t board_id; // There are three boards, one for each Teensy
67 uint32_t
68 processor_index; // There are two processors, one for each driver station
69 uint32_t can_id0;
70 uint32_t can_id1;
71} BOARD_CONFIG_S;
72
73typedef struct {
74 uint32_t buttons;
75 uint16_t enc0;
76 uint16_t enc1;
77 uint16_t abs0;
78 uint16_t abs1;
79 uint16_t abs2;
80 uint16_t abs3;
81} MEASUREMENT_DATA_S;
82
Sindy Tanfc771aa2024-07-19 22:38:43 -070083class DriverStation {
Sindy Tan9e89a882024-06-29 16:13:59 -070084 public:
Sindy Tanfc771aa2024-07-19 22:38:43 -070085 DriverStation() = default;
Sindy Tan9e89a882024-06-29 16:13:59 -070086 int Run();
87 // Number of bytes per CAN packet
88 static constexpr uint16_t kReportSize = 1 * 5 + 2;
89
90 private:
91 BOARD_CONFIG_S board_config;
92
93 // Contains the while loop to read inputs, pack data, and send it via CAN and
94 // USB
95 void SendJoystickData(teensy::HidFunction *joystick0,
96 teensy::HidFunction *joystick1,
97 teensy::HidFunction *joystick2,
98 uint32_t processor_index);
99 void EnableLeds();
100 void EnableCan();
101 void EnableEncoders();
102 void EnableQD(LittleFTM *ftm, int encoder);
103 void EnableGlitchFilter();
104 void EnableButtons();
105 void DisableLeds();
106 void AdcInitJoystick();
107 JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &);
108 void ComposeReport(char report[][kReportSize],
109 MEASUREMENT_DATA_S *bbMeasurements, uint8_t board_id,
110 int can_1_board, int can_2_board);
111 int ReadQuadrature(int encoderNum, uint16_t *encoderAngle);
112 int MeasureAbsPosition(uint32_t encoder_id, ABS_POSITION_S *abs_position);
113 int DetermineEncoderValues(ENCODER_DATA_S *enc, ABS_POSITION_S *absAngle,
114 int encoderNum, uint16_t resetTime_ms);
115 void ZeroMeasurements(MEASUREMENT_DATA_S *bbMeasurements, uint32_t board);
116 int UpdateMeasurementsFromCAN(MEASUREMENT_DATA_S *bbMeasurements,
117 uint8_t *canRX_data);
118 void PackMeasurementsToCAN(MEASUREMENT_DATA_S *bbMeasurements,
119 uint8_t *canTX_data);
120 uint32_t ReadButtons();
121
122 int MeasurementsToJoystick(MEASUREMENT_DATA_S bbMeasurement);
123};
124} // namespace motors
125} // namespace frc971
126
127#endif // MOTORS_BUTTON_BOARD_H_