Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-attempting rosserial for VEX Cortex #380

Merged
merged 14 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 8 additions & 0 deletions rosserial_vex_cortex/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Changelog for package rosserial_vex_cortex
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0.7.7
-----------------------------
- README fixes
- started tagging versions
- initial was wrong version number, fixed.
23 changes: 23 additions & 0 deletions rosserial_vex_cortex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 2.8.3)
project(rosserial_vex_cortex)

find_package(catkin REQUIRED COMPONENTS)

catkin_python_setup()

catkin_package(CATKIN_DEPENDS)

install(
DIRECTORY src/ros_lib
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/src
)

install(
PROGRAMS scripts/genproject.sh
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

catkin_install_python(
PROGRAMS src/${PROJECT_NAME}/make_libraries.py
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
124 changes: 124 additions & 0 deletions rosserial_vex_cortex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# rosserial for VEX Cortex

This package contains everything needed to run rosserial on the [VEX Cortex](https://www.vexrobotics.com/276-2194.html), on the [PROS Kernel](https://pros.cs.purdue.edu/cortex/index.html).

# Requirements
Software:
1. Linux (Only tested on Ubuntu 18.04LTS) (possible, but not tested, on windows + virtual machines with USB support)
2. ROS installed on Linux (Only tested on ROS Melodic) - [installation guide](http://wiki.ros.org/melodic/Installation/Source).
3. PROS installed on Linux - [installation guide](https://pros.cs.purdue.edu/cortex/getting-started/index.html)
Hardware:
1. VEX essentials: VEX Cortex, Joystick, VexNet keys, battery
2. VEX Programming Cable
3. (recommended, for debugging) a [USB-serial adapter](https://www.adafruit.com/product/954)
4. Three Male-Male jumper wires for USB-serial adapter

# Setup
Setup a ROS workspace and build rosserial packages (including rosserial_vex_cortex) from source:
```bash
source /opt/ros/melodic/setup.bash # or replace melodic with your corresponding ROS version name
mkdir -p <your-workspace-name>/src
cd <your-workspace-name>/src
git clone https://github.com/ros-drivers/rosserial.git
cd ..
catkin_make
catkin_make install # this will generate folders in the workspace that contain executable scripts.
```

# Usage Example (Linux)
This will show you how to run the "hello world" example PROS project. Plug in the VEX Programming cable to the computer and the joystick, plug the VexNet keys into the Cortex and the joystick. Power cycle the Joystick and Cortex between cortex downloads for optimal usage.

### Step 1 : Run the ROS instance
open a terminal window and run:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/open/Open/

```bash
source /opt/ros/melodic/setup.bash # or replace melodic with your corresponding ROS version name
roscore
```
this will start ROS on the host linux machine.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/this/This/
s/linux/Linux/


### Step 2: get the VEX Cortex to talk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/get/Get/

This step generates a PROS project with the necessary includes for ROS to talk to the VEX Cortex, and then downloads the "Hello World" program onto the Cortex.
Open another terminal window and run:
```bash
source <your workspace name>/install/setup.bash
cd /anywhere/on/your/computer
# below runs the generate command to build the PROS project with the rosserial libraries installed.
rosrun rosserial_vex_cortex genproject.sh <pros-project-name>
cd <pros-project-name>
pros make upload # uploads the hello world demo onto the cortex.
```
The Cortex is now trying to send messages on the "chatter" topic, as long as it is plugged into the VEX Programming cable. see `src/opcontrol.cpp` and look around so see how the PROS side works.

### Step 3: Begin the serial node!
This is how rosserial clients are able to communicate with the ROS system.
```bash
source <your-workspace-name>/install/setup.bash
rosrun rosserial_arduino serial_node.py _port:=/dev/ttyACM0 _baud:=115200
```
see the `Serial Connections` section below, for more information about how the serial ports are used.

### Step 4: Display a topic in a terminal (technically optional)
open another terminal window and run:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/open/Open/

```bash
source /opt/ros/melodic/setup.bash # or replace melodic with your corresponding ROS version name
rostopic echo chatter
```
this will display any messages that come through on the "chatter" topic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/this/This/


If everyting is working properly, then the terminal from step 2 should show outputs of `"Hello World!"` This means the bridge between the cortex and ROS is established.
You are now able to use ROS with the VEX Cortex!


# Physical Serial Connections
The optimal setup for this project is with two physical serial connections, one for rosserial to function, and one for debugging. The default connection for rosserial is the VEX Programming Cable, and the default debugging serial connection is UART2.

Since the The VEX programming cable provides the default rosserial connection, the baud rate of the ROS serial connecting node must be 115200 Hz, which is why the command reads:

```bash
rosserial_arduino serial_node _port:=/dev/ttyACM0 _baud:=115200
```
This overrides the default (57600) Hz.To switch the rosserial/debug serial connections, see `logger.h` in your generated PROS project. If you do end up using UART1 or UART2 for rosserial instead of debugging, update the USB device argument: `_port:=/dev/ttyUSB0` instead, and update the baud rate argument, or simply remove it for the default 57600 Hz.

Also, the USB device path for the VEX Programming cable on linux may either be `/dev/ttyACM0` or `/dev/ttyACM1`. To figure out which to use as an argument, use `pros lsusb`, or unplug/replug the cable from/into the computer and run `dmesg` and look at the last lines.

Viewing the UART debug stream requires a [USB-serial adapter](https://www.adafruit.com/product/954) for your computer, and it needs to be plugged in correctly.
To set up the wires with the cable linked above, use this layout:
[layout](./uartdiagram.png)
You need male-male jumper wires to plug in this adapter.
Run `dmesg` right after plugging in the adapter to identify the USB device path - it is most likely `/dev/ttyUSB0`.

To view serial output from UART2, (instead of running `pros terminal`, which only works for the VEX Programming cable), use screen:
```bash
# to install screen: sudo apt-get install screen
screen /dev/ttyUSB0 57600
```
Again, you may need to change the above command to use the correct USB device path and baud rate, if you change the default configuration.

PROS does not provide a debugger, but it does provide printing.
use vexroslog(char* out, ...) just like you would use fprintf from the PROS API:

```bash
vexroslog("hello, my favorite number is %d", 3);
```
remember to include this header in your code for logging!
```cpp
#include "logger.h"
```

# Generating Custom Messages
To design you own ROS messages, it is necessary to add the `msgs` directory to this project,
add the `message_generation` dependency, and make several modifications to this project's `CMakeLists.txt`.
This infrastructure is removed from this project by default,
because there are many useful built-in message types that are used commonly across ROS packages anyway.

See documentation and source of sister packages, such as `rosserial_arduino`, for more information about generating custom messages.

# Limitations
Global scope variables causes segmentation faults. Unlike in the other rosserial examples, avoid ROS objects in the global scope.

This has been developed and tested on ROS melodic, but it should work on many earier/later versions as well.

# Speed
Over the VEX Programming cable/VexNet wireless connection, the simplest messages can stream at upwards of 200Hz. More complex messages, such as sensor_msgs::JointState, can be published at 50hz. The baud rate degrades as the distance from the cortex and the Joystick increases, however.

A wired UART1 or UART2 connection should have higher stability and frequency with arbitrarily-sized messages, with no degradation in baud rate as distance increases.
17 changes: 17 additions & 0 deletions rosserial_vex_cortex/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<package>
<name>rosserial_vex_cortex</name>
<version>0.7.7</version>
<description>
rosserial for Cortex/AVR platforms.
</description>
<author>Cannon</author>
<maintainer email="canyonturtle@gmail.com">Canyon Turtle</maintainer>
<license>BSD</license>
<!--<url>http://ros.org/wiki/rosserial_arduino</url>-->

<buildtool_depend>catkin</buildtool_depend>

<run_depend>rospy</run_depend>
<run_depend>rosserial_client</run_depend>
<run_depend>rosserial_python</run_depend>
</package>
21 changes: 21 additions & 0 deletions rosserial_vex_cortex/scripts/genproject.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# generate new pros project with name as first argument
pros conduct new $1

# go into incude directory and generate ros message library.
cd $1/include
rosrun rosserial_vex_cortex make_libraries.py .

# custom include for files containing strlen.
# grep -lnrw '.' -e 'strlen'
grep -lrnw '.' -e 'strlen' | xargs sed -i '1 s@^@#include "vexstrlen.h"\n@g; s@strlen(@vexstrlen(@g'

# move the cpp files into the src directory.
mv ros_lib/*.cpp ../src
mv ros_lib/examples/*.cpp ../src
cd ..

# delete the incorrect files in src
rm src/*.c

# replace the common.mk with the correct one.
sed -i '/^INCLUDE=/ s@$@ -I$(ROOT)/include/ros_lib@' common.mk
11 changes: 11 additions & 0 deletions rosserial_vex_cortex/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
packages=['rosserial_vex_cortex'],
package_dir={'': 'src'},
)

setup(**d)
74 changes: 74 additions & 0 deletions rosserial_vex_cortex/src/ros_lib/CortexHardware.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2011, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote prducts derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _ROSSERIAL_VEX_CORTEX_CORTEX_HARDWARE_H_
#define _ROSSERIAL_VEX_CORTEX_CORTEX_HARDWARE_H_

#include "main.h"
#include "logger.h"

#define SERIAL_CLASS int

class CortexHardware {
public:
CortexHardware() {}

// any initialization code necessary to use the serial port
// note: the serial port initialization for rosserial for VEX Cortex must be implemented in `src/init.cpp`
// see that file for more information.
void init() {}

// read a byte from the serial port. -1 = failure
int read() {
if(fcount(stdin) != 0) {
int c = vexrosreadchar();
return c;
}
return -1;
}

// write data to the connection to ROS
void write(uint8_t* data, int length) {
for(int i = 0; i < length; i++) {
vexroswritechar(data[i]);
}
}

// returns milliseconds since start of program
unsigned long time() {
return millis();
}
};

#endif
30 changes: 30 additions & 0 deletions rosserial_vex_cortex/src/ros_lib/auto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** @file auto.c
* @brief File for autonomous code
*
* This file should contain the user autonomous() function and any functions related to it.
*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*
* PROS contains FreeRTOS (http://www.freertos.org) whose source code may be
* obtained from http://sourceforge.net/projects/freertos/files/ or on request.
*/

#include "main.h"

/*
* Runs the user autonomous code. This function will be started in its own task with the default
* priority and stack size whenever the robot is enabled via the Field Management System or the
* VEX Competition Switch in the autonomous mode. If the robot is disabled or communications is
* lost, the autonomous task will be stopped by the kernel. Re-enabling the robot will restart
* the task, not re-start it from where it left off.
*
* Code running in the autonomous task cannot access information from the VEX Joystick. However,
* the autonomous function can be invoked from another task if a VEX Competition Switch is not
* available, and it can access joystick information if called in this way.
*
* The autonomous task may exit, unlike operatorControl() which should never exit. If it does
* so, the robot will await a switch to another mode or disable/enable cycle.
*/
void autonomous() {
}
46 changes: 46 additions & 0 deletions rosserial_vex_cortex/src/ros_lib/examples/helloworld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* rosserial Publisher Example for VEX Cortex
* Prints "hello world!"
*
*
*
* Note: defining rosserial objects on the global scope will cause segmentation faults.
* put variables in functons.
*/

#include <ros.h>
#include <std_msgs/String.h>

// this loop is run in setup function, which publishes at 50hz.
inline void loop(ros::NodeHandle & nh, ros::Publisher & p, std_msgs::String & str_msg, char* msgdata)
{
str_msg.data = msgdata;
p.publish( &str_msg );
nh.spinOnce();
delay(20);
}

// The setup function will start a publisher on the topic "chatter" and begin publishing there.
inline void setup()
{
// debug logging
vexroslog("\n\n\n\n\r\t\tROSserial for VEX Cortex V2 - June 2018 - START\n\n\n\n\n\r");

// make a node handle object, string message, and publisher for that message.
ros::NodeHandle nh;
std_msgs::String str_msg;
ros::Publisher chatter("chatter\0", &str_msg);

// set up rosserial, and prepare to publish the chatter message
nh.initNode();
nh.advertise(chatter);

// message data variable.
char* msg = (char*) malloc(20 * sizeof(char));
while (1) {

// send a message about the time!
sprintf(msg, "[%d] Hello there!!", (int) millis());
loop(nh, chatter, str_msg, msg);
}
}
Loading