Wednesday, April 26, 2017
Home » Flowstone Workshop » Flowstone Workshop 12

Flowstone Workshop 12

Flowstone-HeaderHuman Interface Devices

FLOWSTONE WORKSHOP 12-2Welcome to the FlowStone Workshop number 12, where we give a beginner’s guide to computer programming using the FlowStone graphical programming language inside FlowBotics Studio.

FLOWSTONE WORKSHOP 12-1In this issue, we are going to look at the USB phenomena called HID (Human Interface Device). HID is been slowly invading our USB ports and is now one of the most used ways to communicate between computer hardware peripherals and the PC. A few examples you may have heard of are keyboards, mice and game controllers. One of the benefits of HID is that it requires no USB drivers to be installed, as HID is part of Windows.

So what can we do with HID in robotics and what’s available for us to use? Almost all game controllers are HID, so things like the PS2 controllers, Xbox controllers, wheels, pedals and joysticks are all HID, so these can be used to control our robots using FlowStone. In addition, there are many USB HID-compatible microchips now on the market, such as Arduino, PIC chips, etc. These have spurred on a wave of low-cost of development boards available for you to program your own code into the chip to interface with your hardware. While we will show you how to connect HID game controllers in FlowStone, the main area of interest for this article is the interfacing of off-the-shelf HID-compatible microchip boards. Here are a few common HID devices:

Game controllers:

Most games controller including: joysticks, Xbox controller, PS2 controller, etc.

Microprocessor boards:

Microchip – PIC18, PIC24, PIC32, dsPIC

Arduino – Uno, Mega, Teensy or Leonardo

Adafruit – Trinket

FLOWSTONE WORKSHOP 12-3
Adafruit Trinket HID usb board.

THE HID PROTOCOL

Simplistically, HID is typically programmed as 64 bytes of Hex (Hexadecimal) data in and out at around 100 times a second (100Hz). Put another way, it is a string of up to 64 characters sent to the hardware device and a string of up to 64 characters returned from the device. Some devices, like joysticks, only send out data to the PC so you don’t need to send anything to these devices.

A typical String will look like this:

0080FF7F0080FF7F008000000000

Fortunately, FlowStone has a pre-made HID module that does the hard part so you can just use any HID device without the need for low level programming.

CONNECTING A GAME CONTROLLER

In this example, we have connected a USB Xbox controller to the PC and you can see from the FlowStone code that the HID module has found three devices connected.

FLOWSTONE WORKSHOP 12-4
An Xbox Controller is an HID USB Device.

Clearly the Xbox is device 0 in the list, so we have set zero as the device “Idx” to connect to. Once connected, the Xbox data is just output 100 times a second from the “Data In” port of the HID module. This string is just a string of Hex that will change as you move the Xbox controls.

In order to make sense of this, you will need to decode the Hex into decimal or binary so that you can use it to control something in the FlowStone software.

Here’s an example for the four Xbox buttons A, B, X & Y:

FLOWSTONE WORKSHOP 12-5
Xbox visible as an HID Controller
FLOWSTONE WORKSHOP 12-8
Xbox buttons A,B,X,Y.

Nothing Pressed: 0080FF7F0080FF7F008000000000

Butt on A: 0080FF7F0080FF7F008001000000

Butt on B: 0080FF7F0080FF7F008002000000

Butt on X: 0080FF7F0080FF7F008004000000

Butt on Y: 0080FF7F0080FF7F008008000000

The observant among you will have noticed that Byte 11 has changed from 00,01,02,04,08

So in order to decode this, we just need to convert this byte into its binary bits:

01 = 0001, 02 = 0010, 03 = 0100, 04 = 1000 (Hex to Binary).

In FlowStone this would look like this:

FLOWSTONE WORKSHOP 12-9
Decoding the HID Hex String.

So here, we are splitting the long String onto its individual Bytes, then converting Byte 11 into Binary and then splitting this into its individual bits and using these to turn on LEDs corresponding to the controller button pressed. For good measure, we also added a display Meter connected to Byte 4 (Xbox left Joystick Y), and converted the Hex to decimal so that we could show the Joystick position. In FlowStone, there are already complete modules dedicated to the Xbox or PS2 controller in devices that do all of the decoding shown for you. However, it’s a good example showing how you can connect any HID device and decode the inputs.

HID AND MICROPROCESSORS

As I said in the introduction, the really interesting part of using HID is interfacing to microprocessor chips like Arduino and PIC chips to give you external ins and outs for your PC.

For this article, we are going to focus on the “START USB” development board from MikroElektronika. At only $19USD, this represents excellent value for money and is very easy to program using the MikroC, MikroBasic or MikroPascal programming languages.

FLOWSTONE WORKSHOP 12-6
The START USB PIC HID Board

The START USB board has a PIC18F2550 PIC chip on board and can be directly programmed via the USB port, so no additional programming hardware is needed. It also has a prototyping area so you can easily add your own hardware to make a complete prototype product to interface to your robot. This chip has the usual array of ins and outs, 10 x 10 bit analog ins, digital IO, UART, SPI, I2C, timers, etc., making it ideal as a high-speed data acquisition board (DAQ). There is even a version of this board called “PIC Clicker” that also has the pins to accept any of the “Click” boards, which are things such as sensors, displays, GPS, MP3 audio, SD cards and accelerometers.

FLOWSTONE WORKSHOP 12-10

We used MikroC Pro for our programming language; ANSI C is the language of choice for industry and will serve you well as you grow into more sophisticated projects. What’s great with MikroC is that it has a professional IDE (Integrated Development Environment) and is much easier to get going than Arduino in our opinion (you get what you pay for). There are literally hundreds of pre-made libraries that you can access, saving you hours of time and the range of PIC chips is huge.

OUR FIRST MICROPROCESSOR HID PROJECT!

The equivalent of a “Hello World” program using HID in a Microprocessor is an “HID Echo” program that just echoes back the data string you send to the board. This proves that the board is connected and working. There is an HID example already available for this in the MikroC example and it’s very simple!

char cnt;

char kk;

char readbuff [64];

char writebuff [64];

void main(void){

HID_Enable(&readbuff ,&writebuff );

while(1){

USB_Polling_Proc();

kk = HID_Read();

if(kk != 0){

for(cnt=0;cnt<64;cnt++)

writebuff [cnt]=readbuff [cnt];

HID_Write(&writebuff ,64);

}

}

}

FLOWSTONE WORKSHOP 12-13
HID Terminal used to make your HID descriptor file.

The final step is to make your own ID file for the board so that it will come up with your name for the board in Windows. There is an app for this in the tools of the MikroC called HID Terminal, use this to create a descriptor file for your board:

Compile the HID example C code and download it to your START USB board using the HID Bootloader app from the tools:

Now in FlowStone, the START USB board can be seen in the HID devices list as Robot Magazine HID from DSPRobotics:

FLOWSTONE WORKSHOP 12-11
USB Bootloader used to program chip.

To test it, we added a slider with a range of 0-255, converted it to Hex and sent it out of the HID module to the START USB board with our ID. The board then processes the data and returns the value back which is then converted back to decimal and sent to a meter for display. It works!

MAKING A USB DAQ ROBOTICS INTERFACE

Now that we have our basic HID Echo test program working, we can add some more functionality to the microprocessor code to interface with our robotics hardware. Even this simple, low-cost board packs some powerful features. There are three ports available on the chip A,B & C offering 20 programmable ins and outs. These can be inputs, outputs or analog ins, plus some special features like a UART for connecting with RS232 type devices and SPI for connecting to displays. The chip runs at up to 48MHz and can perform up to 12 MIPS, making some signal processing possible too.

FLOWSTONE WORKSHOP 12-12
Testing the HID Interface.

CONCLUSION

This is only the very beginning of using HID to connect to your robots or, in fact, another kind of electronics project. Using microprocessor chips, you can do the highspeed external processing necessary for robotics, control motors, servos, read sensors, monitor GPS, and accelerometers. There are literally hundreds of microprocessor chips available, from simple 8-bit devices up to super-powerful 32-bit chips running at 100MHz. All of them will interface to your PC for free (no drivers) using HID and FlowStone. You can then use FlowStone to display the real-time data and become the user interface for your robot.

Links
DSPRobotics, dsprobotcs.com
FlowBotics Studio, flowbotics.com
MikroElektronika, mikroe.com

Leave a Reply

Your email address will not be published. Required fields are marked *