Library to handle SpaceBall, SpaceMouse and SpaceOrb on serial port. Gets access to 3D rotation and translation vector as well as button status. (USB is not supported)

Dependents:   SpaceBall_Example

Library to handle SpaceBall, SpaceMouse and SpaceOrb on serial port. Gets access to 3D rotation and translation vector as well as button status. (USB is not supported)

All handling and decoding is done in the RX interrupt and the vector values can be read out asynchronously with different coordinate mappings.

Example:

#include "mbed.h"
#include "SpaceBall.h"
 
PwmOut led[] = {(LED1), (LED2), (LED3), (LED4) };
SpaceBall SBall(p9, p10);   // tx, rx, bSOrb
 
int main() {
    SBall.Init();
    
    while(1) {
 
        led[0] = abs( SBall[TX] ) + abs( SBall[TY] ) + abs( SBall[TZ] );
        led[1] = abs( SBall[RX] );
        led[2] = abs( SBall[RY] );
        led[3] = abs( SBall[RZ] );
        
        wait_us(500);
    }
}

In this exaple the 4 LEDs are powered dependent on force at the Spaceball. LED1 shows the sum of all translation forces. LED2 to LED4 shows the rotation forces.

For more information about SpaceBall devices see manufactorers page http://www.3dconnexion.com

For connecting a SpaceBall (or SpaceMouse or SpaceOrb) to mbed see page wiki/Serial-Connection

Example: SpaceBall 4000

/media/uploads/jocis/spaceball1.jpg

Committer:
jocis
Date:
Wed Sep 03 07:36:43 2014 +0000
Revision:
4:f953792e45cb
Parent:
3:7bacae57a30b
Added documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jocis 0:f67a8fffd94a 1 #include "SpaceBall.h"
jocis 0:f67a8fffd94a 2
jocis 0:f67a8fffd94a 3 #define Sleep(x) wait_ms(x*10)
jocis 3:7bacae57a30b 4 //extern Serial pc;
jocis 0:f67a8fffd94a 5
jocis 0:f67a8fffd94a 6 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 7
jocis 0:f67a8fffd94a 8 SpaceBall::SpaceBall ( PinName tx, PinName rx, bool bSpaceOrb ) : _serial ( tx, rx )
jocis 0:f67a8fffd94a 9 {
jocis 0:f67a8fffd94a 10 _bSpaceOrb = bSpaceOrb;
jocis 0:f67a8fffd94a 11 _serial.baud ( 9600 );
jocis 0:f67a8fffd94a 12
jocis 0:f67a8fffd94a 13 _fScale = 1.0f;
jocis 0:f67a8fffd94a 14
jocis 2:a7c0fcd157f7 15 _axis[0] = 0; /* last translational data received */
jocis 2:a7c0fcd157f7 16 _axis[1] = 0;
jocis 2:a7c0fcd157f7 17 _axis[2] = 0;
jocis 2:a7c0fcd157f7 18 _axis[3] = 0; /* last rotational data received */
jocis 2:a7c0fcd157f7 19 _axis[4] = 0;
jocis 2:a7c0fcd157f7 20 _axis[5] = 0;
jocis 2:a7c0fcd157f7 21 _buttons = 0; /* current button status */
jocis 0:f67a8fffd94a 22
jocis 1:e6282b645d9b 23 _data[0] = 0;
jocis 0:f67a8fffd94a 24 m_erroroccured = 0; /* if set, we've received an error packet or packets */
jocis 0:f67a8fffd94a 25 m_resetoccured = 0; /* if set, ball was reset, so have to reinitialize it */
jocis 0:f67a8fffd94a 26 m_spaceball4000 = 0; /* if set, its a Spaceball 4000 */
jocis 0:f67a8fffd94a 27 m_leftymode4000 = 0; /* if set, Spaceball 4000 in "lefty" orientation */
jocis 1:e6282b645d9b 28
jocis 3:7bacae57a30b 29 _mapping = 1;
jocis 1:e6282b645d9b 30 _escape = false;
jocis 1:e6282b645d9b 31 _idx = 0;
jocis 1:e6282b645d9b 32
jocis 0:f67a8fffd94a 33 _serial.attach ( this, &SpaceBall::SerialISR );
jocis 0:f67a8fffd94a 34
jocis 1:e6282b645d9b 35 //Init();
jocis 0:f67a8fffd94a 36
jocis 0:f67a8fffd94a 37 };
jocis 0:f67a8fffd94a 38
jocis 0:f67a8fffd94a 39 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 40
jocis 0:f67a8fffd94a 41 void SpaceBall::Init()
jocis 0:f67a8fffd94a 42 {
jocis 1:e6282b645d9b 43 if ( _bSpaceOrb )
jocis 1:e6282b645d9b 44 InitSO ();
jocis 1:e6282b645d9b 45 else
jocis 1:e6282b645d9b 46 InitSB ();
jocis 0:f67a8fffd94a 47 }
jocis 0:f67a8fffd94a 48
jocis 0:f67a8fffd94a 49 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 50
jocis 0:f67a8fffd94a 51 void SpaceBall::SerialISR(void)
jocis 0:f67a8fffd94a 52 {
jocis 0:f67a8fffd94a 53 char c;
jocis 0:f67a8fffd94a 54
jocis 0:f67a8fffd94a 55 while ( _serial.readable() )
jocis 0:f67a8fffd94a 56 {
jocis 0:f67a8fffd94a 57 c = _serial.getc();
jocis 0:f67a8fffd94a 58
jocis 0:f67a8fffd94a 59 Process ( c );
jocis 0:f67a8fffd94a 60 }
jocis 0:f67a8fffd94a 61 }
jocis 0:f67a8fffd94a 62
jocis 0:f67a8fffd94a 63 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 64
jocis 0:f67a8fffd94a 65 void SpaceBall::Process ( char c )
jocis 0:f67a8fffd94a 66 {
jocis 0:f67a8fffd94a 67 if ( _bSpaceOrb )
jocis 0:f67a8fffd94a 68 ProcessSO ( c );
jocis 0:f67a8fffd94a 69 else
jocis 0:f67a8fffd94a 70 ProcessSB ( c );
jocis 0:f67a8fffd94a 71 }
jocis 0:f67a8fffd94a 72
jocis 0:f67a8fffd94a 73 ///////////////////////////////////////////////////////////////////////////////
jocis 2:a7c0fcd157f7 74
jocis 2:a7c0fcd157f7 75 float SpaceBall::GetAxis ( int nAxis ) const
jocis 2:a7c0fcd157f7 76 {
jocis 2:a7c0fcd157f7 77 float fValue = 0;
jocis 2:a7c0fcd157f7 78
jocis 2:a7c0fcd157f7 79 if ( _mapping == CNC )
jocis 2:a7c0fcd157f7 80 {
jocis 2:a7c0fcd157f7 81 if ( nAxis == TX ) fValue = _axis[0];
jocis 2:a7c0fcd157f7 82 else if ( nAxis == TY ) fValue = _axis[2];
jocis 2:a7c0fcd157f7 83 else if ( nAxis == TZ ) fValue = _axis[1];
jocis 2:a7c0fcd157f7 84 else if ( nAxis == RX ) fValue = -_axis[0];
jocis 2:a7c0fcd157f7 85 else if ( nAxis == RY ) fValue = -_axis[2];
jocis 2:a7c0fcd157f7 86 else if ( nAxis == RZ ) fValue = -_axis[1];
jocis 2:a7c0fcd157f7 87 }
jocis 2:a7c0fcd157f7 88 else if ( _mapping == CNCvert )
jocis 2:a7c0fcd157f7 89 {
jocis 2:a7c0fcd157f7 90 if ( nAxis == TX ) fValue = _axis[0];
jocis 2:a7c0fcd157f7 91 else if ( nAxis == TY ) fValue = _axis[1];
jocis 2:a7c0fcd157f7 92 else if ( nAxis == TZ ) fValue = -_axis[2];
jocis 2:a7c0fcd157f7 93 else if ( nAxis == RX ) fValue = -_axis[0];
jocis 2:a7c0fcd157f7 94 else if ( nAxis == RY ) fValue = -_axis[1];
jocis 2:a7c0fcd157f7 95 else if ( nAxis == RZ ) fValue = -_axis[2];
jocis 2:a7c0fcd157f7 96 }
jocis 2:a7c0fcd157f7 97 else
jocis 2:a7c0fcd157f7 98 {
jocis 2:a7c0fcd157f7 99 if ( nAxis == TX ) fValue = _axis[0];
jocis 2:a7c0fcd157f7 100 else if ( nAxis == TY ) fValue = _axis[1];
jocis 2:a7c0fcd157f7 101 else if ( nAxis == TZ ) fValue = _axis[2];
jocis 2:a7c0fcd157f7 102 else if ( nAxis == RX ) fValue = _axis[0];
jocis 2:a7c0fcd157f7 103 else if ( nAxis == RY ) fValue = _axis[1];
jocis 2:a7c0fcd157f7 104 else if ( nAxis == RZ ) fValue = _axis[2];
jocis 2:a7c0fcd157f7 105 }
jocis 2:a7c0fcd157f7 106
jocis 2:a7c0fcd157f7 107 if ( nAxis <= TZ )
jocis 2:a7c0fcd157f7 108 fValue *= _fScaleT * _fScale;
jocis 2:a7c0fcd157f7 109 else
jocis 2:a7c0fcd157f7 110 fValue *= _fScaleR * _fScale;
jocis 2:a7c0fcd157f7 111
jocis 2:a7c0fcd157f7 112 return fValue;
jocis 2:a7c0fcd157f7 113 }
jocis 2:a7c0fcd157f7 114
jocis 2:a7c0fcd157f7 115 ///////////////////////////////////////////////////////////////////////////////
jocis 2:a7c0fcd157f7 116
jocis 2:a7c0fcd157f7 117 int SpaceBall::GetAxisRaw ( int nAxis ) const
jocis 2:a7c0fcd157f7 118 {
jocis 2:a7c0fcd157f7 119 if ( nAxis<0 || nAxis>5 )
jocis 2:a7c0fcd157f7 120 return 0;
jocis 2:a7c0fcd157f7 121
jocis 2:a7c0fcd157f7 122 return _axis[nAxis];
jocis 2:a7c0fcd157f7 123 }
jocis 2:a7c0fcd157f7 124
jocis 0:f67a8fffd94a 125 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 126 ///////////////////////////////////////////////////////////////////////////////
jocis 2:a7c0fcd157f7 127 ///////////////////////////////////////////////////////////////////////////////