Documentation/Introduction.md

Introduction

Rduino

This project attempts to establish a method of communication between the R programming language and Arduino. The ability to read from and write to the Arduino pins, control a servo, and generate a signal were, in particular, deemed esential. This functionality was implemented by taking advantage of the R serial package's ability to read from and write to device files, and programming the Arduino to expect commands of a particular format, as described in the "Usage" section. Depending on the command received, the Arduino would then respond at the hardware level. Because these commands form the core of general Arduino operation, this project allows the user to do things like collect data through the Arduino, perform calculations on the data in R, and then respond to the data on the Arduino, completely from within R.

Arduino

"An Arduino is an open-source electronic platform" based on a microcontroller and GPIO (General purpose input and output) pins 6. The pins allow the Arduino to connect to innumerable sensors and shields, further expanding its functionality. Its small form factor, low cost, and simplicity makes it popular for use cases ranging from smart home controllers to robotics to network monitoring. The Arduino platform consists of hardware that is used to interact with the real world, as well as a provided Integrated Development Environment (IDE) that is used to program the Arduino to respond to the real world as desired. It communicates with a computer through a USB connection, and can also be powered off of the USB if the computer can supply 5V. There is also a seperate charging port so that the Arduino can run independent of a computer, allowing it to employed in embedded projects.

Pins

Arduinos are commonly used to interface with electronics peripherals through the use of "pins", which are electrical connections to the actual microcontroller, and can generally be seperated into digital or analog 1. Digital pins can be set to output or read in two distinct values, HIGH or LOW, which can be thought of as 1 or 0. Analog pins can only be used to read in values, which can range between 0-1023 2. Analog output functionality can also be simulated on the digital pins that have PWM functionality, which stands for Pulse Width Modulation (These pins are denoted by a “~” next to the pin number on the board). Essentially, this technique allows the digital pins to appear to be steadily outputting some voltage between 0 and 5V by quickly switching the pin from LOW to HIGH, simulating analog output. This range is broken down into a scale of 0-255 3. The Arduino can be programmed to react to inputs from these pins, and respond to inputs by outputting through these pins. However, this functionality is disabled while the Arduino is actively sending a pulse.

Servos

There are two main types of servos, but both accept the same types of inputs. Standard servos recieve a "pulse", or a square wave that is at high voltage for a specified period of time, and then at a low voltage for around 20ms. The servo interprets the amount of time that the pulse spends at the high voltage as an angle, and rotates to that position. Continuous rotation servos use the pulse to set the speed and direction of rotation 7.

Device Files

Unix based operating systems view everything as files. This includes physical devices such as mice, keyboards, and Arduinos. This means that receiving data from the Arduino can be achieved by reading the device file, and sending data to the Arduino is accomplished by writing to the device file 5. These device files are all located in the directory /dev, and the exact file can be found in the Arduino IDE under tools > port. However, R cannot natively read from and write to device files, so the serial package must be used to allow for communication between R and Arduino. The operating system views the Arduino as a device file, so the same functionality of the serial monitor in the Arduino IDE can be achieved by reading from and writing to the associated device file. The Rduino project implements this by uploading code to the Arduino that waits for specific encoded commands and then responds appropriately upone receiving such a command. Thus, writing these encoded commands to the device file from R allows the Arduino to be controlled from within R. Additionally, some of the encoded commands writes data from the pins to the device file from the Arduino side, which can then be read in R. Essentially, devices files act as an intermediary for R and Arduino to interface.

Serial

Serial communication is simply the transmission of information one bit at a time. There are many different implementations of serial communication, but the Arduino employs an asynchronous version, which means that there is no previously established clock by which to anitcipate the sending and receiving of data 4. The Arduino interfaces with PC's through a USB (Universal Serial Bus) hardware connection, which allows the PC to send and receive data from the Arduino. Asynchronous serial communication is typically described in terms of four factors: Baud rate, parity bit, data bits, and stop bits. The baud rate defines the rate at which information is sent, the parity bit can validate whether the data bits have been sent correctly, the data bits contain the actual information that was sent, and the stop bits signal the end of the data package.

Serial Package

Rduino relies upon the serial package, which in turn relies upon R's built in Tcl/Tk engine. Tcl has the ability to interface with serial connections, so this allows R to use Tcl commands to communicate with the Arduino's serial connection. The serial connection is initialized using the "open" and "fconfigure" commands, while reading and writing is achieved with the "puts" and "gets" commands, respectively.



pdhoff/Rduino documentation built on May 29, 2019, 7:36 a.m.