Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

Robotics Development Kit Blink LED Tutorial

$
0
0

Driving the GPIO Pins on the UP Board of the Intel® RealSense™ Robotics Development Kit to blink a LED circuit.

What you will learn

This tutorial sets out to demonstrate a basic usage of the GPIO pins by appealing to the classic electronic analogy for “Hello World”, which entails setting up the UP Board to make an external LED blink.

Introduction to the Robotics Development Kit UP Board

The Intel® RealSense™ Robotics Development Kit (RDK) is comprised of the Intel® RealSense™ R200 depth-camera (R200) and the Aaeon* UP Board. The computing heart of the system, the UP Board, is capable of connectivity to the outside world via a 40-pin input/output header as shown in Figure 1. The UP Board has a total of 28 independent GPIO pins that are provided on the I/O header. Ubuntu 14.04 has kernel platform drivers developed for the UP board to provide Linux GPIO pin numbering in the range 0-27, emulating that of the Raspberry Pi. 

Access to the hardware GPIOs for this tutorial will be done at a low level using Linux sysfs.  Sysfs provides a way in Linux for users (or code in the user-space) to interact with devices at the system (kernel) level.

This tutorial requires no special libraries since it uses the sysfs interface.

Important: Please note that the Linux GPIO numbers in Figure 1 are different from both the physical pin numbers and the UP Board pinout. The Linux GPIO numbers are assigned to match the Raspberry Pi BCM GPIO numbering scheme.

For comparison with Raspberry Pi layouts, visit: http://pinout.xyz

Lighting up a LED Using Your UP Board

Once you have configured the UP Board of the RDK according to the quickstart guide From Zero to Hero: Getting up and running with the Intel RealSense Robotic Development Kityou are ready for your first real project. We will light up a LED using the C programming language and the GPIO pins on your UP board.

What You Will Learn

  • You will construct a basic electrical circuit and attach it to your UP Board GPIO pins

What You Will Need

  • 1 - small LED, any color
  • 1 - 50 ohm resistor
  • Some small-gauge solid core wire
  • Breadboard or alligator clips, or both, to hold connections

Let There Be Light

Before we get around to writing any code, let's first get acquainted with the pin numbering of our UP Board and create a simple circuit. We'll start by simply lighting our led using the 3.3v pin and the ground pin on our UP Board. We will use the following schematic for our circuit:


Figure 2

Before starting, unplug your UP Board. You wouldn't want to risk shorting it out while working with it 'powered on', especially since this is our first project.

  • Using your assortment of materials, create the circuit on either your breadboard, or using your alligator clips.
  • Pin 1 (+3.3 volts) should go to the longer leg of your LED (anode). This pin provides a steady supply of 3.3v. Unlike the GPIO pins on your UP Board, this pin is not programmable, and cannot be controlLED by software.
  • Attach the shorter leg of the LED to the resistor. Finally, attach the other end of the resistor to Pin 6 (- ground) on your UP Board.

Double-check your connections. When you are done, your circuit should look like this:


Figure 3

Power on your UP Board - the LED should immediately turn on.

Controlling the LED with Code

Now that we've tested our basic circuit, it's time to move the positive lead from the 'always on' 3.3v pin to one of the programmable GPIO pins. Here is what our circuit will look like:


Figure 4

  • Power-off your UP Board again before making any changes to your wiring.
  • Move the positive lead from pin 1 to pin 7.

When you are done, your circuit should look like this:


Figure 5

Source to Drive Blink

Below is a code sample that will drive to blink the circuit you have built thus far.

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define IN   0
#define OUT  1

#define LOW  0
#define HIGH 1

#define PIN  24 /* physical pin 18 */
#define POUT 4  /* physical pin 7  */

static int GPIOExport(int pin);
static int GPIOUnexport(int pin);
static int GPIODirection(int pin, int dir);
static int GPIORead(int pin);
static int GPIOWrite(int pin, int value);

int main(int argc, char *argv[])
{
	int repeat = 9;

	/*
	 * Enable GPIO pins
	 */
	if (-1 == GPIOExport(POUT) || -1 == GPIOExport(PIN))
		return(1);

	/*
	 * Set GPIO directions
	 */
	if (-1 == GPIODirection(POUT, OUT) || -1 == GPIODirection(PIN, IN))
		return(2);

	do {
		/*
		 * Write GPIO value
		 */
		if (-1 == GPIOWrite(POUT, repeat % 2))
			return(3);

		/*
		 * Read GPIO value
		 */
		printf("I'm reading %d in GPIO %d\n", GPIORead(PIN), PIN);

		usleep(500 * 1000);
	}
	while (repeat--);

	/*
	 * Disable GPIO pins
	 */
	if (-1 == GPIOUnexport(POUT) || -1 == GPIOUnexport(PIN))
		return(4);

	return(0);
}

Int GPIOExport(int pin)
{
#define BUFFER_MAX 3
	char buffer[BUFFER_MAX];
	ssize_t bytes_written;
	int fd;

	fd = open("/sys/class/gpio/export", O_WRONLY);
	if (-1 == fd) {
		fprintf(stderr, "Failed to open export for writing!\n");
		return(-1);
	}

	bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
	write(fd, buffer, bytes_written);
	close(fd);
	return(0);
}

Int GPIOUnexport(int pin)
{
	char buffer[BUFFER_MAX];
	ssize_t bytes_written;
	int fd;

	fd = open("/sys/class/gpio/unexport", O_WRONLY);
	if (-1 == fd) {
		fprintf(stderr, "Failed to open unexport for writing!\n");
		return(-1);
	}

	bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
	write(fd, buffer, bytes_written);
	close(fd);
	return(0);
}

Int GPIODirection(int pin, int dir)
{
	static const char s_directions_str[]  = "in\0out";

#define DIRECTION_MAX 35
	char path[DIRECTION_MAX];
	int fd;

	snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
	fd = open(path, O_WRONLY);
	if (-1 == fd) {
		fprintf(stderr, "Failed to open gpio direction for writing!\n");
		return(-1);
	}

	if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) {
		fprintf(stderr, "Failed to set direction!\n");
		return(-1);
	}

	close(fd);
	return(0);
}

Int GPIORead(int pin)
{
#define VALUE_MAX 30
	char path[VALUE_MAX];
	char value_str[3];
	int fd;

	snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
	fd = open(path, O_RDONLY);
	if (-1 == fd) {
		fprintf(stderr, "Failed to open gpio value for reading!\n");
		return(-1);
	}

	if (-1 == read(fd, value_str, 3)) {
		fprintf(stderr, "Failed to read value!\n");
		return(-1);
	}

	close(fd);

	return(atoi(value_str));
}

Int GPIOWrite(int pin, int value)
{
	static const char s_values_str[] = "01";

	char path[VALUE_MAX];
	int fd;

	snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
	fd = open(path, O_WRONLY);
	if (-1 == fd) {
		fprintf(stderr, "Failed to open gpio value for writing!\n");
		return(-1);
	}

	if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
		fprintf(stderr, "Failed to write value!\n");
		return(-1);
	}

	close(fd);
	return(0);
}

If you have the source code into a file named blink.c you can compile it using the following command-line command:

gcc –Wall –o blink blink.c

Conclusion

This UP Board tutorial sets out to present a “Hello World” circuit that enables the UP Board to a C program using the Linux sysfs mechanism to drive a LED to blink on-off for 10 times.

The next tutorial in this series will drive the brightness of the LED by incorporating depth-data coming from the R200 camera and the Intel® RealSense™ Cross Platform API, and modifying both the circuit and the C program to accomplish this task.


Viewing all articles
Browse latest Browse all 3384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>