Difference between revisions of "Microcontroller And Electronics"

From veeOp Wiki
Jump to: navigation, search
Line 135: Line 135:
 
| align="center" | <math>(12,0,0,10)</math>  
 
| align="center" | <math>(12,0,0,10)</math>  
 
| align="center" | <math>(0,12,1,2)</math>
 
| align="center" | <math>(0,12,1,2)</math>
| Responds showing us the ignition last changed it's state 258 seconds ago. Note that <math>R_1</math> and <math>R_2</math> represent a 16 bit integer.<BR> In the response <math>(1,2)</math> could be represented as bytes in hex as <math>\text{(0x01,0x02)}</math>, which will convert as a little endian into a decimal value of <math>\text{0x102 hex = 258 dec}</math>
+
| Responds showing us the ignition last changed it's state 258 seconds ago. See 16-bit integer note, below.
 
|-
 
|-
 
|Last time ignition changed (minutes ago)  
 
|Last time ignition changed (minutes ago)  
Line 162: Line 162:
 
| align="center" | <math>(14,1,2,10)</math>  
 
| align="center" | <math>(14,1,2,10)</math>  
 
| align="center" | <math>(0,14,1,2)</math>
 
| align="center" | <math>(0,14,1,2)</math>
| Simply echos the sent bytes back. Returns <math>P_1</math> as <math>R_1</math> and returns <math>P_2</math> as <math>R_2</math>
+
| Simply echos the sent bytes back. Returns <math>P_1</math> as <math>R_1</math> and returns <math>P_2</math> as <math>R_2</math>. Mainly useful as
 
|}
 
|}
 +
 +
==== 16-bit Integer Example ====
 +
 +
Using the "Last time ignition changed (seconds ago)" command example from above.
 +
 +
Note that <math>R_1</math> and <math>R_2</math> represent a 16 bit integer.In the response <math>(1,2)</math> could be represented as bytes in hex as <math>\text{(0x01,0x02)}</math>, which will convert as a little endian into a decimal value of <math>\text{0x102 hex = 258 dec}</math>
  
 
=== Error Codes ===
 
=== Error Codes ===

Revision as of 13:19, 11 October 2013

Contents

Re-programming the on-board Arduino Compatible

The veeOP has an on-board arduino compatible, allowing you to customize it to the fullest extent.

The tool included with the downloadable distros here is the InoTool, a command-line Arduino tool. To get into the details, check out their installation guide, and their quick-start guide. But, we'll go over the details here, too.

In the distros provided here in the home directory for the "pi" user, you'll find a directory named 'arduino-vop' which already has an editable version of the code that's already on the microcontroller that you can use to get started!


Installing the necessary tools

If you're not using a veeOP customized distro, you might wish to install the tools from scratch. The examples given here are from PiDora (the Fedora distro for the pi), but, can be easily applied to most distros.

For example, where I say "yum install package_name" under a Debian-based distro you can use "apt-get install package_name".


Using Arduino Shields with your veeOP

The veeOP is designed to be able to extend it using Arduino Shields. There are shields for all kinds of applications

  • GPSr (global position system recievers)
  • GSM (cell phone service)
  • LCD Displays
  • Wireless (like the ZigBee / XBee)
  • And much, much more.

Are you new to Arduino?

Don't sweat it! The Arduino is intended to be an approachable technology, and one that gives a new user a lot of power. And, you'll feel great interacting directly with microcontrollers. The best part is, you can use your veeOP to help you learn.

You can log onto the veeOP connected to your raspberry pi, but, you might want to download the Arduino IDE (integrated development environment) and get started on your workstation (your laptop or desktop computer). You can compile the veeOP code right there to do some initial testing and exploration.

Some resources you might be interested in:

The I²C API

Main Article: I²C API

The I²C API on the microcontroller is a rather simple little set of commands that rides on I²C. For more information about I²C we direct you to Wikipedia.

In practice, this is also abstracted by the veeOP REST API, which resides on the Raspberry Pi. The REST API is much more verbose, and provides more flexibility in handling the day-to-day tasks with your veeOP. But, should you see an opportunity to extend it's communication -- it's a simple system. Don't let all the bits and bytes confuse you here, when it comes down to it it's this simple:

  1. Raspberry Pi says "Hi, microcontroller, I want you to do something."
    1. ...And sends it 4 bytes to tell it to do so.
  2. Microcontroller says "Sure! I did what you want, and here's some info about it."
    1. ...And it sends 4-bytes back to the Raspberry Pi.

The veeOP uses the Raspberry Pi as the I²C master, with the arduino-compatible being the slave, at address 4 (0x04) by default (this is configurable in the library.)

Download the code!

If you're using one of the downloadable pre-customized distros, you can look in:

/home/pi/arduino-vop/src/

For the source code. But, if you really want to dig in -- download the code from google code.

It's as simple as placing the vOP folder into your libraries folder in your Arduino's default install directory, to compile it. Check out the provided example sketch as it will guide you into extending the functionality. You can use the provided example sketch to keep the veeOP functionality intact, but! Use the microcontroller for other purposes, and it's extend it's functionality.

How Commands are transmitted

Commands are sent to the veeOP arduino-compatible with a 4-byte tuple. It looks like so:

(C,P_1,P_2,C_e)

Where each element is a single byte, representing:


    \begin{align}
      C & = \text{Command}                       \\
      P_1 & = \text{First Parameter}             \\
      P_2 & = \text{Second Parameter}            \\
      C_e & = \text{Command Ending (0x0A)} \\
    \end{align}


  • C (command) is any (known) value other than 10 (0x0A, in hex.) and originally assigned all higher 10.
  • P_1 & P_2 are sometimes treated as individual integer values each, or together as a 16 bit integer, depending on context.
  • C_e Command ending allows the microcontroller know when a command is incomplete, or too many bytes are sent.


Why's it end in 10? It's the "end of line" ASCII character, check it out asciitable.com.

How responses are received

When the microcontroller recieves the proper 4 bytes for the command, it responds in turn with a response of 4 bytes.

(E,C_r,R_1,R_2)

Where each element is a single byte, representing:


    \begin{align}
      E & = \text{Error Code}           \\
      C_r & = \text{Command return}     \\
      R_1 & = \text{First return byte}  \\
      R_2 & = \text{Second return byte} \\
    \end{align}


  • E errors of a value of 0 denotes an error-free response, any non-zero value is defined in a table below.
  • C_r is a redundant byte that represents C from the sent command. This is used for error handling on the raspberry pi.
  • R_1 & R_2 are the resulting responses from the command. Treated as two bytes, or a 16-bit unsigned integer depending on the context of the command.


Commands

In this chart, we show each available command, and it's return type. The example commands and responses are given showing each 4-byte command and response, in decimal.

Command Description Value P_1 P_2 Returns Example Command (Decimal) Example Response (decimal) Example Description
Check Ignition State 11 - - Boolean (11,0,0,10) (0,11,0,1) Response showing the ignition is on. An R_2 value of 1 denotes the ignition is on, where a value of 0 denotes it being off.
Last time ignition changed (seconds ago) 12 - - 16-bit Integer (12,0,0,10) (0,12,1,2) Responds showing us the ignition last changed it's state 258 seconds ago. See 16-bit integer note, below.
Last time ignition changed (minutes ago) 13 - - 16-bit Integer (13,0,0,10) (0,13,0,90) Responds showing us the ignition last changed it's state 90 minutes ago
Pat the watchdog 15 - - - (15,0,0,10) (0,15,0,0) Responds simply stating it pat the watchdog without an error.
Echo bytes sent 14 Byte Byte 2 Bytes (14,1,2,10) (0,14,1,2) Simply echos the sent bytes back. Returns P_1 as R_1 and returns P_2 as R_2. Mainly useful as

16-bit Integer Example

Using the "Last time ignition changed (seconds ago)" command example from above.

Note that R_1 and R_2 represent a 16 bit integer.In the response (1,2) could be represented as bytes in hex as \text{(0x01,0x02)}, which will convert as a little endian into a decimal value of \text{0x102 hex = 258 dec}

Error Codes

E value Code Constant Description
1 ERR_BUFFER_OVERFLOW Too many bytes received before C_e
2 ERR_COMMAND_UNKNOWN Command is unknown.
3 ERR_COMMAND_INCOMPLETE Request for data from master received before C_e sent.

Advanced Resources

Looking at the DTR and Serial RX/TX lines on a HP Logic Analyzer (from 1984!) on the ATMega while developing the veeOP

Other resources that were valuable as the interface between the veeOP board and Raspberry Pi for programming the onboard arduino-compatible: