Stepdance Software Library
Loading...
Searching...
No Matches
channels.hpp
1#include "arm_math.h"
2#include <sys/_stdint.h>
3#include "output_ports.hpp"
4#include "core.hpp"
5
6/*
7Channels Module of the StepDance Control System
8
9[More Details to be Added]
10
11A part of the Mixing Metaphors Project
12(c) 2025 Ilan Moyer, Jennifer Jacobs, Devon Frost, Emilie Yu
13*/
14
15#ifndef channels_h //prevent importing twice
16#define channels_h
17
18#define MAX_NUM_CHANNELS 10 //just to start with, until we need more.
19
20void run_all_registered_channels(); //drives all registered channels to their target positions
21void activate_channels(); //adds channels to the frame interrupt routine
22
23
33
34class Channel : public Plugin{
35 public:
36 // Public State
41
45 bool filtering_on = false;
46
51 DecimalPosition target_position; //primary target position, in pulses.
52 DecimalPosition target_position_2; // secondary target position, used for coordinate transforms.
53 DecimalPosition current_position; //tracks the current position, in pulses.
54 DecimalPosition filtered_target_position; // filtered target position
55 float32_t num_averaging_samples = 20; //samples in the averaging window
56 // BlockPorts
57 BlockPort input_target_position_2;
58
59 void push_deep();
60
62
63 // Public Methods
64 Channel();
69 void begin(); //channel with no output port
70
76 void begin(OutputPort* target_output_port, uint8_t output_signal);
81 void set_max_pulse_rate(float max_pulses_per_sec);
87 void set_ratio(float input_units, float channel_units = 1.0); //sets the transmission ratio for all target transmissions
92 void set_upper_limit(DecimalPosition upper_limit_input_units); //sets the upper limit, using input (world) units.
97 void set_lower_limit(DecimalPosition lower_limit_input_units); //sets the lower limit, using input (world) units.
112 inline void disable_limits(){
115 }
116
119 void disable();
123 void enable();
128 void enable_filtering(uint16_t num_samples = 20);
149 void invert_output(bool invert);
150
151
156 void enroll(RPC *rpc, const String& instance_name);
157 void run(); //Drives the current position to the target position by one pulse, and generates a signal
158
159 DecimalPosition read_deep(BlockPort& in_blockport) override; //is not user-facing.
161
162
163 private:
164 // Constants
165 const uint32_t ACCUMULATOR_THRESHOLD = 1000000;
166 const uint32_t PULSE_MAX_RATE = 1000000 / CORE_FRAME_PERIOD_US;
167
168 // Configuration
169 int has_output = 0; //1 if channel has an output port, otherwise 0.
170 OutputPort* target_output_port; //stores the target output port
171 uint8_t output_signal = SIGNAL_X; //default to signal X
172 uint8_t output_inverted = 0; //if 1, will invert the output direction of the channel
173
174 // Private State
175 volatile float accumulator;
176 volatile float accumulator_velocity;
177 volatile int last_direction;
178 DecimalPosition upper_limit;
179 DecimalPosition lower_limit;
180 bool upper_limit_enabled = false;
181 bool lower_limit_enabled = false;
182 bool enabled = true;
183
184 // Private Methods
185 void initialize_state(); // initializes all state variables
186 void register_channel(); // registers channel with the signal generator loop
187 void pulse(int8_t direction); // generates a step pulse and releases a signal on the output port
188
189};
190
191#endif
BlockPorts provide a unified interface for mapping inputs and outputs of different StepDance componen...
Definition core.hpp:152
void disable_limits()
Disables the channel.
Definition channels.hpp:112
void disable_lower_limit()
Disables the lower position limit.
bool filtering_on
Flag indicating whether filtering is enabled for the channel.
Definition channels.hpp:45
void begin()
Initialize the Channel with no output port. Not for normal use.
BlockPort input_target_position
Blockport managing the target position of the channel in pulses. Target positon is what the channel i...
Definition channels.hpp:40
void enable()
Enables the channel.
void enable_filtering(uint16_t num_samples=20)
Enables a moving average filter with a specified sample window. The bigger the window,...
void disable()
Disables the channel.
int8_t is_outside_limits()
Checks if the current channel position is outside the set limits.
void disable_upper_limit()
Disables the upper position limit.
void begin(OutputPort *target_output_port, uint8_t output_signal)
Initialize the Channel with a target OutputPort and output signal.
void set_upper_limit(DecimalPosition upper_limit_input_units)
Sets the upper position limit for the channel. Useful if you want the channel to stop transmitting mo...
void invert_output(bool invert)
Inverts the channel output direction. Useful for reversing motor direction without changing wiring.
void invert_output()
Inverts the channel output direction.
void set_max_pulse_rate(float max_pulses_per_sec)
Sets the maxium allowable pulse rate for the channel.
void set_ratio(float input_units, float channel_units=1.0)
Sets the transmission ratio for all target transmissions.
void set_lower_limit(DecimalPosition lower_limit_input_units)
Sets the lower position limit for the channel. Useful if you want the channel to stop transmitting mo...
void disable_filtering()
Disables the moving average filter.
OutputPorts are modules that convert internal step commands into a frame of pulse signals on the phys...
Definition output_ports.hpp:108
RPC class for handling remote procedure calls over serial streams.
Definition rpc.hpp:35