Showing posts with label Unojoy. Show all posts
Showing posts with label Unojoy. Show all posts

2016/06/21

Interfacing a RC transmitter with a computer

This post is about how to interface a RC transmitter for plane / boat / car with a computer. The purpose of doing so is to use the RC transmitter to control the games / simulators running on the interfaced computer. For this post, I will be using DEVO 7E and RX601, but this post is applicable to any RC transmitter and receiver pair that works with Servo motor similar to the one shown below.

DEVO 7E Transmitter and RX601 Receiver
3-wire Servo 

How it works?

An Arduino Uno board is used to take in the PWM signals from the PWM outputs of the receiver. The PWM signals are then processed by the software running on the Arduino Uno board to come out with the control signals to be fed to the computer connected to the Arduino Uno board via a USB cable. The computer treats the input signals as from a connected joystick and the signals are used to control the games / simulators running on the computer.

The PWM waveforms

Before a program can be made to process the PWM signals, we need to know how they look. The photo below shows the division of the channels on the RX601 receiver.

The waveform of the PWM output of each channel on the receiver are shown below.










Code

#include "UnoJoy.h"

int tmp;
unsigned long time_now;

void setup(){

  setupPins();
  setupUnoJoy();
}

void loop(){
  // Always be getting fresh data

  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
}

void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the
  // two serial line pins

  for (int i = 2; i <= 12; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(A4, INPUT);
  digitalWrite(A4, HIGH);
  pinMode(A5, INPUT);
  digitalWrite(A5, HIGH);

  // RX601 Receiver CH1 Input,
  pinMode(A0, INPUT);
 
}

dataForController_t getControllerData(void){

  //  Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before

  dataForController_t controllerData = getBlankDataForController();

  //  Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  //  The section below is not used in our application.
  //---------------------------------------------------------------
  controllerData.triangleOn = !digitalRead(2);
  controllerData.circleOn = !digitalRead(3);
  controllerData.squareOn = !digitalRead(4);
  controllerData.crossOn = !digitalRead(5);
  controllerData.dpadUpOn = !digitalRead(6);
  controllerData.dpadDownOn = !digitalRead(7);
  controllerData.dpadLeftOn = !digitalRead(8);
  controllerData.dpadRightOn = !digitalRead(9);
  controllerData.l1On = !digitalRead(10);
  controllerData.r1On = !digitalRead(11);
  controllerData.selectOn = !digitalRead(12);
  controllerData.startOn = !digitalRead(A4);
  controllerData.homeOn = !digitalRead(A5);
  //--------------------------------------------------------------

  // Get the input from CH1 of RX601

  while (digitalRead(A0) == LOW) {}
  time_now = micros();
  while (digitalRead(A0) == HIGH) {}
  tmp = micros() - time_now;
  controllerData.leftStickX = tmp >> 2;
  //Serial.println(tmp);

  while (digitalRead(A1) == LOW) {}
  time_now = micros();
  while (digitalRead(A1) == HIGH) {}
  tmp = micros() - time_now;
  controllerData.leftStickY = tmp >> 2;
  //Serial.println(tmp);

  while (digitalRead(A2) == LOW) {}
  time_now = micros();
  while (digitalRead(A2) == HIGH) {}
  tmp = micros() - time_now;
  controllerData.rightStickX = tmp >> 2;
  //Serial.println(tmp);

  while (digitalRead(A3) == LOW) {}
  time_now = micros();
  while (digitalRead(A3) == HIGH) {}
  tmp = micros() - time_now;
  controllerData.rightStickY = tmp >> 2;
  //Serial.println(tmp);

  // And return the data!
  return controllerData;
}

Demo Video

Click here to play the demo video on Youtube.


For more about how to prepare Arduino Uno for the above application, please refer to the posts mentioned in the reference section below.

References

How to use Unojoy with Arduino Uno

How to turn Arduino Uno into HID Keyboard - Part 2 of 2

How to turn Arduino Uno into HID Keyboard - Part 1 of 2
http://wei48221.blogspot.tw/2016/06/arduino-uno-hid-keyboard-joystick-part.html

---------------------------------------------------------------------------------------------------------------------

Buying Info.

I've prepared a kit that contains one Arduino Uno board pre-configured and pre-loaded with the necessary software and accessories (see below photos for the included items). Please e-mail me if you want to order or have any questions.

Price: US$30 per kit;
Shipping: Free shipping for order over US$50 to most major countries (please contact me to see if free shipping is applicable to your location);
Tax and tariffs: To be paid by the buyer;
Item location: Taipei, Taiwan;
Items included:
- 1 x Arduino Uno board;
- 1 x USB cable;
- 6 x Jumping wires with connectors.


The photo below shows how to connect the RX601 with the Arduino Uno board.



2016/06/14

How to use Unojoy with Arduino Uno

This is a quick summary of how to use Unojoy with Arduino Uno.

Preparing the necessary software

1. Download the code from here.

Pick "UnoJoyWin-21Feb2013.zip" as it's the latest version for MS-Windows.




2. Unzip the downloaded file into a folder. In the folder, the key files are in the 3 folders highlighted below.

Note,

In the ATmega8u2Code\HexFiles folder, there are "UnoJoy.hex" and "Arduino-usbserial-uno.hex". Burning the "UnoJoy.hex" into the Atmega16U2 of an Arduino Uno board will turn the board into a HID Joystick. Burning the "Arduino-usbserial-uno.hex" into the Atmega16U2 of the same board will turn in back to an Arduino Uno board.

The procedure for burning "UnoJoy.hex" and "Arduino-usbserial-uno.hex" into the Atmega16U2 of an Arduino Uno board can be found in my previous post here.

The sample Arduino sketch and its library are located under the UnoJoyArduinoSample folder.

The program for verifying the code developed to take advantage of Unojoy is located under the UnoJoyProcessingVisualizer folder.

Compile and upload the sample sketch

Compile and upload the "UnoJoyArduinoSample.ino" sketch in the UnoJoyArduinoSample folder to the Arduino Uno board.

Verify the sketch

Launch the "UnoJoyProcessingVisualizer.exe" program in the UnoJoyProcessingVisualizer folder. A window similar to the one below will appear.


Plug one end of a wire into the GND and the other end into digital pin 2 ~ 12 and analog pin A4 ~ A5 to see the corresponding button lights up in the window.


For the joystick portion, please refer to the article - Interfacing a Joystick.

After checking the sketch and seeing everything is working as planned. It's time to turn the Arduino Uno board into a joystick.

Flashing the "UnoJoy.hex" into the ATMega16U2 of the Arduino Uno board

Please refer to the "Burning the firmware into atmega16u2" section of my previous post for detail instruction.

After burning the "UnoJoy.hex" and unplug and plug the USB cable, there is now a new device called "HID-compliant game controller" in Windows Device Manager.


Note, To upload sketch to the Arduino Uno board again, burn the "Arduino-usbserial-uno.hex" into the atmega16u2.

Reference

unojoy - GettingStarted.wiki
https://code.google.com/archive/p/unojoy/wikis/GettingStarted.wiki

Arduino UnoJoy gamepad (Windows, XBox emulation)
https://www.youtube.com/watch?v=GrO8ZmxbOyI

Arduino wireless gamepad (UnoJoy)
https://www.youtube.com/watch?v=cZc-23NjNHY

Interfacing a Joystick
https://www.arduino.cc/en/Tutorial/JoyStick

2016/06/13

How to turn Arduino Uno into HID Keyboard - Part 2 of 2

This post is the second part of How to turn Arduino Uno into HID Keyboard tutorial. The first part can be found here.

To test the HID Keyboard, I want to use the "Random Key/Random Delay" code from http://mitchtech.net/arduino-usb-hid-keyboard/ because of its simplicity and there is no wiring involved.

/* Arduino USB HID Keyboard Demo
 * Random Key/Random Delay
 */

uint8_t buf[8] = {
  0 }; /* Keyboard report buffer */

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  delay(200);
}

void loop()
{
  int randomChar = random(4, 130);
  long randomDelay = random(1000, 10000);

  delay(randomDelay);

  buf[2] = randomChar;  // Random character
  Serial.write(buf, 8); // Send keypress
  releaseKey();
}

void releaseKey()
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key
}



To use the code, follow the below procedure (assuming the ATMega16U2 has "Arduino-usbserial-uno.hex" in it):

1. Copy and past the above code into Arduino IDE;

2. Compile and upload the code to Arduino Uno;

3.. Follow the instruction in my previous post to burn the "Arduino-keyboard-0.3.hex" into ATMega16U2;

4. Unplug the USB cable and plug it back in;

5. Launch a word editing program (MS-Word, UltraEdit, etc.) and wait to see random character appears at random interval.

When you are done with the test, follow the instruction in my previous post to burn the "Arduino-usbserial-uno.hex" back into ATMega16U2 then unplug the USB cable and plug it back to make the board a normal Arduino Uno again.

Reference

Arduino USB HID Keyboard
http://mitchtech.net/arduino-usb-hid-keyboard/

2016/06/11

How to turn Arduino Uno into HID Keyboard - Part 1 of 2

This is the first of a two parts series. This part is to provide a brief overview of HID concept and how to prepare and turn an Arduino Uno into a HID device (in this case, a HID Keyboard).

The 2nd part will be dealing with the coding of an Arduino sketch to send keystrokes to the connected PC over the USB port and some other stuff. The 2nd part can be found here.

Brief Overview

The Arduino USB HID keyboard is implemented by sending a 8-byte long "Keyboard Report Buffer" via the USB that connects the PC and the Arduino.

The format of the Keyboard Report Buffer is as follow.


The format of the Modifier Keys (Byte 0) is as follow.



As for keycode 1 ~ 6, they can be found in the Keyboard Scan Codes in the reference section of this post.

Preparing the Arduino and the needed firmware and tools

Note, the instructions below are for Arduino Uno and MS-Windows 8.1.

1. Check the Arduino board for its version and whether the USB Serial chip used is supported by the DFU tool.

Mine is R3 (no need to modify it)

If yours is R1, please follow the instruction here to modify it.

This board is using MEGA16U2

MEGA16U2 is on the support list

2. Download and save the "Arduino-usbserial.hex" in a safe location. This firmware needs to be in atmega16u2 for burning sketch unto the Arduino board using Arduino IDE and USB cable.

Here is the download link.
https://dl.dropboxusercontent.com/u/1816557/Arduino-usbserial-uno.hex

Note,

I think there is a missing Arduino-usbserial.hex in at the below github site.

https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/firmwares/atmegaxxu2/arduino-usbserial

3. Download a DFU Programmer from http://www.atmel.com/tools/flip.aspx.

Since I already have Java Run Time installed on my PC, I downloaded the one without Java Run Time.


4. Download the Keyboard HID firmware "Arduino-keyboard-0.3.hex" from here and save it to a safe location. After this firmware is written into atmega16u2, the Arduino Uno board will be viewed as a USB keyboard when it's plugged into a PC.


Burning the firmware into atmega16u2

1. Put the atmega16u2 in DFU (Device Firmware Update) mode by briefly shorting the 2 leftmost ICSP headers next to the USB port.


2. Install the ATmega16U2 DFU driver.

Note, the driver needs to be installed for Flip to communicate with ATmega16U2 on the Arduino Uno board.

On the computer, the driver "atmel_usb_dfu.inf" can be found under C:\Program Files (x86)\Atmel\Flip 3.4.7\usb.

3. Launch Flip. Select "Device", then select ATmega16U2 from the list.


4. Left click on the "Select a Communication Medium", left click on USB, left click on Open. The USB:ON indication on the lower right hand corner indicates Flip is now connected to ATmega16U2.


5. Go to File, Load HEX File, and select the Hex file to be written into ATmega16U2.


In this case, I choose to burn the "Arduino-keyboard-0.3.hex".


6. Click on Run to erase the existing firmware (Earse), write the new firmware (Program), and verify the written firmware (Verify) in ATmega16U2.

7. Unplug the USB cable that connects the Arduino Uno and the PC then plug the cable back in again. Go to Device Manager and there is now a HID Keyboard Device under "Keyboard".


Below is the Device Manager screen when a normal Arduino Uno is plugged in. Note that there is no HID Keyboard Device under Keyboard and an Arduino Uno is shown under COM & LPT


To burn the USB Serial firmware back to ATmega16U2 (turn the HID Keyboard Device back to a normal Arduino Uno board), simply repeat step 1 to 7 above except step 5. In step 5, select the previous saved "Arduino-usbserial-uno.hex" instead of "Arduino-keyboard-0.3.hex".

After "Arduino-usbserial-uno.hex" is written into ATmega16U2, close Flip, remove USB cable then plug it back in. The Windows OS will detect it as a new device and look for its driver. Very soon it will recognize it as an Arduino Uno and uses the already installed driver to support it. After that, the board is ready for uploading sketch via the Arduino IDE again.

Note,

Remember to short the 2 leftmost ICSP headers next to the USB port before launching Flip.


After shorting the pins, there will be an Atmel USB Devices in the Device Manager and no HID Keyboard Device. The ATmega16U2 is now ready to be flashed with new firmware.


Reference

Keyboard Scan Codes

The tables below are from P53 ~ P59 of the "USB HID Usage Tables". This document can be downloaded at http://www.usb.org/developers/hidpage/Hut1_12v2.pdf.









DFU Related References

Updating the Atmega8U2 and 16U2 on an Uno or Mega2560 using DFU
https://www.arduino.cc/en/Hacking/DFUProgramming8U2

DFU programming the atmega16u2 on the Arduino UNO R3
http://bartruffle.blogspot.tw/2013/09/dfu-programming-atmega16u2-on-arduino.html

HID Keyboard Related References

List of hex keyboard scan codes and USB HID keyboard documentation
http://stackoverflow.com/questions/27075328/list-of-hex-keyboard-scan-codes-and-usb-hid-keyboard-documentation

HID USB Keyboard
http://stackoverflow.com/questions/24055687/hid-usb-keyboard

USB Device Class Definition for Human Interface Devices (HID)
http://www.usb.org/developers/hidpage/HID1_11.pdf

HID Keyboard Related Arduino Sketch References

Arduino USB HID Keyboard
http://mitchtech.net/arduino-usb-hid-keyboard/

Arduino UNO Keyboard HID part 2
http://hunt.net.nz/users/darran/weblog/faf5e/Arduino_UNO_Keyboard_HID_part_2.html

--------------------------------------------------------------------------------------------------------------------------

HID Joystick Related Arduino Sketch References

Arduino UNO Big Joystick HID firmware
http://hunt.net.nz/users/darran/weblog/15f92/Arduino_UNO_Big_Joystick_HID_firmware.html

unojoy - GettingStarted.wiki
https://code.google.com/archive/p/unojoy/wikis/GettingStarted.wiki

Arduino wireless gamepad (UnoJoy)
https://www.youtube.com/watch?v=cZc-23NjNHY

--------------------------------------------------------------------------------------------------------------------------

Misc. Info.

PPM / CPPM Receiver
http://www.deviationtx.com/forum/3-feedback-questions/1845-ppm-cppm-receiver

Arduino + 2 Servos + Thumbstick (joystick)
http://www.instructables.com/id/Arduino-2-Servos-Thumbstick-joystick/step3/Connecting-the-thumbstick-joystick/

Arduino thumbstick controller
http://www.instructables.com/id/Arduino-thumbstick-controller/

How to Restore the Arduino UNO R3 ATmega16U2 Firmware Using the Arduino IDE
http://www.instructables.com/id/How-to-Restore-the-Arduino-UNO-R3-ATmega16U2-Firmw/