You are playing your favorite game on your favorite platform. You are driving a car in the game, and as you encounter a sharp turn, you hit your brakes, and start drifting into the curve. While drifting, your joystick is continuously letting you know about it. This brings you the sensation of a fast car, cutting curves even closer. You eventually change the game, and now it's a shooter. Every time you pull the trigger, your joystick vibrates for just a slight second, and you have a sensation of a gun recoil. Now it's on for another game, this time it's a fighter, boxing for example, and any time you deliver a blow, or take a hit, your controller vibrates to let you know what's going on. These features are pretty awesome, right? But let's look at them from the perspective of an embedded engineer, and see just how exactly this stuff works.
What's is Haptic feedback?
What you are experiencing is called Haptic feeedback, which is basically a machine communicating with it's user by means of vibration patterns. Haptic feedback is done using what we call Haptic technology. There are two types of haptic feedback: kinesthetic and tactile. To simply explain the difference, let's take an example. Let's say you are holding your morning cup of coffee. The muscles, joints, and tendons in your finger constantly send data to your brain about how much did you stretch your fingers, how strong do they need to pull the cup towards your palm, and where exactly the cup is located in your hand. This is kinesthetic feedback, informing you about how much your muscles and joints are used in your activity, and where an object is located. Tactic feedback would be you feeling the temperature of the cup, its surface, the pressure you put on the mug etc.
Both tactile and kinesthetic feedback falls under what we call haptic feedback. Over the years technology has advanced, and so has haptic technology, from just delivering simple vibrations, to now bringing complex data to the end user.
There are numerous ways of how haptic technology has been interfaced to the end user. These include: joysticks, pencils, gloves and much more.
Working with the Haptic click
Our Haptic click features a set of modes: it can generate feedback based on audio input, pwm input, or by reading custom patterns of vibration. Source code and library for the click can be found on GitHub and Libstock.
https://github.com/MikroElektronika/Click_Haptic_DRV2605
Setting a desired mode is easy:
haptic_set_mode (HAPTIC_MODE_AUDIOVIBE); // Set Audio-to-Vibe mode
You can also send the device a desired vibration to output, for example:
haptic_hal_write8 (DRV2605_REG_WAVESEQ1, 0x5E); // write desired pattern haptic_start_motor(); // start the motor and fire the waveform
This writes a certain pattern which the device will read and output.
The device also has built in libraries with certain patterns of vibration, to use these libraries, all you need to do is call:
haptic_set_library (0x01); haptic_start_motor(); // start the motor and fire the waveforms
Now the haptic click will output the patterns built in these libraries.
Configuring and starting the haptic click
Configuring the Haptic click is easy:
First, make sure your i2c bus is initialized. Haptic click has one enable pin, map the pin and pull it high to enable the Haptic click to work. After that, set your desired mode, and you're ready!
sbit HAPTIC_EN at GPIOD_ODR.B13; // HAPTIC enable pin void main() { GPIO_Digital_Output(&GPIOD_ODR, _GPIO_PINMASK_13); // Set EN pin as output I2C1_Init_Advanced(400000, &_GPIO_MODULE_I2C1_PB67); // I2C1 initialization haptic_i2c_init(HAPTIC_I2C_ADDR); // initialize correct slave device address Delay_ms(200); // Delay (200 ms) HAPTIC_EN = 1; // pull EN pin high, enable haptic click haptic_set_mode (HAPMODE_INTERNAL); // custom waveforms are fired }
Example
The following example shows how the Haptic click works in audio-to-vibe mode:
#include "haptic_hw.h" #includesbit HAPTIC_EN at GPIOD_ODR.B13; // HAPTIC enable pin void main() { GPIO_Digital_Output(&GPIOD_ODR, _GPIO_PINMASK_13); // Set EN pin as output I2C1_Init_Advanced(400000, &_GPIO_MODULE_I2C1_PB67); // I2C1 initialization haptic_i2c_init(HAPTIC_I2C_ADDR); // initialize correct slave device address Delay_ms(200); // Delay (200 ms) HAPTIC_EN = 1; // Initialization for Audio input haptic_set_mode(HAPTIC_MODE_AUDIOVIBE); // Set Audio-to-Vibe mode set_audio_minimum_input(0xC0); // Minimum Input level (About 1.35V) set_input_to_analog (); // Select Input mode for Analog input while(1); }
The motor will vibrate depending on the bass line of your song!
Here's another example, this time firing custom vibration patterns:
#include "haptic_hw.h" #includesbit HAPTIC_EN at GPIOD_ODR.B13; // HAPTIC enable pin void main() { GPIO_Digital_Output(&GPIOD_ODR, _GPIO_PINMASK_13); // Set EN pin as output I2C1_Init_Advanced(400000, &_GPIO_MODULE_I2C1_PB67); // I2C1 initialization haptic_i2c_init(HAPTIC_I2C_ADDR); // initialize correct slave device address Delay_ms(200); // Delay (200 ms) HAPTIC_EN = 1; haptic_set_mode (HAPMODE_INTERNAL); // Set internal mode while (1) { if(button1()) { haptic_hal_write8(DRV2605_REG_WAVESEQ1, 0x02); //write desired pattern haptic_start_motor(); // start the motor and fire the waveform } else if(button2()) { haptic_hal_write8(DRV2605_REG_WAVESEQ1, 0x0B); // write desired pattern haptic_start_motor(); // start the motor and fire the waveform } else if(button3()) { haptic_hal_write8(DRV2605_REG_WAVESEQ1, 0x5E); // write desired pattern haptic_start_motor(); // start the motor and fire the waveform } else if(button4()) { haptic_hal_write8(DRV2605_REG_WAVESEQ1, 0x3A); // write desired pattern haptic_start_motor(); // start the motor and fire the waveform } } }
Summary
Haptic feedback is used in a wide variety of applications. From medical, gaming, to robotics and art. Whether you want revolutionize the gaming industry with a brand new joystick for virtual reality, make a medical tool for surgeons to practice on, or you need some haptic feedback off of the surface of Mars - Haptic click will help you develop your project fully! Be sure to check out the documentation for our library for the haptic click, so that you can use the click to it's full potential!
References
wikipedia.org "Haptic technology" wikipedia 2016
electronics.howstuffworks.com "Haptic technology" howstuffworks 2016