From 940d2782c361acee060488d8b07636eb3fc2eb7f Mon Sep 17 00:00:00 2001 From: Corwin Perren Date: Sat, 3 Feb 2018 17:12:15 -0800 Subject: [PATCH] Added beginnings of getting motor control working from within ROS. Current launch file and tester node work and make the motor movegit status! --- .../inspectionProfiles/Project_Default.xml | 15 + .../inspectionProfiles/profiles_settings.xml | 7 + software/rover/rover_drive/.idea/misc.xml | 14 + software/rover/rover_drive/.idea/modules.xml | 8 + .../rover/rover_drive/.idea/rover_drive.iml | 12 + .../rover/rover_drive/.idea/workspace.xml | 598 ++++++++++++++++++ software/rover/rover_drive/CMakeLists.txt | 198 ++++++ .../rover/rover_drive/launch/example.launch | 8 + .../rover/rover_drive/msg/RoverMotorDrive.msg | 7 + software/rover/rover_drive/package.xml | 65 ++ software/rover/rover_drive/src/rover_drive.py | 0 .../rover/rover_drive/src/rover_drive_node.py | 104 +++ .../src/rover_drive_node_tester.py | 31 + 13 files changed, 1067 insertions(+) create mode 100644 software/rover/rover_drive/.idea/inspectionProfiles/Project_Default.xml create mode 100644 software/rover/rover_drive/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 software/rover/rover_drive/.idea/misc.xml create mode 100644 software/rover/rover_drive/.idea/modules.xml create mode 100644 software/rover/rover_drive/.idea/rover_drive.iml create mode 100644 software/rover/rover_drive/.idea/workspace.xml create mode 100644 software/rover/rover_drive/CMakeLists.txt create mode 100644 software/rover/rover_drive/launch/example.launch create mode 100644 software/rover/rover_drive/msg/RoverMotorDrive.msg create mode 100644 software/rover/rover_drive/package.xml delete mode 100644 software/rover/rover_drive/src/rover_drive.py create mode 100755 software/rover/rover_drive/src/rover_drive_node.py create mode 100644 software/rover/rover_drive/src/rover_drive_node_tester.py diff --git a/software/rover/rover_drive/.idea/inspectionProfiles/Project_Default.xml b/software/rover/rover_drive/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..cb8673d --- /dev/null +++ b/software/rover/rover_drive/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/software/rover/rover_drive/.idea/inspectionProfiles/profiles_settings.xml b/software/rover/rover_drive/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/software/rover/rover_drive/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/software/rover/rover_drive/.idea/misc.xml b/software/rover/rover_drive/.idea/misc.xml new file mode 100644 index 0000000..74cce13 --- /dev/null +++ b/software/rover/rover_drive/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/software/rover/rover_drive/.idea/modules.xml b/software/rover/rover_drive/.idea/modules.xml new file mode 100644 index 0000000..8cbe4b5 --- /dev/null +++ b/software/rover/rover_drive/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/software/rover/rover_drive/.idea/rover_drive.iml b/software/rover/rover_drive/.idea/rover_drive.iml new file mode 100644 index 0000000..6f63a63 --- /dev/null +++ b/software/rover/rover_drive/.idea/rover_drive.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/software/rover/rover_drive/.idea/workspace.xml b/software/rover/rover_drive/.idea/workspace.xml new file mode 100644 index 0000000..d05528a --- /dev/null +++ b/software/rover/rover_drive/.idea/workspace.xml @@ -0,0 +1,598 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1517699292320 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/software/rover/rover_drive/CMakeLists.txt b/software/rover/rover_drive/CMakeLists.txt new file mode 100644 index 0000000..eb78fe1 --- /dev/null +++ b/software/rover/rover_drive/CMakeLists.txt @@ -0,0 +1,198 @@ +cmake_minimum_required(VERSION 2.8.3) +project(rover_drive) + +## Compile as C++11, supported in ROS Kinetic and newer +# add_compile_options(-std=c++11) + +## Find catkin macros and libraries +## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) +## is used, also find other catkin packages +find_package(catkin REQUIRED COMPONENTS + rospy + std_msgs + message_generation +) + +## System dependencies are found with CMake's conventions +# find_package(Boost REQUIRED COMPONENTS system) + + +## Uncomment this if the package has a setup.py. This macro ensures +## modules and global scripts declared therein get installed +## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html +# catkin_python_setup() + +################################################ +## Declare ROS messages, services and actions ## +################################################ + +## To declare and build messages, services or actions from within this +## package, follow these steps: +## * Let MSG_DEP_SET be the set of packages whose message types you use in +## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...). +## * In the file package.xml: +## * add a build_depend tag for "message_generation" +## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET +## * If MSG_DEP_SET isn't empty the following dependency has been pulled in +## but can be declared for certainty nonetheless: +## * add a run_depend tag for "message_runtime" +## * In this file (CMakeLists.txt): +## * add "message_generation" and every package in MSG_DEP_SET to +## find_package(catkin REQUIRED COMPONENTS ...) +## * add "message_runtime" and every package in MSG_DEP_SET to +## catkin_package(CATKIN_DEPENDS ...) +## * uncomment the add_*_files sections below as needed +## and list every .msg/.srv/.action file to be processed +## * uncomment the generate_messages entry below +## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...) + +## Generate messages in the 'msg' folder + add_message_files( + FILES + RoverMotorDrive.msg + ) + +## Generate services in the 'srv' folder +# add_service_files( +# FILES +# Service1.srv +# Service2.srv +# ) + +## Generate actions in the 'action' folder +# add_action_files( +# FILES +# Action1.action +# Action2.action +# ) + +## Generate added messages and services with any dependencies listed here + generate_messages( + DEPENDENCIES + std_msgs # Or other packages containing msgs +) + +################################################ +## Declare ROS dynamic reconfigure parameters ## +################################################ + +## To declare and build dynamic reconfigure parameters within this +## package, follow these steps: +## * In the file package.xml: +## * add a build_depend and a run_depend tag for "dynamic_reconfigure" +## * In this file (CMakeLists.txt): +## * add "dynamic_reconfigure" to +## find_package(catkin REQUIRED COMPONENTS ...) +## * uncomment the "generate_dynamic_reconfigure_options" section below +## and list every .cfg file to be processed + +## Generate dynamic reconfigure parameters in the 'cfg' folder +# generate_dynamic_reconfigure_options( +# cfg/DynReconf1.cfg +# cfg/DynReconf2.cfg +# ) + +################################### +## catkin specific configuration ## +################################### +## The catkin_package macro generates cmake config files for your package +## Declare things to be passed to dependent projects +## INCLUDE_DIRS: uncomment this if your package contains header files +## LIBRARIES: libraries you create in this project that dependent projects also need +## CATKIN_DEPENDS: catkin_packages dependent projects also need +## DEPENDS: system dependencies of this project that dependent projects also need +catkin_package( +# INCLUDE_DIRS include +# LIBRARIES rover_drive +# CATKIN_DEPENDS rospy +# DEPENDS system_lib +) + +########### +## Build ## +########### + +## Specify additional locations of header files +## Your package locations should be listed before other locations +include_directories( +# include + ${catkin_INCLUDE_DIRS} +) + +## Declare a C++ library +# add_library(${PROJECT_NAME} +# src/${PROJECT_NAME}/rover_drive.cpp +# ) + +## Add cmake target dependencies of the library +## as an example, code may need to be generated before libraries +## either from message generation or dynamic reconfigure +# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Declare a C++ executable +## With catkin_make all packages are built within a single CMake context +## The recommended prefix ensures that target names across packages don't collide +# add_executable(${PROJECT_NAME}_node src/rover_drive_node.cpp) + +## Rename C++ executable without prefix +## The above recommended prefix causes long target names, the following renames the +## target back to the shorter version for ease of user use +## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" +# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") + +## Add cmake target dependencies of the executable +## same as for the library above +# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Specify libraries to link a library or executable target against +# target_link_libraries(${PROJECT_NAME}_node +# ${catkin_LIBRARIES} +# ) + +############# +## Install ## +############# + +# all install targets should use catkin DESTINATION variables +# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html + +## Mark executable scripts (Python etc.) for installation +## in contrast to setup.py, you can choose the destination +# install(PROGRAMS +# scripts/my_python_script +# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark executables and/or libraries for installation +# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node +# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark cpp header files for installation +# install(DIRECTORY include/${PROJECT_NAME}/ +# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} +# FILES_MATCHING PATTERN "*.h" +# PATTERN ".svn" EXCLUDE +# ) + +## Mark other files for installation (e.g. launch and bag files, etc.) +# install(FILES +# # myfile1 +# # myfile2 +# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} +# ) + +############# +## Testing ## +############# + +## Add gtest based cpp test target and link libraries +# catkin_add_gtest(${PROJECT_NAME}-test test/test_rover_drive.cpp) +# if(TARGET ${PROJECT_NAME}-test) +# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) +# endif() + +## Add folders to be run by python nosetests +# catkin_add_nosetests(test) diff --git a/software/rover/rover_drive/launch/example.launch b/software/rover/rover_drive/launch/example.launch new file mode 100644 index 0000000..c4cee6a --- /dev/null +++ b/software/rover/rover_drive/launch/example.launch @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/software/rover/rover_drive/msg/RoverMotorDrive.msg b/software/rover/rover_drive/msg/RoverMotorDrive.msg new file mode 100644 index 0000000..1f29cb1 --- /dev/null +++ b/software/rover/rover_drive/msg/RoverMotorDrive.msg @@ -0,0 +1,7 @@ +bool first_motor_direction +bool first_motor_sleep +uint16 first_motor_speed + +bool second_motor_direction +bool second_motor_sleep +uint16 second_motor_speed \ No newline at end of file diff --git a/software/rover/rover_drive/package.xml b/software/rover/rover_drive/package.xml new file mode 100644 index 0000000..5d20e85 --- /dev/null +++ b/software/rover/rover_drive/package.xml @@ -0,0 +1,65 @@ + + + rover_drive + 0.0.0 + The rover_drive package + + + + + caperren + + + + + + TODO + + + + + + + + + + + + + + + + + + + + + + + + message_generation + + + + + + message_runtime + + + + + catkin + rospy + std_msgs + rospy + std_msgs + rospy + std_msgs + + + + + + + + diff --git a/software/rover/rover_drive/src/rover_drive.py b/software/rover/rover_drive/src/rover_drive.py deleted file mode 100644 index e69de29..0000000 diff --git a/software/rover/rover_drive/src/rover_drive_node.py b/software/rover/rover_drive/src/rover_drive_node.py new file mode 100755 index 0000000..8b430ea --- /dev/null +++ b/software/rover/rover_drive/src/rover_drive_node.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +##################################### +# Imports +##################################### +# Python native imports +import serial.rs485 +import minimalmodbus +import time + +import rospy +from rover_drive.msg import RoverMotorDrive + +# Custom Imports + +##################################### +# Global Variables +##################################### +NODE_NAME = "rover_drive" + +DEFAULT_PORT = "/dev/ttyUSB0" +DEFAULT_BAUD = 2000000 + +MOTOR_TOPIC = "motoroneandtwo" + +FIRST_MOTOR_ID = 1 +SECOND_MOTOR_ID = 2 + +COMMUNICATIONS_TIMEOUT = 0.3 # Seconds + +MOTOR_DRIVERS_DIRECTION = { + "FORWARDS": 1, + "BACKWARDS": 0 +} + +MOTOR_DRIVER_REGISTER_MAP = { + "DIRECTION": 0, + "SPEED": 1 +} + +MOTOR_DRIVER_DEFAULT_MESSAGE = [ + MOTOR_DRIVERS_DIRECTION["FORWARDS"], + 0 +] + + +##################################### +# RoverDrive Class Definition +##################################### +class RoverDrive(object): + def __init__(self): + + rospy.init_node(NODE_NAME) + + self.port = rospy.get_param("port", DEFAULT_PORT) + self.baud = rospy.get_param("baud", DEFAULT_BAUD) + + self.first_motor_modbus_id = rospy.get_param("first_motor_modbus_id", FIRST_MOTOR_ID) + self.second_motor_modbus_id = rospy.get_param("second_motor_modbus_id", SECOND_MOTOR_ID) + + self.motors_subscriber_topic = rospy.get_param("topic", MOTOR_TOPIC) + + self.first_motor = minimalmodbus.Instrument(self.port, self.first_motor_modbus_id) + self.second_motor = minimalmodbus.Instrument(self.port, self.second_motor_modbus_id) + + self.motors_subscriber = rospy.Subscriber(MOTOR_TOPIC, RoverMotorDrive, self.__motor_message_callback) + + self.__setup_minimalmodbus_for_485() + + self.run() + + def run(self): + while not rospy.is_shutdown(): + time.sleep(0.25) + # try: + # self.first_motor.write_register(1, 10000) + # except IOError, error: + # pass + # + # try: + # self.second_motor.write_register(1, 30000) + # except IOError, error: + # pass + + def __setup_minimalmodbus_for_485(self): + self.first_motor.serial = serial.rs485.RS485(self.port, baudrate=self.baud, timeout=COMMUNICATIONS_TIMEOUT) + self.first_motor.serial.rs485_mode = \ + serial.rs485.RS485Settings(rts_level_for_rx=1, rts_level_for_tx=0, delay_before_rx=0, delay_before_tx=0) + + self.second_motor.serial = serial.rs485.RS485(self.port, baudrate=self.baud, timeout=COMMUNICATIONS_TIMEOUT) + self.second_motor.serial.rs485_mode = \ + serial.rs485.RS485Settings(rts_level_for_rx=1, rts_level_for_tx=0, delay_before_rx=0, delay_before_tx=0) + + def __motor_message_callback(self, data): + first_motor_register_data = list(MOTOR_DRIVER_DEFAULT_MESSAGE) # Makes a copy + first_motor_register_data[MOTOR_DRIVER_REGISTER_MAP["DIRECTION"]] = int(data.first_motor_direction) + first_motor_register_data[MOTOR_DRIVER_REGISTER_MAP["SPEED"]] = data.first_motor_speed + print first_motor_register_data + self.first_motor.write_registers(0, first_motor_register_data) + +##################################### +# Main Definition +##################################### +if __name__ == "__main__": + drive = RoverDrive() diff --git a/software/rover/rover_drive/src/rover_drive_node_tester.py b/software/rover/rover_drive/src/rover_drive_node_tester.py new file mode 100644 index 0000000..33ef88a --- /dev/null +++ b/software/rover/rover_drive/src/rover_drive_node_tester.py @@ -0,0 +1,31 @@ +import rospy +import time + +from rover_drive.msg import RoverMotorDrive + +rospy.init_node("drive_tester") + +pub = rospy.Publisher("/drive/motoroneandtwo", RoverMotorDrive, queue_size=10) + +speed_step = 500 + +last_dir = 0 + +sleep_time = 0.01 + +while not rospy.is_shutdown(): + drive = RoverMotorDrive() + + last_dir = not last_dir + for i in range(0, 65535, speed_step): + drive.first_motor_direction = last_dir + drive.first_motor_speed = i + pub.publish(drive) + time.sleep(sleep_time) + print i + for i in range(65535, 0, -speed_step): + drive.first_motor_direction = last_dir + drive.first_motor_speed = i + pub.publish(drive) + time.sleep(sleep_time) + print i