Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bringup/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/config)
endif()

# Install controller_switcher script from bringup package if present
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/controller_switcher.py)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/controller_switcher.py OR ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmd_vel_to_rmd.py)
install(PROGRAMS
scripts/controller_switcher.py
scripts/cmd_vel_to_rmd.py
DESTINATION lib/${PROJECT_NAME}
)
endif()
Expand Down
83 changes: 83 additions & 0 deletions src/bringup/scripts/cmd_vel_to_rmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from geometry_msgs.msg import TwistStamped
import socket
import struct
import time

CAN_FMT = "=IH2x8s"
CAN_SIZE = struct.calcsize(CAN_FMT)


CAN_IDS = [0x145, 0x141, 0x142, 0x144]
CAN_INTERFACE = "can1"
MAX_SPEED_DPS = 500.0
SCALE = 100.0
TOPIC = "/rear_ackermann_controller/reference"


SPEED_CONTROL_CMD = 0xA2




class CmdVelToRMD(Node):
def __init__(self):
super().__init__("cmd_vel_to_rmd")


self._sock = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
self._sock.bind((CAN_INTERFACE,))
self.get_logger().info(f"Listening on {TOPIC}, sending to {[hex(i) for i in CAN_IDS]}")


self.create_subscription(TwistStamped, TOPIC, self._cb, 10)


def _cb(self, msg: TwistStamped):
speed_dps = msg.twist.linear.x * SCALE
speed_dps = max(-MAX_SPEED_DPS, min(MAX_SPEED_DPS, speed_dps))
speed_ctrl = int(speed_dps / 0.01) & 0xFFFFFFFF


data = bytes([
SPEED_CONTROL_CMD, 0x00, 0x00, 0x00,
(speed_ctrl) & 0xFF,
(speed_ctrl >> 8) & 0xFF,
(speed_ctrl >> 16) & 0xFF,
(speed_ctrl >> 24) & 0xFF,
])


for can_id in CAN_IDS:
self._sock.send(struct.pack(CAN_FMT, can_id, 8, data))
time.sleep(.001)


def destroy_node(self):
self._sock.close()
super().destroy_node()




def main(args=None):
rclpy.init(args=args)
node = CmdVelToRMD()
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
finally:
node.destroy_node()
rclpy.shutdown()




if __name__ == "__main__":
main()



4 changes: 2 additions & 2 deletions src/config/cyclonedds/config_jetson.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ https://raw.githubusercontent.com/eclipse-cyclonedds/cyclonedds/master/etc/cyclo
</General>
<Discovery>
<Peers>
<Peer address="192.168.1.1"/>
<Peer address="192.168.1.12"/>
<Peer address="192.168.88.90"/>
<Peer address="192.168.88.10"/>
</Peers>
<ParticipantIndex>auto</ParticipantIndex>
<MaxAutoParticipantIndex>1000</MaxAutoParticipantIndex>
Expand Down
4 changes: 2 additions & 2 deletions src/config/cyclonedds/config_right_base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ https://raw.githubusercontent.com/eclipse-cyclonedds/cyclonedds/master/etc/cyclo
</General>
<Discovery>
<Peers>
<Peer address="192.168.1.12"/>
<Peer address="192.168.1.1"/>
<Peer address="192.168.88.90"/>
<Peer address="192.168.88.10"/>
</Peers>
<ParticipantIndex>auto</ParticipantIndex>
<MaxAutoParticipantIndex>1000</MaxAutoParticipantIndex>
Expand Down
4 changes: 2 additions & 2 deletions src/description/ros2_control/arm/arm.rmd.ros2_control.xacro
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<ros2_control name="${name}" type="system">
<hardware>
<plugin>rmd_ros2_control/RMDHardwareInterface</plugin>
<param name="update_rate">2</param> <!-- Hz -->
<param name="update_rate">10</param> <!-- Hz -->
<param name="logger_rate">5</param> <!-- Hz -->
<param name="logger_state">0</param> <!-- 0 for off, 1 for on -->
<param name="can_interface">can0</param>
</hardware>

<joint name="shoulder_pitch">
<param name="node_id">0x14A</param>
<param name="node_id">0x149</param>
<param name="gear_ratio">100</param>
<param name="joint_orientation">1.0</param>
<param name="operating_velocity">150</param> <!-- motor dps -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ros2_control name="${name}" type="system">
<hardware>
<plugin>smc_ros2_control/SMCHardwareInterface</plugin>
<param name="update_rate">2</param> <!-- Hz -->
<param name="update_rate">10</param> <!-- Hz -->
<param name="logger_rate">5</param> <!-- Hz -->
<param name="logger_state">0</param> <!-- 0 for off, 1 for on -->
<param name="can_interface">can0</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</hardware>

<joint name="steer_bl_joint">
<param name="node_id">5</param>
<param name="node_id">7</param>
<param name="gear_ratio">26</param>
<command_interface name="position"/>
<command_interface name="velocity"/>
Expand All @@ -21,7 +21,7 @@
</joint>

<joint name="steer_br_joint">
<param name="node_id">6</param>
<param name="node_id">5</param>
<param name="gear_ratio">26</param>
<command_interface name="position"/>
<command_interface name="velocity"/>
Expand Down
10 changes: 5 additions & 5 deletions src/description/ros2_control/drive/drive.rmd.ros2_control.xacro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ros2_control name="${name}" type="system">
<hardware>
<plugin>rmd_ros2_control/RMDHardwareInterface</plugin>
<param name="update_rate">30</param> <!-- Hz -->
<param name="update_rate">10</param> <!-- Hz -->
<param name="logger_rate">5</param> <!-- Hz -->
<param name="logger_state">0</param> <!-- 0 for off, 1 for on -->
<param name="can_interface">can0</param>
Expand All @@ -24,7 +24,7 @@
</joint>

<joint name="propulsion_fr_joint">
<param name="node_id">0x149</param>
<param name="node_id">0x145</param>
<param name="gear_ratio">1</param>
<param name="joint_orientation">1.0</param>
<param name="operating_velocity">150</param> <!-- motor dps -->
Expand All @@ -37,7 +37,7 @@
</joint>

<joint name="propulsion_bl_joint">
<param name="node_id">0x141</param>
<param name="node_id">0x144</param>
<param name="gear_ratio">1</param>
<param name="joint_orientation">-1.0</param>
<param name="operating_velocity">150</param> <!-- motor dps -->
Expand All @@ -50,7 +50,7 @@
</joint>

<joint name="propulsion_br_joint">
<param name="node_id">0x143</param>
<param name="node_id">0x141</param>
<param name="gear_ratio">1</param>
<param name="joint_orientation">1.0</param>
<param name="operating_velocity">150</param> <!-- motor dps -->
Expand All @@ -63,4 +63,4 @@
</joint>
</ros2_control>
</xacro:macro>
</robot>
</robot>
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ return_type ODriveHardwareInterface::write(const rclcpp::Time&, const rclcpp::Du
msg.Input_Pos = (axis.pos_setpoint_ * axis.gear_ratio_) / (2 * M_PI);
msg.Vel_FF = axis.vel_input_enabled_ ? ((axis.vel_setpoint_ * axis.gear_ratio_) / (2 * M_PI)) : 0.0f;
msg.Torque_FF = axis.torque_input_enabled_ ? (axis.torque_setpoint_ / axis.gear_ratio_) : 0.0f;
// RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Writing positions for ODrive %d Setpoint: %f, Joint angle of motor (rev): %f", axis.node_id_, axis.pos_setpoint_, (axis.pos_setpoint_ * axis.gear_ratio_) / (2 * M_PI));
//RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Writing positions for ODrive %d Setpoint: %f, Joint angle of motor (rev): %f", axis.node_id_, axis.pos_setpoint_, (axis.pos_setpoint_ * axis.gear_ratio_) / (2 * M_PI));

axis.send(msg);
} else if (axis.vel_input_enabled_) {
Set_Input_Vel_msg_t msg;
msg.Input_Vel = (axis.vel_setpoint_ * axis.gear_ratio_) / (2 * M_PI);
msg.Input_Torque_FF = axis.torque_input_enabled_ ? (axis.torque_setpoint_ / axis.gear_ratio_): 0.0f;
// RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Writing velocities for ODrive %d Joint velocity of motor (rev/s): %f", axis.node_id_, (axis.vel_setpoint_ * axis.gear_ratio_) / (2 * M_PI));
RCLCPP_INFO(rclcpp::get_logger("ODriveHardwareInterface"), "Writing velocities for ODrive %d Joint velocity of motor (rev/s): %f", axis.node_id_, (axis.vel_setpoint_ * axis.gear_ratio_) / (2 * M_PI));

axis.send(msg);
} else if (axis.torque_input_enabled_) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

#include "ICanBus.hpp"
#include <memory>
#include <thread>
#include <atomic>
#include <queue>
#include <mutex>
#include <condition_variable>

namespace CANLib {

Expand All @@ -17,13 +19,21 @@ class SocketCanBus : public ICanBus {
bool send(const CanFrame& frame) override;

private:
static constexpr size_t kMaxTxQueueDepth = 256;

int socketFd_;
ReceiveCallback receiveCallback_;
std::thread receiveThread_;
std::thread txThread_;
std::atomic<bool> running_;
std::string interfaceName_;

std::queue<CanFrame> txQueue_;
std::mutex txMutex_;
std::condition_variable txCv_;

void receiveLoop_();
void txLoop_();
};

} // namespace CANLib
Loading
Loading