Split out ADC code into a separate file for pistol grip
Change-Id: Ie6401ef3fbfd1c33f7e46532a9624acb78a97bc6
diff --git a/motors/pistol_grip/BUILD b/motors/pistol_grip/BUILD
index 5bdda25..1946c55 100644
--- a/motors/pistol_grip/BUILD
+++ b/motors/pistol_grip/BUILD
@@ -28,6 +28,8 @@
name = "controller.elf",
srcs = [
"controller.cc",
+ "controller_adc.cc",
+ "controller_adc.h",
"vtable_trigger0.cc",
"vtable_trigger1.cc",
"vtable_wheel0.cc",
diff --git a/motors/pistol_grip/controller.cc b/motors/pistol_grip/controller.cc
index b9e6683..b52a3dd 100644
--- a/motors/pistol_grip/controller.cc
+++ b/motors/pistol_grip/controller.cc
@@ -10,8 +10,8 @@
#include "frc971/control_loops/drivetrain/integral_haptic_wheel.h"
#include "motors/core/time.h"
#include "motors/motor.h"
-#include "motors/peripheral/adc.h"
#include "motors/peripheral/can.h"
+#include "motors/pistol_grip/controller_adc.h"
#include "motors/pistol_grip/motor_controls.h"
#include "motors/print/print.h"
#include "motors/util.h"
@@ -70,106 +70,6 @@
}
}
-struct SmallAdcReadings {
- uint16_t currents[3];
-};
-
-struct SmallInitReadings {
- uint16_t motor0_abs;
- uint16_t motor1_abs;
- uint16_t wheel_abs;
-};
-
-void AdcInitSmall() {
- AdcInitCommon();
-
- // M0_CH0F ADC1_SE17
- PORTA_PCR17 = PORT_PCR_MUX(0);
-
- // M0_CH1F ADC1_SE14
- PORTB_PCR10 = PORT_PCR_MUX(0);
-
- // M0_CH2F ADC1_SE15
- PORTB_PCR11 = PORT_PCR_MUX(0);
-
- // M0_ABS ADC0_SE5b
- PORTD_PCR1 = PORT_PCR_MUX(0);
-
- // M1_CH0F ADC0_SE13
- PORTB_PCR3 = PORT_PCR_MUX(0);
-
- // M1_CH1F ADC0_SE12
- PORTB_PCR2 = PORT_PCR_MUX(0);
-
- // M1_CH2F ADC0_SE14
- PORTC_PCR0 = PORT_PCR_MUX(0);
-
- // M1_ABS ADC0_SE17
- PORTE_PCR24 = PORT_PCR_MUX(0);
-
- // WHEEL_ABS ADC0_SE18
- PORTE_PCR25 = PORT_PCR_MUX(0);
-
- // VIN ADC1_SE5B
- PORTC_PCR9 = PORT_PCR_MUX(0);
-}
-
-SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) {
- SmallAdcReadings r;
-
- ADC1_SC1A = 17;
- while (!(ADC1_SC1A & ADC_SC1_COCO)) {
- }
- ADC1_SC1A = 14;
- r.currents[0] = ADC1_RA;
- while (!(ADC1_SC1A & ADC_SC1_COCO)) {
- }
- ADC1_SC1A = 15;
- r.currents[1] = ADC1_RA;
- while (!(ADC1_SC1A & ADC_SC1_COCO)) {
- }
- r.currents[2] = ADC1_RA;
-
- return r;
-}
-
-SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) {
- SmallAdcReadings r;
-
- ADC0_SC1A = 13;
- while (!(ADC0_SC1A & ADC_SC1_COCO)) {
- }
- ADC0_SC1A = 12;
- r.currents[0] = ADC0_RA;
- while (!(ADC0_SC1A & ADC_SC1_COCO)) {
- }
- ADC0_SC1A = 14;
- r.currents[1] = ADC0_RA;
- while (!(ADC0_SC1A & ADC_SC1_COCO)) {
- }
- r.currents[2] = ADC0_RA;
-
- return r;
-}
-
-SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) {
- SmallInitReadings r;
-
- ADC0_SC1A = 5;
- while (!(ADC0_SC1A & ADC_SC1_COCO)) {
- }
- ADC0_SC1A = 17;
- r.motor0_abs = ADC0_RA;
- while (!(ADC0_SC1A & ADC_SC1_COCO)) {
- }
- ADC0_SC1A = 18;
- r.motor1_abs = ADC0_RA;
- while (!(ADC0_SC1A & ADC_SC1_COCO)) {
- }
- r.wheel_abs = ADC0_RA;
-
- return r;
-}
constexpr float kHapticWheelCurrentLimit = static_cast<float>(
::frc971::control_loops::drivetrain::kHapticWheelCurrentLimit);
diff --git a/motors/pistol_grip/controller_adc.cc b/motors/pistol_grip/controller_adc.cc
new file mode 100644
index 0000000..498c4e2
--- /dev/null
+++ b/motors/pistol_grip/controller_adc.cc
@@ -0,0 +1,100 @@
+#include "motors/pistol_grip/controller_adc.h"
+
+#include "motors/peripheral/adc.h"
+
+namespace frc971 {
+namespace motors {
+
+void AdcInitSmall() {
+ AdcInitCommon();
+
+ // M0_CH0F ADC1_SE17
+ PORTA_PCR17 = PORT_PCR_MUX(0);
+
+ // M0_CH1F ADC1_SE14
+ PORTB_PCR10 = PORT_PCR_MUX(0);
+
+ // M0_CH2F ADC1_SE15
+ PORTB_PCR11 = PORT_PCR_MUX(0);
+
+ // M0_ABS ADC0_SE5b
+ PORTD_PCR1 = PORT_PCR_MUX(0);
+
+ // M1_CH0F ADC0_SE13
+ PORTB_PCR3 = PORT_PCR_MUX(0);
+
+ // M1_CH1F ADC0_SE12
+ PORTB_PCR2 = PORT_PCR_MUX(0);
+
+ // M1_CH2F ADC0_SE14
+ PORTC_PCR0 = PORT_PCR_MUX(0);
+
+ // M1_ABS ADC0_SE17
+ PORTE_PCR24 = PORT_PCR_MUX(0);
+
+ // WHEEL_ABS ADC0_SE18
+ PORTE_PCR25 = PORT_PCR_MUX(0);
+
+ // VIN ADC1_SE5B
+ PORTC_PCR9 = PORT_PCR_MUX(0);
+}
+
+SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) {
+ SmallAdcReadings r;
+
+ ADC1_SC1A = 17;
+ while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+ }
+ ADC1_SC1A = 14;
+ r.currents[0] = ADC1_RA;
+ while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+ }
+ ADC1_SC1A = 15;
+ r.currents[1] = ADC1_RA;
+ while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+ }
+ r.currents[2] = ADC1_RA;
+
+ return r;
+}
+
+SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) {
+ SmallAdcReadings r;
+
+ ADC0_SC1A = 13;
+ while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+ }
+ ADC0_SC1A = 12;
+ r.currents[0] = ADC0_RA;
+ while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+ }
+ ADC0_SC1A = 14;
+ r.currents[1] = ADC0_RA;
+ while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+ }
+ r.currents[2] = ADC0_RA;
+
+ return r;
+}
+
+SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) {
+ SmallInitReadings r;
+
+ ADC0_SC1A = 5;
+ while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+ }
+ ADC0_SC1A = 17;
+ r.motor0_abs = ADC0_RA;
+ while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+ }
+ ADC0_SC1A = 18;
+ r.motor1_abs = ADC0_RA;
+ while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+ }
+ r.wheel_abs = ADC0_RA;
+
+ return r;
+}
+
+} // namespace motors
+} // namespace frc971
diff --git a/motors/pistol_grip/controller_adc.h b/motors/pistol_grip/controller_adc.h
new file mode 100644
index 0000000..867807c
--- /dev/null
+++ b/motors/pistol_grip/controller_adc.h
@@ -0,0 +1,34 @@
+#ifndef MOTORS_PISTOL_GRIP_CONTROLLER_ADC_H_
+#define MOTORS_PISTOL_GRIP_CONTROLLER_ADC_H_
+
+#include "motors/util.h"
+
+namespace frc971 {
+namespace motors {
+
+struct SmallAdcReadings {
+ uint16_t currents[3];
+};
+
+struct SmallInitReadings {
+ uint16_t motor0_abs;
+ uint16_t motor1_abs;
+ uint16_t wheel_abs;
+};
+
+// Initializes the ADC.
+void AdcInitSmall();
+
+// Reads motor 0.
+SmallAdcReadings AdcReadSmall0(const DisableInterrupts &);
+
+// Reads motor 1.
+SmallAdcReadings AdcReadSmall1(const DisableInterrupts &);
+
+// Reads the absolute encoder values for initialization.
+SmallInitReadings AdcReadSmallInit(const DisableInterrupts &);
+
+} // namespace motors
+} // namespace frc971
+
+#endif // MOTORS_PISTOL_GRIP_CONTROLLER_ADC_H_