Training Projects

From Bike Wiki
Revision as of 14:19, 16 April 2019 by Keely (talk | contribs) (ROS Arduino Project)
Jump to navigation Jump to search

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.

Part I: 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);

Let's go over this code. The #include line includes other source files into the current source file. For header files and standard files, use #include <file_name> and for user defined files, use #include "file_name". The #define directive creates a macro, which is the association of an identifier or parameterized identifier with a token string. The compiler will then substitute the token string for each occurrence of the identifier in the source file. In this code, we are substituting every instance of RED_LED with 22.

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);
nh.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.

Part II: Rotate wheel as commanded by ROS topic

GPS Project