122 lines
4.0 KiB
Python

from LinearAlgebra.Spherical import *
from LinearAlgebra.Quaternion import *
from LinearAlgebra.SwingTwist import *
from Messages import *
class Thing:
"""! A thing is the basic building block"""
class Type:
"""! Predefined thing types
"""
Undetermined = 0x00
# Sensor
Switch = 0x01
DistanceSensor = 0x02
DirectionalSensor = 0x03
TemperatureSensor = 0x04
Animator = 0x40
Position = 0x01
Orientation = 0x02
LinearVelocity = 0x04
AngularVelocity = 0x08
def __init__(self, owner = None, network_id = 0, thing_id = 0, type = Type.Undetermined, parent = None, name = None):
"""! Create a new thing """
## The parent thing
self.parent = None
if parent is not None:
owner = parent.owner
self.SetParent(parent)
self.children = []
## The participant owning this thing
self.owner = owner
## The network ID of this thing
## @note This field will likely disappear in future versions
self.network_id = network_id
## The ID of the thing
self.id = thing_id
## The type of the thing
self.type = type
## The name of the thing
self.name = name
## An URL pointing to the location where a model of the thing can be found
self.model_url = None
## The position of the thing in local space, in meters
self.position: Spherical = Spherical.zero
self.position_updated: bool = False
## The new orientation in local space
self.orientation: Quaternion = Quaternion.identity
self.orientation_updated: bool = False
## The linear velocity of the thing in local space, in meters per second
self.linear_velocity: Spherical = Spherical.zero
self.linear_velocity_updated: bool = False
## The angular velocity of the thing in local space, in degrees per second
self.angular_velocity: Spherical = Spherical.zero
self.angular_velocity_updated: bool = False
self.pose_updated = 0x00 # the bits indicate which fields have been updated
self.owner.Add(self)
def SetPosition(self, position):
"""! Set the position of the thing
@param position The new position in local space, in meters
"""
self.position = position
self.position_updated = True
def SetOrientation(self, orientation):
"""! Set the orientation of the thing
@param orientation The new orientation in local space
"""
self.orientation = orientation
self.orientation_updated = True
def SetLinearVelocity(self, linear_velocity):
"""! Set the linear velocity of the thing
@param linearVelocity The new linear velocity in local space, in meters per second
"""
self.linear_velocity = linear_velocity
self.linear_velocity_updated = True
def SetAngularVelocity(self, angular_velocity):
"""! Set the angular velocity of the thing
@param angularVelocity the new angular velocity in local space
"""
self.angular_velocity = angular_velocity
self.angular_velocity_updated = True
def Update(self, currentTime):
pose_msg = PoseMsg(self.network_id, self)
for other in self.owner.others:
self.owner.Send(other, pose_msg)
def ProcessBinary(self, data):
print('default binary processor')
pass
def SetParent(self, parent):
if parent is None:
parentThing = self.parent
if parentThing is not None:
parentThing.RemoveChild(self)
self.parent = None
else:
parent.AddChild(self)
def AddChild(self, child):
if child in self.children:
return
child.parent = self
self.children.append(child)
def RemoveChild(self, child):
self.children.remove(child)
def GenerateBinary(self, buffer, ix_ref):
pass