Class: Phidgets::Spatial

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/phidgets-ffi/spatial.rb

Overview

This class represents a PhidgetSpatial.

Defined Under Namespace

Classes: SpatialAccelerometerAxes, SpatialCompassAxes, SpatialGyroAxes

Constant Summary collapse

Klass =
Phidgets::FFI::CPhidgetSpatial

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#attached?, #attached_to_server?, attributes, #close, #detached?, #detached_to_server?, device_class, #device_class, device_id, #id, #initialize, #label, #label=, #name, #on_attach, #on_detach, #on_error, #on_server_connect, #on_server_disconnect, #on_sleep, #on_wake, #serial_number, #server_address, server_address, #server_id, server_id, server_status, #type, #version, #wait_for_attachment

Instance Attribute Details

#accelerometer_axesSpatialAccelerometerAxes (readonly)

Collection of accelerometer axes



11
12
13
# File 'lib/phidgets-ffi/spatial.rb', line 11

def accelerometer_axes
  @accelerometer_axes
end

#attributesObject (readonly)

The attributes of a PhidgetSpatial



24
25
26
# File 'lib/phidgets-ffi/spatial.rb', line 24

def attributes
  @attributes
end

#compass_axesSpatialCompassAxes (readonly)

Collection of compass axes

Returns:



15
16
17
# File 'lib/phidgets-ffi/spatial.rb', line 15

def compass_axes
  @compass_axes
end

#gyro_axesSpatialGyroAxes (readonly)

Collection of gyro axes

Returns:



19
20
21
# File 'lib/phidgets-ffi/spatial.rb', line 19

def gyro_axes
  @gyro_axes
end

Instance Method Details

#data_rateInteger

Returns data rate in ms, or raises an error.

Returns:

  • (Integer)

    returns data rate in ms, or raises an error.



105
106
107
108
109
# File 'lib/phidgets-ffi/spatial.rb', line 105

def data_rate
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getDataRate(@handle, @index, ptr)
  ptr.get_int(0)
end

#data_rate=(new_data_rate) ⇒ Integer

Sets the data rate in ms, or raises an error.

Parameters:

  • new_data_rate (Integer)

    data rate

Returns:

  • (Integer)

    returns the data rate in ms, or raises an error.



114
115
116
117
# File 'lib/phidgets-ffi/spatial.rb', line 114

def data_rate=(new_data_rate)
  Klass.setDataRate(@handle, @index, new_data_rate.to_i)
  new_data_rate.to_i
end

#data_rate_maxInteger

Returns maximum data rate in ms, or raises an error.

Returns:

  • (Integer)

    returns maximum data rate in ms, or raises an error.



127
128
129
130
131
# File 'lib/phidgets-ffi/spatial.rb', line 127

def data_rate_max
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getDataRateMax(@handle, @index, ptr)
  ptr.get_int(0)
end

#data_rate_minInteger

Returns minimum data rate in ms, or raises an error.

Returns:

  • (Integer)

    returns minimum data rate in ms, or raises an error.



120
121
122
123
124
# File 'lib/phidgets-ffi/spatial.rb', line 120

def data_rate_min
  ptr = ::FFI::MemoryPointer.new(:int)
  Klass.getDataRateMin(@handle, @index, ptr)
  ptr.get_int(0)
end

#on_spatial_data(obj = nil, &block) ⇒ Boolean

Sets a spatial data handler callback function. This is called at a fixed rate as determined by the data rate property. Contains data for acceleration/gyro/compass depending on what the board supports.

As this runs in it’s own thread, be sure that all errors are properly handled or the thread will halt and not fire any more.

Examples:

spatial.on_spatial_data do |device, acceleration, magnetic_field, angular_rate, obj|
  puts "Acceleration: #{acceleration[0]},#{acceleration[1]}, #{acceleration[2]} | Magnetic field: #{magnetic_field[0]},#{magnetic_field[1]}, #{magnetic_field[2]} | Angular rate: #{angular_rate[0]},#{angular_rate[1]}, #{angular_rate[2]}"    #     end

Parameters:

  • obj (String) (defaults to: nil)

    Object to pass to the callback function. This is optional.

  • Block (Proc)

    When the callback is executed, the device and object are yielded to this block.

Returns:

  • (Boolean)

    returns true or raises an error



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/phidgets-ffi/spatial.rb', line 41

def on_spatial_data(obj=nil, &block)
  @on_spatial_data_obj = obj
  @on_spatial_data = Proc.new { |device, obj_ptr, data, data_count|
		
	  acceleration = []
	  if accelerometer_axes.size > 0
 acceleration = [accelerometer_axes[0].acceleration, accelerometer_axes[1].acceleration, accelerometer_axes[2].acceleration]
	  end

  magnetic_field= []
  if compass_axes.size > 0 
    #Even when there is a compass chip, sometimes there won't be valid data in the event.
		begin
      magnetic_field = [compass_axes[0].magnetic_field, compass_axes[1].magnetic_field, compass_axes[2].magnetic_field]
    rescue Phidgets::Error::UnknownVal => e
      magnetic_field = ['Unknown', 'Unknown', 'Unknown']
    end
  end

  angular_rate = []
  if gyro_axes.size > 0
    angular_rate = [gyro_axes[0].angular_rate, gyro_axes[1].angular_rate, gyro_axes[2].angular_rate]
  end
	
  yield self, acceleration, magnetic_field, angular_rate, object_for(obj_ptr)
	}
  Klass.set_OnSpatialData_Handler(@handle, @on_spatial_data, pointer_for(obj))
end

#reset_compass_correction_parametersBoolean

Resets correction parameters for the magnetometer triad. This returns magnetometer output to raw magnetic field strength.

Returns:

  • (Boolean)

    returns true if successful, or raises an error.



79
80
81
82
# File 'lib/phidgets-ffi/spatial.rb', line 79

def reset_compass_correction_parameters
  Klass.resetCompassCorrectionParameters(@handle)
	  true
end

#set_compass_correction_parameters(new_mag_field, new_offset0, new_offset1, new_offset2, new_gain0, new_gain1, new_gain2, new_t0, new_t1, new_t2, new_t3, new_t4, new_t5) ⇒ Boolean

Sets correction paramaters for the magnetometer triad. This is for filtering out hard and soft iron offsets, and scaling the output to match the local field strength. These parameters can be obtained from the compass calibration program provided by Phidgets Inc.

Parameters:

  • new_mag_field (Integer)

    local magnetic field strength

  • new_offset0 (Integer)

    axis 0 offset correction

  • new_offset1 (Integer)

    axis 1 offset correction

  • new_offset2 (Integer)

    axis 2 offset correction

  • new_gain0 (Integer)

    axis 0 gain correction.

  • new_gain1 (Integer)

    axis 1 gain correction.

  • new_gain2 (Integer)

    axis 2 gain correction.

  • new_t0 (Integer)

    non-orthogonality correction factor 0

  • new_t1 (Integer)

    non-orthogonality correction factor 1

  • new_t2 (Integer)

    non-orthogonality correction factor 2

  • new_t3 (Integer)

    non-orthogonality correction factor 3

  • new_t4 (Integer)

    non-orthogonality correction factor 4

  • new_t5 (Integer)

    non-orthogonality correction factor 5

Returns:

  • (Boolean)

    returns true if successful, or raises an error.



99
100
101
102
# File 'lib/phidgets-ffi/spatial.rb', line 99

def set_compass_correction_parameters(new_mag_field, new_offset0, new_offset1, new_offset2, new_gain0, new_gain1, new_gain2, new_t0, new_t1, new_t2, new_t3, new_t4, new_t5)
  Klass.setCompassCorrectionParameters(@handle, new_mag_field, new_offset0, new_offset1, new_offset2, new_gain0, new_gain1, new_gain2, new_t0, new_t1, new_t2, new_t3, new_t4, new_t5)
	  true
end

#zero_gyroBoolean

Zeroes the gyro. This takes 1-2 seconds to complete and should only be called when the board is stationary.

Returns:

  • (Boolean)

    returns true if successful, or raises an error.



72
73
74
75
# File 'lib/phidgets-ffi/spatial.rb', line 72

def zero_gyro
  Klass.zeroGyro(@handle)
	  true
end