MAX32630FTHR as a VR Controller #MakeWithMaxim

Published by bowenfeng in the blog bowenfeng's blog. Views: 1392


upload_2017-6-13_4-6-31.png
Introduction

MAX32630FTHR is a really powerful board with quite a lot of onboard peripherals. Most noticeably are the built-in accelerometer and the dual-mode Bluetooth low energy chip which unlock the door to use this board as a VR controller without any additional components.
The most unfortunate thing is as of my project finished, there has been no mbed library support for the onboard PAN1326B dual-mode ble chip, so I have to use some alternative design as a workaround. But the overall design principle will not change which is collecting the accelerometer data, converting to x, y, z axises data, and then send to the Bluetooth central device, in my case is an Android phone, but could be any VR device supporting BLE joystick.

BOM
upload_2017-6-13_0-45-12.png

Arduino Pro mini 3v3 x1 ($9.95)
Tiny BLE x1 ($30.09)
MAX32630FTHR x1 ($28.05)


Schematics


upload_2017-6-13_0-47-45.png

Instructions
This project could be easier and simpler, but because the Bluetooth library is not available, a lot of complexities come from the data transportation between boards.
So the goal is to collect the data from BMI160 accelerometer/gyroscope chip, move them to a board with BLE support, and send out as 3-axis data to a VR device.
Lucky for me I have some experience on Tiny BLE, so I decided to use it as if it is the BLE chip on the MAX32630FTHR board. My first attempt was to use I2C protocol, letting the FTHR board act as the I2C master and Tiny BLE as the I2C slave. But it turned out the I2CSlave is not supported on either board. So I turned to use Serial protocol. This should allow me to use one wire to send data from FTHR board to Tiny BLE on a predefined baud frequency. But that attempt was also without luck. For some reason, the serial data wasn't sent out of FTHR board correctly. I guess this may because of some interference between the built-in USB serial circuit and the UART2 port I used as the data output pins.
Finally, I chose the current solution, using an Arduino Pro Mini as the man-in-middle, converting the incoming I2C data from FTHR board, and send out through TX pin as serial data to Tiny BLE.
More details can be found in my source code listed below. But there are some notable considerations worth mentioning:
  1. FTHR board doesn't work well when sending data using serial protocol, but I2C works perfectly on the board.
  2. mbed I2C use 8-bit address, so the slave address must be shift one bit left before used in the API.
  3. I have to use a slightly lower frequency (10KHz) than the mbed default I2C frequency (100KHz) to allow the much slower Arduino Pro mini being able to recognize the signal.
  4. I have to use a much higher baud (115200) to send out the serial data otherwise the income data will overflow the output.
  5. The 3-axis data are uncalibrated, so you can see I found some difficulties while using it in the VR game. Further improvement will be done to provide a much better user experience.
The final project consists of two part, the VR game and the BLE controller.
The VR game is based on Unity engine. I modified the Roll a Ball tutorial by adding Google VR support. Here I choose the cardboard VR, not the daydream since I'll provide my own VR controller and that will based on FTHR board! By simply following the Google VR Unity support document and we added the VR support to a 3D game. We can now choose Android as the build target, compile our game and run on my Android phone. You can find the full project source code in the Source Code section.
The BLE controller is done by collecting the data from FTHR board, sending through I2C to Pro mini, then passing to Tiny BLE to be exposed as joystick input data. The reason that I need a Pro mini in middle was because neither board can act as I2C slave, and for some reason serial communication did not work.

UPDATE: MAXIM support team just notify me that their engineer team just released the BLE support library for FTHR board. My project will become much simpler by remove the other two boards!
https://developer.mbed.org/teams/MaximIntegrated/code/FTHR_BLE_Demo/

upload_2017-6-13_0-46-38.png

Video


Source Code

Roll A Ball VR Game source code (Unity project)
https://drive.google.com/file/d/0B-FHQj0xfENBcklnOXNqZHRVMk0/view?usp=sharing


MAX32630FTHR code
https://developer.mbed.org/users/bowenfeng/code/MAX32630FTHR_JOYSTICK/
Code (Text):
  1.  
  2. #include "mbed.h"
  3. #include "max32630fthr.h"
  4. #include "bmi160.h"
  5.  
  6. #define LOG(...) { printf(__VA_ARGS__); }
  7.  
  8. int main() {
  9.     DigitalOut rLED(LED1, LED_OFF);
  10.     DigitalOut gLED(LED2, LED_OFF);
  11.     DigitalOut bLED(LED3, LED_OFF);
  12.  
  13.     I2C i2cBus(P5_7, P6_0);
  14.     i2cBus.frequency(400000);
  15.     BMI160_I2C imu(i2cBus, BMI160_I2C::I2C_ADRS_SDO_LO);
  16.  
  17.     I2C dataOut(P3_4, P3_5);
  18.     dataOut.frequency(10000);
  19.  
  20.     uint32_t failures = 0;
  21.  
  22.     MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
  23.  
  24.     if(imu.setSensorPowerMode(BMI160::GYRO, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) {
  25.         LOG("Failed to set gyroscope power mode\n");
  26.         failures++;
  27.     }
  28.     wait_ms(100);
  29.  
  30.     if(imu.setSensorPowerMode(BMI160::ACC, BMI160::NORMAL) != BMI160::RTN_NO_ERROR) {
  31.         LOG("Failed to set accelerometer power mode\n");
  32.         failures++;
  33.     }
  34.     wait_ms(100);
  35.  
  36.     BMI160::AccConfig accConfig;
  37.     //example of using getSensorConfig
  38.     if(imu.getSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) {
  39.         LOG("ACC Range = %d\n", accConfig.range);
  40.         LOG("ACC UnderSampling = %d\n", accConfig.us);
  41.         LOG("ACC BandWidthParam = %d\n", accConfig.bwp);
  42.         LOG("ACC OutputDataRate = %d\n\n", accConfig.odr);
  43.     } else {
  44.         LOG("Failed to get accelerometer configuration\n");
  45.         failures++;
  46.     }
  47.  
  48.     //example of setting user defined configuration
  49.     accConfig.range = BMI160::SENS_4G;
  50.     accConfig.us = BMI160::ACC_US_OFF;
  51.     accConfig.bwp = BMI160::ACC_BWP_2;
  52.     accConfig.odr = BMI160::ACC_ODR_8;
  53.     if(imu.setSensorConfig(accConfig) == BMI160::RTN_NO_ERROR) {
  54.         LOG("ACC Range = %d\n", accConfig.range);
  55.         LOG("ACC UnderSampling = %d\n", accConfig.us);
  56.         LOG("ACC BandWidthParam = %d\n", accConfig.bwp);
  57.         LOG("ACC OutputDataRate = %d\n\n", accConfig.odr);
  58.     } else {
  59.         LOG("Failed to set accelerometer configuration\n");
  60.         failures++;
  61.     }
  62.  
  63.     BMI160::GyroConfig gyroConfig;
  64.     if(imu.getSensorConfig(gyroConfig) == BMI160::RTN_NO_ERROR) {
  65.         LOG("GYRO Range = %d\n", gyroConfig.range);
  66.         LOG("GYRO BandWidthParam = %d\n", gyroConfig.bwp);
  67.         LOG("GYRO OutputDataRate = %d\n\n", gyroConfig.odr);
  68.     } else {
  69.         LOG("Failed to get gyroscope configuration\n");
  70.         failures++;
  71.     }
  72.  
  73.     wait(1.0);
  74.  
  75.     if(failures == 0) {
  76.         float imuTemperature;
  77.         BMI160::SensorData accData;
  78.         BMI160::SensorData gyroData;
  79.         BMI160::SensorTime sensorTime;
  80.  
  81.         char buffer[256] = {0};
  82.  
  83.         while(1) {
  84.             imu.getGyroAccXYZandSensorTime(accData, gyroData, sensorTime, accConfig.range, gyroConfig.range);
  85.             imu.getTemperature(&imuTemperature);
  86.  
  87.             sprintf(buffer, "%d,%d,%d\n", accData.xAxis.raw, accData.yAxis.raw, accData.zAxis.raw);
  88.             LOG(buffer);
  89.             dataOut.write(0x8<<1, buffer, strlen(buffer) + 1);
  90.  
  91.             gLED = !gLED;
  92.         }
  93.     } else {
  94.         while(1) {
  95.             rLED = !rLED;
  96.             wait(0.25);
  97.         }
  98.     }
  99. }
  100.  
  101.  
Arduino Pro mini code
Code (Text):
  1.  
  2. #include <Wire.h>
  3.  
  4. void setup() {
  5.   Serial.begin(115200);
  6.   Wire.onReceive(receiveData);
  7.   Wire.begin(0x8);
  8.  
  9.   delay(2);
  10.  
  11.   Serial.println("Logger Started.");
  12. }
  13.  
  14. void loop() {
  15.   delay(5);
  16. }
  17.  
  18. void receiveData(int cnt) {
  19.   while (Wire.available() > 0) {
  20.     char c = Wire.read();
  21.     Serial.print(c);
  22.   }
  23. }
  24.  
Tiny BLE code
https://developer.mbed.org/users/bowenfeng/code/Seeed_Tiny_BLE_FTHR_Peripheral/
Code (Text):
  1.  
  2. #include "mbed.h"
  3. #include "nrf51.h"
  4. #include "nrf51_bitfields.h"
  5.  
  6. #include "ble/BLE.h"
  7. #include "JoystickService.h"
  8. #include "DFUService.h"
  9. #include "UARTService.h"
  10.  
  11. #include "examples_common.h"
  12.  
  13. #define LOG(...)    { pc.printf(__VA_ARGS__); }
  14.  
  15. #define LED_GREEN   p21
  16. #define LED_RED     p22
  17. #define LED_BLUE    p23
  18.  
  19. #define LED_OFF 1
  20. #define LED_ON  0
  21.  
  22. #define UART_TX     p9
  23. #define UART_RX     p11
  24. #define UART_CTS    p8
  25. #define UART_RTS    p10
  26.  
  27. #define DATA_TX p3
  28. #define DATA_RX p6
  29.  
  30. DigitalOut blue(LED_BLUE);
  31. DigitalOut green(LED_GREEN);
  32. DigitalOut red(LED_RED);
  33.  
  34. Serial pc(UART_TX, UART_RX);
  35. Serial data(DATA_TX, DATA_RX);
  36.  
  37. BLE  ble;
  38. JoystickService *joystickServicePtr;
  39. static const char DEVICE_NAME[] = "BunnyJoystick";
  40. static const char SHORT_DEVICE_NAME[] = "joystick0";
  41.  
  42. volatile bool bleIsConnected = false;
  43. volatile uint8_t tick_event = 0;
  44.  
  45. static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) {
  46.     LOG("disconnected\r\n");
  47.     bleIsConnected = false;
  48.     red = LED_OFF;
  49.     blue = LED_OFF;
  50.     green = LED_ON;
  51.     ble.gap().startAdvertising(); // restart advertising
  52. }
  53.  
  54.  
  55. static void onConnect(const Gap::ConnectionCallbackParams_t *params) {
  56.     LOG("connected\r\n");
  57.     bleIsConnected = true;
  58.     red = LED_OFF;
  59.     blue = LED_ON;
  60.     green = LED_OFF;
  61. }
  62.  
  63. static void waiting() {
  64.     if (!joystickServicePtr->isConnected()) {
  65.         green = !green;
  66.     } else {
  67.         blue = !blue;
  68.     }
  69. }
  70.  
  71.  
  72. int main() {
  73.     blue = LED_OFF;
  74.     green = LED_OFF;
  75.     red = LED_OFF;
  76.  
  77.     data.baud(115200);
  78.  
  79.     wait(4);
  80.     LOG("Bunny Joystick started.\n");
  81.  
  82.     LOG("initialising ticker\r\n");
  83.     Ticker heartbeat;
  84.     heartbeat.attach(waiting, 1);
  85.  
  86.     LOG("initialising ble\r\n");
  87.     ble.init();
  88.  
  89.     ble.gap().onDisconnection(onDisconnect);
  90.     ble.gap().onConnection(onConnect);
  91.  
  92.     initializeSecurity(ble);
  93.  
  94.     LOG("adding hid service\r\n");
  95.     JoystickService joystickService(ble);
  96.     joystickServicePtr = &joystickService;
  97.  
  98.     LOG("adding dev info and battery service\r\n");
  99.     initializeHOGP(ble);
  100.  
  101.     LOG("setting up gap\r\n");
  102.     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::JOYSTICK);
  103.     ble.gap().accumulateAdvertisingPayload(
  104.         GapAdvertisingData::COMPLETE_LOCAL_NAME,
  105.         (const uint8_t *)DEVICE_NAME,
  106.         sizeof(DEVICE_NAME));
  107.     ble.gap().accumulateAdvertisingPayload(
  108.         GapAdvertisingData::SHORTENED_LOCAL_NAME,
  109.         (const uint8_t *)SHORT_DEVICE_NAME,
  110.         sizeof(SHORT_DEVICE_NAME));
  111.     ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME);
  112.  
  113.     LOG("advertising\r\n");
  114.     ble.gap().startAdvertising();
  115.  
  116.     int xraw, yraw, zraw;
  117.  
  118.     while (true) {
  119.         if (data.readable()) {
  120.             char buffer[256] = {0};
  121.             char c;
  122.             int i = 0;
  123.             while(data.readable() && i < 256) {
  124.                 buffer[i++] = c = data.getc();
  125.                 if (c == '\n') {
  126.                     buffer[i] = 0;
  127.                     break;
  128.                 }
  129.             }
  130.             LOG("Received data from FTHR:\n");
  131.             LOG(buffer);
  132.             sscanf(buffer, "%d,%d,%d", &xraw, &yraw, &zraw);
  133.             LOG("%d,%d,%d", xraw, yraw, zraw);
  134.             joystickServicePtr->setSpeed(xraw, yraw, zraw);
  135.  
  136.             wait(0.5);
  137.         }
  138.  
  139.         ble.waitForEvent();
  140.     }
  141. }
  142.  
  143.  
You need to be logged in to comment