![](/media/cache/profiles/6f0cdeedf664c24860cba8842e94b300.50x50_q85.jpg)
This project aims at hacking a remote controlled car by replacing the tx/rx and navigation systems. This project implements the pc side.
Dependencies: mbed-rtos mbed ssWi
For the related information, consult the wiki documentation at:
Import programrover_car
This project aims at hacking a remote controlled car by replacing the tx/rx and navigation systems. This project implements the car side.
Diff: joystick/joystick.cpp
- Revision:
- 0:1b4c3bbaf102
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/joystick/joystick.cpp Tue Mar 12 07:39:13 2013 +0000 @@ -0,0 +1,65 @@ +#include "joystick.hpp" + + +void Joystick::tuneChannel (int nOfSamples, int channel) +{ + if (channel<0 || channel>3) + return; + ranges[channel].maxV = std::numeric_limits<float>::min(); + ranges[channel].minV = std::numeric_limits<float>::max(); + for (int i=0; i<nOfSamples; i++) { + float val = channels[channel]->read(); + if (val>ranges[channel].maxV) + ranges[channel].maxV = val; + else if (val<ranges[channel].minV) + ranges[channel].minV = val; + wait(0.1); + } +} + +void Joystick::tuneCentring (int nOfSamples, int channel) +{ + if (channel<0 || channel>3) + return; + ranges[channel].center = 0.0; + for (int i=0; i<nOfSamples; i++) { + float val = channels[channel]->read(); + ranges[channel].center += val; + wait(0.1); + } + ranges[channel].center /= nOfSamples; +} + + + +float Joystick::read(int channel) +{ + if (channel<0 || channel>3) + return std::numeric_limits<float>::min(); + mutexes[channel].lock(); + float val = values[channel]; + mutexes[channel].unlock(); + return val; +} + + +void Joystick::update (int channel) +{ + mutexes[channel].lock(); + float val = channels[channel]->read(); + mutexes[channel].unlock(); + if (val>ranges[channel].center) + values[channel] = -(val-ranges[channel].center)/ + (ranges[channel].maxV-ranges[channel].center); + else + values[channel] = (ranges[channel].center-val)/ + (ranges[channel].center-ranges[channel].minV); +} + +void Joystick::update () +{ + update(JOYSTICK_SX_X); + update(JOYSTICK_SX_Y); + update(JOYSTICK_DX_X); + update(JOYSTICK_DX_Y); +}