blob: 3f7b1324cc1099f6f68cced342abe15df375f142 [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#ifndef MOTORS_MOTOR_H_
2#define MOTORS_MOTOR_H_
3
4#include <limits.h>
5
6#include <array>
7
8#include "motors/algorithms.h"
9#include "motors/core/kinetis.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070010#include "motors/core/time.h"
Brian Silverman8d3816a2017-07-03 18:52:15 -070011#include "motors/peripheral/adc.h"
Brian Silverman19ea60f2018-01-03 21:43:15 -080012#include "motors/peripheral/configuration.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070013#include "motors/print/print.h"
Brian Silverman8d3816a2017-07-03 18:52:15 -070014#include "motors/util.h"
15
16namespace frc971 {
Brian Silvermana96c1a42018-05-12 12:11:31 -070017namespace motors {
Brian Silverman8d3816a2017-07-03 18:52:15 -070018
19class MotorControls {
20 public:
21 MotorControls() = default;
22 virtual ~MotorControls() = default;
23
24 MotorControls(const MotorControls &) = delete;
25 void operator=(const MotorControls &) = delete;
26
James Kuszmaul998d3032018-09-08 15:41:41 -070027 virtual void Reset() = 0;
28
Brian Silverman19ea60f2018-01-03 21:43:15 -080029 // Scales a current reading from ADC units to amps.
30 //
31 // Note that this doesn't apply any offset. The common offset will be
32 // automatically removed as part of the balancing process.
33 virtual float scale_current_reading(float reading) const = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -070034
Brian Silverman19ea60f2018-01-03 21:43:15 -080035 virtual int mechanical_counts_per_revolution() const = 0;
36 virtual int electrical_counts_per_revolution() const = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -070037
38 // raw_currents are in amps for each phase.
39 // theta is in electrical counts, which will be less than
40 // counts_per_revolution().
James Kuszmaul998d3032018-09-08 15:41:41 -070041 virtual ::std::array<float, 3> DoIteration(const float raw_currents[3],
42 uint32_t theta,
43 const float command_current) = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -070044
45 virtual int16_t Debug(uint32_t theta) = 0;
46
47 virtual float estimated_velocity() const = 0;
James Kuszmaul998d3032018-09-08 15:41:41 -070048 virtual int16_t i_goal(size_t ii) const = 0;
James Kuszmaul521eb652018-10-17 19:09:33 -070049 virtual float overall_measured_current() const = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -070050};
51
52// Controls a single motor.
Brian Silverman19ea60f2018-01-03 21:43:15 -080053class Motor final {
Brian Silverman8d3816a2017-07-03 18:52:15 -070054 public:
55 // pwm_ftm is used to drive the PWM outputs.
56 // encoder_ftm is used for reading the encoder.
Brian Silverman19ea60f2018-01-03 21:43:15 -080057 Motor(BigFTM *pwm_ftm, LittleFTM *encoder_ftm, MotorControls *controls,
58 const ::std::array<volatile uint32_t *, 3> &output_registers);
Brian Silverman8d3816a2017-07-03 18:52:15 -070059
60 Motor(const Motor &) = delete;
61 void operator=(const Motor &) = delete;
62
James Kuszmaul998d3032018-09-08 15:41:41 -070063 void Reset() { controls_->Reset(); }
64
Brian Silverman4787a6e2018-10-06 16:00:54 -070065 void set_printing_implementation(
66 PrintingImplementation *printing_implementation) {
67 printing_implementation_ = printing_implementation;
68 }
Brian Silverman19ea60f2018-01-03 21:43:15 -080069 void set_deadtime_compensation(int deadtime_compensation) {
70 deadtime_compensation_ = deadtime_compensation;
71 }
72 void set_switching_divisor(int switching_divisor) {
73 switching_divisor_ = switching_divisor;
74 }
Brian Silverman6260c092018-01-14 15:21:36 -080075 void set_encoder_offset(int32_t encoder_offset) {
Brian Silverman19ea60f2018-01-03 21:43:15 -080076 encoder_offset_ = encoder_offset;
Brian Silverman6260c092018-01-14 15:21:36 -080077 last_wrapped_encoder_reading_ = wrapped_encoder();
78 }
79 int32_t encoder_offset() const { return encoder_offset_; }
80
81 void set_encoder_calibration_offset(int encoder_offset) {
82 encoder_calibration_offset_ = encoder_offset;
Brian Silverman19ea60f2018-01-03 21:43:15 -080083 // Add mechanical_counts_per_revolution to the offset so that when we mod
84 // below, we are guaranteed to be > 0 regardless of the encoder multiplier.
85 // % isn't well-defined with negative numbers.
Brian Silverman6260c092018-01-14 15:21:36 -080086 while (encoder_calibration_offset_ <
87 controls_->mechanical_counts_per_revolution()) {
88 encoder_calibration_offset_ +=
89 controls_->mechanical_counts_per_revolution();
Brian Silverman19ea60f2018-01-03 21:43:15 -080090 }
91 }
92 void set_encoder_multiplier(int encoder_multiplier) {
93 encoder_multiplier_ = encoder_multiplier;
94 }
95
Brian Silverman6260c092018-01-14 15:21:36 -080096 int32_t absolute_encoder(uint32_t wrapped_encoder_reading) {
97 const uint32_t counts_per_revolution =
98 controls_->mechanical_counts_per_revolution();
99 const uint32_t wrap_down = counts_per_revolution / 4;
100 const uint32_t wrap_up = wrap_down * 3;
101 if (last_wrapped_encoder_reading_ > wrap_up &&
102 wrapped_encoder_reading < wrap_down) {
103 encoder_offset_ += counts_per_revolution;
104 } else if (last_wrapped_encoder_reading_ < wrap_down &&
105 wrapped_encoder_reading > wrap_up) {
106 encoder_offset_ -= counts_per_revolution;
107 }
108
109 last_wrapped_encoder_reading_ = wrapped_encoder_reading;
110
111 return static_cast<int32_t>(wrapped_encoder_reading) + encoder_offset_;
112 }
113
Philipp Schrader790cb542023-07-05 21:06:52 -0700114 int encoder() { return encoder_multiplier_ * encoder_ftm_->CNT; }
Brian Silverman19ea60f2018-01-03 21:43:15 -0800115 uint32_t wrapped_encoder() {
Brian Silverman6260c092018-01-14 15:21:36 -0800116 return (encoder() + encoder_calibration_offset_) %
Brian Silverman19ea60f2018-01-03 21:43:15 -0800117 controls_->mechanical_counts_per_revolution();
118 }
119
Brian Silverman8d3816a2017-07-03 18:52:15 -0700120 // Sets up everything but doesn't actually start the timers.
121 //
122 // This assumes the global time base configuration happens outside so the
123 // timers for both motors (if applicable) are synced up.
Brian Silverman8d3816a2017-07-03 18:52:15 -0700124 void Init();
125
Brian Silverman8d3816a2017-07-03 18:52:15 -0700126 // Starts the timers.
Brian Silverman19ea60f2018-01-03 21:43:15 -0800127 //
128 // If the global time base is in use, it must be activated after this.
Brian Silverman8d3816a2017-07-03 18:52:15 -0700129 void Start();
130
Austin Schuh54c8c842019-04-07 13:54:23 -0700131 void CurrentInterrupt(const BalancedReadings &readings,
132 uint32_t captured_wrapped_encoder);
Brian Silverman19ea60f2018-01-03 21:43:15 -0800133
Austin Schuh6bf82752019-04-07 13:55:01 -0700134 // Runs each phase at a fixed duty cycle.
Austin Schuhefe72932019-04-13 00:03:19 -0700135 void CycleFixedPhaseInterupt(int period = 80);
Austin Schuh6bf82752019-04-07 13:55:01 -0700136
Brian Silverman19ea60f2018-01-03 21:43:15 -0800137 void SetGoalCurrent(float goal_current) {
138 DisableInterrupts disable_interrupts;
139 goal_current_ = goal_current;
140 last_current_set_time_ = micros();
141 }
Brian Silverman8d3816a2017-07-03 18:52:15 -0700142
Brian Silverman19ea60f2018-01-03 21:43:15 -0800143 inline int counts_per_cycle() const {
144 return BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY / switching_divisor_;
145 }
146
James Kuszmaul998d3032018-09-08 15:41:41 -0700147 inline uint16_t get_switching_points_cycles(size_t ii) const {
148 return static_cast<uint16_t>(switching_points_ratio_[ii] *
149 counts_per_cycle());
150 }
151
152 inline float estimated_velocity() const {
153 return controls_->estimated_velocity();
154 }
155
Philipp Schrader790cb542023-07-05 21:06:52 -0700156 inline int16_t i_goal(size_t ii) const { return controls_->i_goal(ii); }
James Kuszmaul998d3032018-09-08 15:41:41 -0700157
Philipp Schrader790cb542023-07-05 21:06:52 -0700158 inline int16_t goal_current() const { return goal_current_; }
James Kuszmaul998d3032018-09-08 15:41:41 -0700159
James Kuszmaul521eb652018-10-17 19:09:33 -0700160 inline float overall_measured_current() const {
161 return controls_->overall_measured_current();
162 }
163
Austin Schuh5b0e6b62019-04-07 14:23:37 -0700164 ::std::array<volatile uint32_t *, 3> output_registers() const {
165 return output_registers_;
166 }
167
James Kuszmaul998d3032018-09-08 15:41:41 -0700168 private:
Brian Silverman19ea60f2018-01-03 21:43:15 -0800169 uint32_t CalculateOnTime(uint32_t width) const;
170 uint32_t CalculateOffTime(uint32_t width) const;
171
Brian Silverman8d3816a2017-07-03 18:52:15 -0700172 bool flip_time_offset_ = false;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800173 int deadtime_compensation_ = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700174
175 BigFTM *const pwm_ftm_;
176 LittleFTM *const encoder_ftm_;
177 MotorControls *const controls_;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800178 const ::std::array<volatile uint32_t *, 3> output_registers_;
James Kuszmaul998d3032018-09-08 15:41:41 -0700179 ::std::array<float, 3> switching_points_ratio_;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800180
181 float goal_current_ = 0;
182 uint32_t last_current_set_time_ = 0;
183 int switching_divisor_ = 1;
Brian Silverman6260c092018-01-14 15:21:36 -0800184 int encoder_calibration_offset_ = 0;
185 int32_t encoder_offset_ = 0;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800186 int encoder_multiplier_ = 1;
Brian Silverman6260c092018-01-14 15:21:36 -0800187 uint32_t last_wrapped_encoder_reading_ = 0;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800188
Brian Silverman4787a6e2018-10-06 16:00:54 -0700189 PrintingImplementation *printing_implementation_ = nullptr;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700190};
191
Brian Silvermana96c1a42018-05-12 12:11:31 -0700192} // namespace motors
Brian Silverman8d3816a2017-07-03 18:52:15 -0700193} // namespace frc971
194
195#endif // MOTORS_MOTOR_H_