Difference between revisions of "Training Projects"

From Bike Wiki
Jump to navigation Jump to search
(ROS Arduino Project)
(ROS Arduino Project)
Line 19: Line 19:
  
 
Let's write some code. Start a new program in the Arduino IDE ("File > New"), and add the following lines to it, at the very top of the file, outside of the functions that are already there: <br>
 
Let's write some code. Start a new program in the Arduino IDE ("File > New"), and add the following lines to it, at the very top of the file, outside of the functions that are already there: <br>
<code>#include <ros.h></code><br>
+
<pre>#include <ros.h>
<code>#include <std_msgs/Bool.h></code><br>
+
#include <std_msgs/Bool.h>
  
<code>#define RED_LED 22</code><br>
+
#define RED_LED 22
<code>ros::NodeHandle_<ArduinoHardware, 1, 3, 500, 500> nh;</code><br>
+
ros::NodeHandle_<ArduinoHardware, 1, 3, 500, 500> nh;
  
<code>bool led_state = false; // Variable for whether the LED is on </code><br>
+
bool led_state = false; // Variable for whether the LED is on  
<code>void updateInstruction(const std_msgs::Bool&data) {</code><br>
+
void updateInstruction(const std_msgs::Bool&data) {
: <code>led_state = data.data;</code><br>
+
  led_state = data.data;
<code>}</code><br>
+
}
<code>ros::Subscriber<std_msgs::Bool> led_sub("/led", &updateInstruction);</code>
+
ros::Subscriber<std_msgs::Bool> led_sub("/led", &updateInstruction);</pre>
 +
 
 +
In the setup() function, paste this code: <br>
 +
<pre>nh.initNode();
 +
nh.subscribe(led_sub);
 +
Serial.begin(115200);
 +
pinMode(RED_LED, OUTPUT);</pre>
 +
 
 +
Finally, in the loop() function, paste this code:
 +
<pre>digitalWrite(RED_LED, led_state);
 +
ny.spinOnce();</pre>
 +
 
 +
Try compiling and uploading to the Due. It shouldn't do anything. Remember to shut off the wheels while uploading, otherwise they may spin. Now we need to write the code on the Pi that will send instructions via the <code>/led</code> topic.
 
==== 2. Rotate wheel as commanded by ROS topic ====
 
==== 2. Rotate wheel as commanded by ROS topic ====
  
 
=== GPS Project ===
 
=== GPS Project ===

Revision as of 04:18, 16 April 2019

These training projects are meant to familiarize new members with the theory behind the bike and the bike itself.

DC Motor Control Project

  • 1: Test DC Motor using Arduino

Reference Circuit: https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/overview

  • 2: One DOF Helicopter: Build a chopstick-fan that stays horizontal when disturbed

Reference: ECE 4760 Lab http://people.ece.cornell.edu/land/courses/ece4760/labs/f2018/lab4_helicopter.html

ROS Arduino Project

ROS stands for the Robotic Operating System, and despite the name is not an operating system. It provides a framework for programs in different languages running on different platforms to communicate. A running program is called a ROS node. Nodes can be started and stopped independently, and there must be a ROS core running (a special kind of node) for communication to occur.

One way nodes can communicate is via a ROS topic. A node will use a topic to publish messages, and other nodes may subscribe to the topic. A given topic's messages always have the same message type (i.e. data type). There are many possible data types; many will be familiar to you from previous programming languages.

For these programs, you will need the Arduino IDE. This open-source software allows you to write code and upload it to the board. Click here to download it.

1. Blink light as commanded by ROS topic

This program will allow an LED on the bike to be controlled by a ROS topic. It will subscribe to a topic called /led, and whenever a boolean value appears the program will set the state of the LED to that value. You may need a new library to compile ROS code. If #include <ros.h> compiles without an error, then you can skip this step. Otherwise, download the file "ros_lib.zip" in the bike drive folder. In the Arduino IDE, go to the menu item "Sketch > Include Library > Add .ZIP Library..." and navigate to where you downloaded "ros_lib.zip", and select it.

You may also need the library for the Due. Go to the "Tools > Board" submenu. If an entry called "Arduino Due (Programming Port)" appears all the way at the bottom, then you can skip this step. Otherwise, in the same submenu ("Tools > Board"), click on "Boards Manager..." at the top. Type "Due" into the search bar, and install the package that comes up. Now, from the "Tools > Board" menu, select "Arduino Due (Programming Port)", and plug the wire from the programming port on the Due into a USB port on your computer. You should now be able to upload code to the Due.

Let's write some code. Start a new program in the Arduino IDE ("File > New"), and add the following lines to it, at the very top of the file, outside of the functions that are already there:

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

#define RED_LED 22
ros::NodeHandle_<ArduinoHardware, 1, 3, 500, 500> nh;

bool led_state = false; // Variable for whether the LED is on 
void updateInstruction(const std_msgs::Bool&data) {
   led_state = data.data;
}
ros::Subscriber<std_msgs::Bool> led_sub("/led", &updateInstruction);

In the setup() function, paste this code:

nh.initNode();
nh.subscribe(led_sub);
Serial.begin(115200);
pinMode(RED_LED, OUTPUT);

Finally, in the loop() function, paste this code:

digitalWrite(RED_LED, led_state);
ny.spinOnce();

Try compiling and uploading to the Due. It shouldn't do anything. Remember to shut off the wheels while uploading, otherwise they may spin. Now we need to write the code on the Pi that will send instructions via the /led topic.

2. Rotate wheel as commanded by ROS topic

GPS Project