MIPI - Physical Interface for MIDI Files
Loading...
Searching...
No Matches
controller.h
Go to the documentation of this file.
1#ifndef CONTROLLER_H
2#define CONTROLLER_H
3
4// Project Includes
5#include "jamc/srv/load.hpp"
6#include "jamc/srv/func.hpp"
7#include "jamc/srv/time_scale.hpp"
8
10
11// System path includes
12#include "rclcpp/rclcpp.hpp"
13#include <geometry_msgs/msg/twist_stamped.hpp>
14#include <geometry_msgs/msg/point.hpp>
15#include <chrono>
16#include <cmath>
17#include <optional>
18#include <geometry_msgs/msg/pose_array.hpp>
19#include <sensor_msgs/msg/joint_state.hpp>
20#include <control_msgs/msg/joint_jog.hpp>
21
22using Clock = std::chrono::steady_clock;
23
24namespace Control
25{
29 struct vector3
30 {
31 double x;
32 double y;
33 double z;
34 };
35
42 enum class STATE
43 {
44 WAITING,
45 PLAYING,
46 MOVING
47 };
48
49 class Controller : public rclcpp::Node
50 {
51 public:
52 Controller();
54 private:
55 //Variables & Helpers
60
61 long CONTROL_TIME; //Variable for the control loop to use for timing
62 Clock::time_point LAST_CONTROL_TIME_POINT; //The last time the control loop ran, used to calculate delta time for timing the notes
63 STATE state_; //The current state of the controller, used to determine behavior in the control loop
65 std::mutex key_positions_mutex_; //Mutex to protect access to the key positions
66 geometry_msgs::msg::PoseArray key_positions_; //The positions of the keys on the piano, used for calculating target positions for the end effector
67
68 std::mutex song_mutex_; //Mutex to protect access to the song data
69 std::vector<int> song_; //The notes in the currently loaded channel of the currently loaded song
70 std::vector<double> note_timings_; //The timings of the notes in the current channel of the currently loaded song
71 std::vector<double> note_durations_; //The durations of the notes in the current channel of the currently loaded song
72
73 bool play_ = false; //Whether the song should be playing or not
74 double time_scale_ = 1.0; //The time scaling factor for playback, default is 1.0 (normal speed)
75 bool direction_ = true; //The direction of playback, true for forward, false for backward
76 int current_note_index_ = 0; //The index of the current note being played in the song
77 bool song_loaded_ = false; //Whether a song has been loaded or not
78
79 std::mutex joint_mutex_; //Mutex to protect access to the latest joint state
80 std::vector<double> latest_joint_state_; //The latest joint state of the robot
81
82 rclcpp::TimerBase::SharedPtr control_timer_; //Timer for the control loop
83 rclcpp::TimerBase::SharedPtr startup_timer_; //Timer for the startup sequence
84
85 bool startup_complete_ = false; //Whether the startup sequence is complete or not
87
88 //Methods
93
94 void sendTwistMsg(double x, double y, double z, double angular_x = 0.0, double angular_y = 0.0, double angular_z = 0.0);
95 void sendJointJog(double shoulder_lift, double elbow, double wrist_1, double wrist_2, double wrist_3, double shoulder_pan);
96 void sendJointJog(std::vector<double> vel);
97 void sendVector(const vector3 &vec);
98 void sendStop();
99 int activeTrackDebug(vector3 target, bool x = false, bool y = false, bool z = false);
101
102 //Startup & Shutdown
107
108 void startup();
110
111 //Publishers
116
117 rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr twist_pub_;
118 rclcpp::Publisher<control_msgs::msg::JointJog>::SharedPtr joint_traj_streaming_pub_;
120
121 //Subscribers
126
127 rclcpp::Subscription<geometry_msgs::msg::Point>::SharedPtr debug_target_sub_;
128 rclcpp::Subscription<geometry_msgs::msg::PoseArray>::SharedPtr key_positions_sub_;
129 rclcpp::Subscription<sensor_msgs::msg::JointState>::SharedPtr joint_state_sub_;
131
132 //Subscriber Callbacks
137
138 void debug_target_callback(const geometry_msgs::msg::Point::SharedPtr msg);
139 void key_positions_callback(const geometry_msgs::msg::PoseArray::SharedPtr msg);
140 void joint_state_callback(const sensor_msgs::msg::JointState::SharedPtr msg);
142
143 //Services
148
149 rclcpp::Service<jamc::srv::Load>::SharedPtr load_service_;
150 rclcpp::Service<jamc::srv::TimeScale>::SharedPtr time_service_;
151 rclcpp::Service<jamc::srv::Func>::SharedPtr play_pause_service_;
152 rclcpp::Service<jamc::srv::Func>::SharedPtr play_direction_service_;
153 rclcpp::Service<jamc::srv::Func>::SharedPtr debug_service_;
155
156 //Service Callbacks
161
162 void load_callback(const std::shared_ptr<jamc::srv::Load::Request> request, std::shared_ptr<jamc::srv::Load::Response> response);
163 void time_scale_callback(const std::shared_ptr<jamc::srv::TimeScale::Request> request, std::shared_ptr<jamc::srv::TimeScale::Response> response);
164 void play_pause_callback(const std::shared_ptr<jamc::srv::Func::Request> request, std::shared_ptr<jamc::srv::Func::Response> response);
165 void play_direction_callback(const std::shared_ptr<jamc::srv::Func::Request> request, std::shared_ptr<jamc::srv::Func::Response> response);
166 void debug_service_callback(const std::shared_ptr<jamc::srv::Func::Request> request, std::shared_ptr<jamc::srv::Func::Response> response);
168
169 //Control Loop
174
175 void control_loop();
176 double calculate_z(double xy);
177 std::optional<vector3> calculate_velocity(int note);
178 std::optional<vector3> play_note(double duration, double time);
180 };
181}
182
183
184#endif // CONTROLLER_H
Definition controller.h:50
rclcpp::Service< jamc::srv::Load >::SharedPtr load_service_
Definition controller.h:149
void time_scale_callback(const std::shared_ptr< jamc::srv::TimeScale::Request > request, std::shared_ptr< jamc::srv::TimeScale::Response > response)
Callback function for the TimeScale Service, used to set the time scaling factor for playback.
Definition controller.cpp:242
void sendVector(const vector3 &vec)
The public method for sending translational End Effector vector commands to the UR3e.
Definition controller.cpp:75
std::optional< vector3 > play_note(double duration, double time)
Plays a single note or presses a button, streams Z velocity for a hardcoded duration to push down on ...
Definition controller.cpp:377
rclcpp::Subscription< geometry_msgs::msg::Point >::SharedPtr debug_target_sub_
Definition controller.h:127
rclcpp::Subscription< sensor_msgs::msg::JointState >::SharedPtr joint_state_sub_
Definition controller.h:129
Clock::time_point LAST_CONTROL_TIME_POINT
Definition controller.h:62
std::vector< double > latest_joint_state_
Definition controller.h:80
Controller()
Standalone Class for Controlling a UR3e. Typically reads data from .mipi files for plaback on a piano...
Definition controller.cpp:15
void joint_state_callback(const sensor_msgs::msg::JointState::SharedPtr msg)
Subscriber Callback that obtains the joint state of the robot.
Definition controller.cpp:419
rclcpp::Service< jamc::srv::Func >::SharedPtr play_pause_service_
Definition controller.h:151
double calculate_z(double xy)
A helper method for calculating the target Z velocity (Scaled to meet a target height based on the X&...
Definition controller.cpp:388
bool startup_complete_
Definition controller.h:85
void control_loop()
The main control loop that runs when the control node starts.
Definition controller.cpp:281
void load_callback(const std::shared_ptr< jamc::srv::Load::Request > request, std::shared_ptr< jamc::srv::Load::Response > response)
Callback function for the Load Service, used to load .mipi files and select which instrument channel.
Definition controller.cpp:206
rclcpp::Publisher< control_msgs::msg::JointJog >::SharedPtr joint_traj_streaming_pub_
Definition controller.h:118
rclcpp::TimerBase::SharedPtr control_timer_
Definition controller.h:82
void startup()
The startup sequence that moves the robot into the starting position for playing.
Definition controller.cpp:428
void play_direction_callback(const std::shared_ptr< jamc::srv::Func::Request > request, std::shared_ptr< jamc::srv::Func::Response > response)
Callback function for the Play Direction Service, used to toggle the direction of playback.
Definition controller.cpp:269
void sendJointJog(double shoulder_lift, double elbow, double wrist_1, double wrist_2, double wrist_3, double shoulder_pan)
A wrapper for sending joint jog commands for direct joint velocity control.
Definition controller.cpp:479
int activeTrackDebug(vector3 target, bool x=false, bool y=false, bool z=false)
A method for enabling "activeTrack" mode, allowing for live visual servoing following a topic.
Definition controller.cpp:95
rclcpp::TimerBase::SharedPtr startup_timer_
Definition controller.h:83
bool song_loaded_
Definition controller.h:77
std::vector< double > note_durations_
Definition controller.h:71
void sendTwistMsg(double x, double y, double z, double angular_x=0.0, double angular_y=0.0, double angular_z=0.0)
A convenience method that neatly packs up a twist messge and publishes it to the UR_Driver.
Definition controller.cpp:52
void key_positions_callback(const geometry_msgs::msg::PoseArray::SharedPtr msg)
Subscriber Callback that obtains the key positions from the.
Definition controller.cpp:399
rclcpp::Subscription< geometry_msgs::msg::PoseArray >::SharedPtr key_positions_sub_
Definition controller.h:128
rclcpp::Service< jamc::srv::Func >::SharedPtr play_direction_service_
Definition controller.h:152
rclcpp::Service< jamc::srv::TimeScale >::SharedPtr time_service_
Definition controller.h:150
std::mutex key_positions_mutex_
Definition controller.h:65
long CONTROL_TIME
Definition controller.h:61
std::vector< double > note_timings_
Definition controller.h:70
bool play_
Definition controller.h:73
std::mutex joint_mutex_
Definition controller.h:79
std::optional< vector3 > calculate_velocity(int note)
Velocity calculation for control loop. If the note is not visible, will travel in the parallel to the...
Definition controller.cpp:150
int current_note_index_
Definition controller.h:76
geometry_msgs::msg::PoseArray key_positions_
Definition controller.h:66
bool direction_
Definition controller.h:75
std::mutex song_mutex_
Definition controller.h:68
~Controller()
Standard Destructor for Controller Class.
Definition controller.cpp:38
STATE state_
Definition controller.h:63
rclcpp::Service< jamc::srv::Func >::SharedPtr debug_service_
Definition controller.h:153
std::vector< int > song_
Definition controller.h:69
void debug_service_callback(const std::shared_ptr< jamc::srv::Func::Request > request, std::shared_ptr< jamc::srv::Func::Response > response)
Callback function for the debug service, used to test anything.
Definition controller.cpp:550
rclcpp::Publisher< geometry_msgs::msg::TwistStamped >::SharedPtr twist_pub_
Definition controller.h:117
MidiProcessor connor
Definition controller.h:64
double time_scale_
Definition controller.h:74
void play_pause_callback(const std::shared_ptr< jamc::srv::Func::Request > request, std::shared_ptr< jamc::srv::Func::Response > response)
Callback function for the Play/Pause Service, used to toggle playback of the current track.
Definition controller.cpp:255
void debug_target_callback(const geometry_msgs::msg::Point::SharedPtr msg)
Callback function for catching the debug subscriber, used to activate activeTrack.
Definition controller.cpp:108
void sendStop()
A simple helper method for immediately sending stop to the UR3e.
Definition controller.cpp:83
Standalone Class for processing midi files and saving/loading mipi files. Contains various getters to...
Definition midi_processor.h:54
std::chrono::steady_clock Clock
Definition controller.h:22
Definition controller.h:25
STATE
An enum for representing the state of the controller, used to determine behavior in the control loop ...
Definition controller.h:43
A struct for representing 3D vectors, used for velocity commands and target positions.
Definition controller.h:30
double z
Definition controller.h:33
double x
Definition controller.h:31
double y
Definition controller.h:32