Skip to content

Commit

Permalink
Committing clang-format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Clang Robot committed Oct 13, 2023
1 parent 0e998e5 commit 9ed9d19
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ class ThrusterInterface {
void publish_thrust_to_escs(std::vector<double> forces);
float interpolate(float force);
};

Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#pragma once

#include <ament_index_cpp/get_package_share_directory.hpp>
#include <rclcpp/rclcpp.hpp>
#include <vortex_msgs/msg/pwm.hpp>
#include <vortex_msgs/msg/thruster_forces.hpp>
#include <ament_index_cpp/get_package_share_directory.hpp>


#include <thruster_interface/thruster_interface.hpp>
using std::placeholders::_1;

class ThrusterInterfaceROS : public rclcpp::Node {
private:
std::string mapping_file = ament_index_cpp::get_package_share_directory("thruster_interface") +
"/config/ThrustMe_P1000_force_mapping.csv";
std::string mapping_file =
ament_index_cpp::get_package_share_directory("thruster_interface") +
"/config/ThrustMe_P1000_force_mapping.csv";
ThrusterInterface thrusterInterface{mapping_file};

rclcpp::Subscription<vortex_msgs::msg::ThrusterForces>::SharedPtr subscription_;
rclcpp::Subscription<vortex_msgs::msg::ThrusterForces>::SharedPtr
subscription_;
rclcpp::Publisher<vortex_msgs::msg::Pwm>::SharedPtr publisher_;

public:
ThrusterInterfaceROS() : Node("thruster_interface") {
publisher_ = this->create_publisher<vortex_msgs::msg::Pwm>("pwm", 10);
subscription_ = this->create_subscription<vortex_msgs::msg::ThrusterForces>(
"thrust/thruster_forces", 10, std::bind(&ThrusterInterfaceROS::thrust_callback, this, _1));
"thrust/thruster_forces", 10,
std::bind(&ThrusterInterfaceROS::thrust_callback, this, _1));
}
void thrust_callback(const vortex_msgs::msg::ThrusterForces::SharedPtr msg);
};


82 changes: 41 additions & 41 deletions motion/thruster_interface/src/thruster_interface_ros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@

void ThrusterInterfaceROS::thrust_callback(
const vortex_msgs::msg::ThrusterForces::SharedPtr msg) {
// Convert from Newton to grams
double newton_to_gram_conversion_factor = 101.97162;

std::vector<double> forces_in_grams = {
msg->thrust[0] * newton_to_gram_conversion_factor,
msg->thrust[1] * newton_to_gram_conversion_factor,
msg->thrust[2] * newton_to_gram_conversion_factor,
msg->thrust[3] * newton_to_gram_conversion_factor};

std::vector<int> pwm_values;
for (auto force : forces_in_grams) {
pwm_values.push_back(thrusterInterface.interpolate(force));
}

vortex_msgs::msg::Pwm pwm_msg;
// TODO: Get mapping and offsets from rosparam
// Give thrust to thruster 0: publish on pin = thruster_to_pin_map[0]
std::vector<int> thruster_to_pin_map = {1, 3, 2, 0};
std::vector<int> thruster_direction_map = {1, 1, 1, -1};
std::vector<int> pwm_offsets = {100, 100, 100, 100};

// Iterates through thruster 0 to 3, where 0 is front right, iterated clockwise
for (int i = 0; i < 4; i++) {

int center_pwm_value = 1500;
int offset_from_center_value =
pwm_values[i] - center_pwm_value + pwm_offsets[i];
int pwm_value_correct_direction =
center_pwm_value + thruster_direction_map[i] * offset_from_center_value;

int pwm_clamped = std::min(std::max(pwm_value_correct_direction, 1300),
1700); // min 1100, max 1900
pwm_msg.positive_width_us.push_back(pwm_clamped);
pwm_msg.pins.push_back(thruster_to_pin_map[i]);
}

publisher_->publish(pwm_msg);

// thrusterInterface.publish_thrust_to_escs(forces_in_grams);
// Convert from Newton to grams
double newton_to_gram_conversion_factor = 101.97162;

std::vector<double> forces_in_grams = {
msg->thrust[0] * newton_to_gram_conversion_factor,
msg->thrust[1] * newton_to_gram_conversion_factor,
msg->thrust[2] * newton_to_gram_conversion_factor,
msg->thrust[3] * newton_to_gram_conversion_factor};

std::vector<int> pwm_values;
for (auto force : forces_in_grams) {
pwm_values.push_back(thrusterInterface.interpolate(force));
}

vortex_msgs::msg::Pwm pwm_msg;
// TODO: Get mapping and offsets from rosparam
// Give thrust to thruster 0: publish on pin = thruster_to_pin_map[0]
std::vector<int> thruster_to_pin_map = {1, 3, 2, 0};
std::vector<int> thruster_direction_map = {1, 1, 1, -1};
std::vector<int> pwm_offsets = {100, 100, 100, 100};

// Iterates through thruster 0 to 3, where 0 is front right, iterated
// clockwise
for (int i = 0; i < 4; i++) {

int center_pwm_value = 1500;
int offset_from_center_value =
pwm_values[i] - center_pwm_value + pwm_offsets[i];
int pwm_value_correct_direction =
center_pwm_value + thruster_direction_map[i] * offset_from_center_value;

int pwm_clamped = std::min(std::max(pwm_value_correct_direction, 1300),
1700); // min 1100, max 1900
pwm_msg.positive_width_us.push_back(pwm_clamped);
pwm_msg.pins.push_back(thruster_to_pin_map[i]);
}

publisher_->publish(pwm_msg);

// thrusterInterface.publish_thrust_to_escs(forces_in_grams);
}

int main(int argc, char * argv[])
{
int main(int argc, char *argv[]) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<ThrusterInterfaceROS>());
rclcpp::shutdown();
Expand Down
15 changes: 9 additions & 6 deletions motion/thruster_interface/tests/test_thruster_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,20 @@ std::vector<uint8_t> pwm_to_bytes(const std::vector<int> &pwm_values) {
// int pwm_thr_3 = interpolate(pwm_table, desired_force_thr_3);
// int pwm_thr_4 = interpolate(pwm_table, desired_force_thr_4);

// std::vector<int> pwm_values = {pwm_thr_1, pwm_thr_2, pwm_thr_3, pwm_thr_4};
// std::vector<uint8_t> pwm_bytes = pwm_to_bytes(pwm_values);
// std::vector<int> pwm_values = {pwm_thr_1, pwm_thr_2, pwm_thr_3,
// pwm_thr_4}; std::vector<uint8_t> pwm_bytes = pwm_to_bytes(pwm_values);

// int data_size = pwm_bytes.size();

// // Send the I2C message
// auto write_feedback = write(file, pwm_bytes.data(), data_size);
// if (write_feedback != data_size) {
// std::cerr << "Feedback: " << write_feedback << " Data size: " << data_size
// std::cerr << "Feedback: " << write_feedback << " Data size: " <<
// data_size
// << ". Error sending data, ignoring...\n";
// } else {
// std::cout << "Feedback: " << write_feedback << " Data size: " << data_size
// std::cout << "Feedback: " << write_feedback << " Data size: " <<
// data_size
// << " Data sent successfully!\n";
// }

Expand All @@ -153,8 +155,9 @@ std::vector<uint8_t> pwm_to_bytes(const std::vector<int> &pwm_values) {
// ThrusterInterface thruster_interface("mapping_file.txt");
// std::vector<int> pwm_values = {1000, 1500, 2000};
// std::vector<uint8_t> expected_bytes = {0x03, 0xE8, 0x05, 0xDC, 0x07, 0xD0};
// std::vector<uint8_t> actual_bytes = thruster_interface.pwm_to_bytes(pwm_values);
// EXPECT_EQ(expected_bytes, actual_bytes);
// std::vector<uint8_t> actual_bytes =
// thruster_interface.pwm_to_bytes(pwm_values); EXPECT_EQ(expected_bytes,
// actual_bytes);
// }

// TEST(ThrusterInterfaceTest, PublishThrustToEscsTest) {
Expand Down

0 comments on commit 9ed9d19

Please sign in to comment.