|
|
@ -51,11 +51,17 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void init_servos() { |
|
|
|
void init_servos() { |
|
|
|
_servos = { Servo { 9 }, Servo { 25 } }; |
|
|
|
_servos = { Servo { 23 }, Servo { 24 } }; |
|
|
|
for (auto& servo : _servos) { |
|
|
|
for (auto& servo : _servos) { |
|
|
|
#ifdef BUILD_RASPBERRY_PI |
|
|
|
#ifdef BUILD_RASPBERRY_PI |
|
|
|
set_mode(_pi, servo.pin, PI_OUTPUT); |
|
|
|
int result = set_mode(_pi, servo.pin, PI_OUTPUT); |
|
|
|
gpio_write(_pi, servo.pin, PI_LOW); |
|
|
|
if (result != 0) { |
|
|
|
|
|
|
|
RCLCPP_ERROR(get_logger(), "Error setting pin mode: %d", result); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
result = gpio_write(_pi, servo.pin, PI_LOW); |
|
|
|
|
|
|
|
if (result != 0) { |
|
|
|
|
|
|
|
RCLCPP_ERROR(get_logger(), "Error writing pin: %d", result); |
|
|
|
|
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -70,9 +76,12 @@ public: |
|
|
|
value = std::max(value, -90.f); |
|
|
|
value = std::max(value, -90.f); |
|
|
|
// convert value from -90 to 90 to int from 1000 to 2000 (us)
|
|
|
|
// convert value from -90 to 90 to int from 1000 to 2000 (us)
|
|
|
|
unsigned int decoded_value = static_cast<unsigned int>( |
|
|
|
unsigned int decoded_value = static_cast<unsigned int>( |
|
|
|
((value / 90.f) * 500.f) + 1000); |
|
|
|
((value / 90.f) * 500.f) + 1500); |
|
|
|
#ifdef BUILD_RASPBERRY_PI |
|
|
|
#ifdef BUILD_RASPBERRY_PI |
|
|
|
set_servo_pulsewidth(_pi, servo.pin, decoded_value); |
|
|
|
int result = set_servo_pulsewidth(_pi, servo.pin, decoded_value); |
|
|
|
|
|
|
|
if (result != 0) { |
|
|
|
|
|
|
|
RCLCPP_ERROR(get_logger(), "error setting servo pulse width: %d", result); |
|
|
|
|
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -103,14 +112,21 @@ public: |
|
|
|
std::bind(&ControlsNode::on_loop, this)); |
|
|
|
std::bind(&ControlsNode::on_loop, this)); |
|
|
|
|
|
|
|
|
|
|
|
_last_value = 0; |
|
|
|
_last_value = 0; |
|
|
|
// start 100Hz servo timer
|
|
|
|
|
|
|
|
_servo_timer = create_wall_timer(std::chrono::milliseconds(10), |
|
|
|
|
|
|
|
std::bind(&ControlsNode::on_servo_update, this)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void on_message(const UaspireCommand::SharedPtr cmd) { |
|
|
|
void on_message(const UaspireCommand::SharedPtr cmd) { |
|
|
|
RCLCPP_INFO(get_logger(), "Command received: %s", cmd->data.c_str()); |
|
|
|
RCLCPP_INFO(get_logger(), "Command received: %s", cmd->data.c_str()); |
|
|
|
|
|
|
|
if (cmd->data == "start") { |
|
|
|
|
|
|
|
// start 25Hz servo timer
|
|
|
|
|
|
|
|
_servo_timer = create_wall_timer(std::chrono::milliseconds(40), |
|
|
|
|
|
|
|
std::bind(&ControlsNode::on_servo_update, this)); |
|
|
|
|
|
|
|
} else if (cmd->data == "stop") { |
|
|
|
|
|
|
|
if (_servo_timer) { |
|
|
|
|
|
|
|
_servo_timer->cancel(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
_servo_timer.reset(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void on_loop() { |
|
|
|
void on_loop() { |
|
|
|