Example #1
0
class Sensors(object):

    def __init__(self):
     self.__ipcon = IPConnection('localhost', 4223)
     
    def __del__(self):
        self.__ipcon.destroy()
        
    def add(self, pSensor):
        self.__ipcon.add_device(pSensor)
        
Example #2
0
class Q:
    HOST = "localhost"
    PORT = 4223
    UID = "aeoUQwwyAvY" # Change to your UID

    def __init__(self):
        self.base_x = 0.0
        self.base_y = 0.0
        self.base_z = 0.0
        self.base_w = 0.0

        self.imu = IMU(self.UID) # Create device object
        self.ipcon = IPConnection(self.HOST, self.PORT) # Create IPconnection to brickd
        self.ipcon.add_device(self.imu) # Add device to IP connection
        # Don't use device before it is added to a connection

        # Wait for IMU to settle
        print 'Set IMU to base position and wait for 10 seconds'
        print 'Base position will be 0 for all angles'
        time.sleep(10)
        q = self.imu.get_quaternion()
        self.set_base_coordinates(q.x, q.y, q.z, q.w)

        # Set period for quaternion callback to 10ms
        self.imu.set_quaternion_period(10)

        # Register quaternion callback
        self.imu.register_callback(self.imu.CALLBACK_QUATERNION, self.quaternion_cb)

    def quaternion_cb(self, x, y, z, w):
        # Use conjugate of quaternion to rotate coordinates according to base system
        x, y, z, w = self.make_relative_coordinates(-x, -y, -z, w)

        x_angle = int(math.atan2(2.0*(y*z - w*x), 1.0 - 2.0*(x*x + y*y))*180/math.pi)
        y_angle = int(math.atan2(2.0*(x*z + w*y), 1.0 - 2.0*(x*x + y*y))*180/math.pi)
        z_angle = int(math.atan2(2.0*(x*y + w*z), 1.0 - 2.0*(x*x + z*z))*180/math.pi)

        print 'x: {0}, y: {1}, z: {2}'.format(x_angle, y_angle, z_angle)

    def set_base_coordinates(self, x, y, z, w):
        self.base_x = x
        self.base_y = y
        self.base_z = z
        self.base_w = w

    def make_relative_coordinates(self, x, y, z, w):
        # Multiply base quaternion with current quaternion
        return (
            w * self.base_x + x * self.base_w + y * self.base_z - z * self.base_y,
            w * self.base_y - x * self.base_z + y * self.base_w + z * self.base_x,
            w * self.base_z + x * self.base_y - y * self.base_x + z * self.base_w,
            w * self.base_w - x * self.base_x - y * self.base_y - z * self.base_z
        )
Example #3
0
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "a4JritAp6Go" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_imu import IMU

imu = IMU(UID) # Create device object

# Quaternion callback
def quaternion_cb(x, y, z, w):
    print("x: " + str(x) + "\ny: " + str(y) + "\nz: " + str(z) + "\nw: " + str(w) + "\n")

if __name__ == "__main__":
	ipcon = IPConnection(HOST, PORT) # Create IPconnection to brickd
	ipcon.add_device(imu) # Add device to IP connection
	# Don't use device before it is added to a connection

	# Set period for quaternion callback to 1s
	imu.set_quaternion_period(100)

	# Register quaternion callback
	imu.register_callback(imu.CALLBACK_QUATERNION, quaternion_cb)
	
	imu.leds_off() # Turn LEDs off.

	raw_input('Press key to exit\n') # Use input() in Python 3
	ipcon.destroy()
Example #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-  

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_gps import GPS
from settings import HOST, PORT, GPSUID
from array import array


ipcon = IPConnection(HOST, PORT) # Create ip connection to brickd

gps = GPS(GPSUID) # Create device object
ipcon.add_device(gps) # Add device to ip connection
coords = gps.get_coordinates()
lon = coords.longitude/1000000.0
lat = coords.latitude/1000000.0
print('Latitude: ' + str(lat) + '° ' + coords.ns)
print('Longitude: ' + str(lon/1000000.0) + '° ' + coords.ew)
if coords.ns == "S":
    lat = 0 - lat
if coords.ew == "W":
    lon = 0 - lon
arr = array(lat, lon)


try:
    # This will create a new file or **overwrite an existing file**.
    f = open("location.txt", "w")
    try:
        arr.tofile(f)
    finally:
Example #5
0
    # hier soll spaeter die Webcam gezeigt werden
    screen = pygame.display.set_mode((200, 200))
    # Wird benoetigt um die KeyWiederholung zu realisieren
    clock = pygame.time.Clock()

    # Create IP connection to brickd
    ipcon = IPConnection(HOST, PORT)

    # Create device objects
    dc0 = DC(UIDdc0)
    dc1 = DC(UIDdc1)
    lcd = LCD20x4(UIDdpl)
    mst = Master(UIDmaster)

    # Connect to devices
    ipcon.add_device(dc0)
    ipcon.add_device(dc1)
    ipcon.add_device(lcd)
    ipcon.add_device(mst)

    # Hier wird ein Initial Program gestartet um eventuelle sonderwuensche entgegenzunehmen
    velol = 0
    velor = 0
    hz = 0
    acc = 0
    getVars(velol, velor, hz, acc)

    #Hier der zweite thread der sich um die Steuerung kuemmert, er laueft deutlich schneller!
    #Hier wird nun auch der Status abgefragt - ca. je 1sec * 6 / 1000 !!
    Control(velol, velor, hz, acc)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UIDm = "9p19drqHQdS"  # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_master import Master
#from tinkerforge.brick_dc import DC
#from tinkerforge.bricklet_lcd_20x4 import LCD20x4

ipcon = IPConnection(HOST, PORT)  # Create IP connection to brickd

master = Master(UIDm)  # Create device object

ipcon.add_device(master)  # Add device to IP connection

# Get voltage and current from stack (in mV/mA)
voltage = master.get_stack_voltage()
current = master.get_stack_current()
tempera = master.get_chip_temperature()

# Print Voltage, Current and Temperature from Master
print('Stack Voltage: ' + str(voltage / 1000.0) + ' V')
print('Stack Current: ' + str(current / 1000.0) + ' A')
print('Stack Tempera: ' + str(tempera / 10) + ' °C')
    else:
        steps = random.randint(-5000, -1000) # steps (backward)
        print('Driving backward: ' + str(steps) + ' steps')

    vel = random.randint(200, 2000) # steps/s
    acc = random.randint(100, 1000) # steps/s^2
    dec = random.randint(100, 1000) # steps/s^2
    print('Configuration (vel, acc, dec): ' + str((vel, acc, dec)))

    stepper.set_speed_ramping(acc, dec)
    stepper.set_max_velocity(vel)
    stepper.set_steps(steps)

if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

    stepper = Stepper(UID) # Create device object
    ipcon.add_device(stepper) # Add device to IP connection
    # Don't use device before it is added to a connection

    # Register "position reached callback" to cb_reached
    # cb_reached will be called every time a position set with
    # set_steps or set_target_position is reached
    stepper.register_callback(stepper.CALLBACK_POSITION_REACHED, cb_reached)

    stepper.enable()
    stepper.set_steps(1) # Drive one step forward to get things going

    raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.destroy()
Example #8
0
class TomIMU:	
	def __init__(self,host,port,uid,callbackPeriodMS=100):
		self.host=host
		self.port=port
		self.uid=uid
		self._imu = IMU(uid) # Create device object
		self._ipcon = IPConnection(self.host,self.port)  # Create IPconnection to brickd
		self._ipcon.add_device(self._imu) # Add device to IP connection
		
		self.ready = True # Don't use device before it is added to a connection
		
		# Set period for quaternion callback (defaults to 100ms)
		self._imu.set_quaternion_period(callbackPeriodMS)
		
		# Register quaternion callback
		self._imu.register_callback(self._imu.CALLBACK_QUATERNION, self._QuaternionCallback)
		
		self._imu.leds_off() # Turn LEDs off.
		self._imu.set_convergence_speed(5) # 5ms convergence.
		
		# Orientation origin and most recent values
		q = self._imu.get_quaternion() # Get a temp quaternion from current pose.
		self.rel_x = q.x
		self.rel_y = q.y
		self.rel_z = q.z
		self.rel_w = q.w
		self.x = q.x
		self.y = q.y
		self.z = q.z
		self.w = q.w
		
	def __destroy__(self):
		self._ipcon.destroy()
	
	def _QuaternionCallback(self, x, y, z, w):
		""" Records the most recent quaternion orientation values. """
		self.x,self.y,self.z,self.w = x,y,z,w
		
	def SetOrientationOrigin(self, origin=None):
		""" Resets the orientation origin to the given values, or the latest reading if none. """
		if origin is None:
			self.rel_x, self.rel_y, self.rel_z, self.rel_w = self.x, self.y, self.z, self.w
		else:
			self.rel_x, self.rel_y, self.rel_z, self.rel_w = origin
	
	def GetEulerOrientation(self):
		x,y,z,w = self.GetQuaternionOrientation()
		from math import atan2, asin
		roll  = atan2(2.0*y*w - 2.0*x*z, 1.0 - 2.0*y*y - 2.0*z*z)
		pitch = atan2(2.0*x*w - 2.0*y*z, 1.0 - 2.0*x*x - 2.0*z*z)
		yaw   =  asin(2.0*x*y + 2.0*z*w)
		return roll,pitch,yaw
	
	def GetQuaternionOrientation(self):
		# Conjugate
		x,y,z = -self.x, -self.y, -self.z
		w = self.w
		# Multiply
		wn = w * self.rel_w - x * self.rel_x - y * self.rel_y - z * self.rel_z
		xn = w * self.rel_x + x * self.rel_w + y * self.rel_z - z * self.rel_y
		yn = w * self.rel_y - x * self.rel_z + y * self.rel_w + z * self.rel_x
		zn = w * self.rel_z + x * self.rel_y - y * self.rel_x + z * self.rel_w
		return xn,yn,zn,wn
	
	def GetConvergenceSpeed(self):
		return self._imu.get_convergence_speed()
	
	def SetConvergenceSpeed(self, speed):
		self._imu.set_convergence_speed(speed)
Example #9
0
        enable = True
    else:
        enable = False
        print("stop!!!1")
        servo.set_position(steeringsrv, mid)
        servo.set_position(motor, stop)




if __name__ == "__main__":
    enable = False
    ipcon = IPConnection(HOST, PORT) # Create ip connection to brickd

    gps = GPS(GPSUID) # Create device object
    ipcon.add_device(gps) # Add device to ip connection
    servo = Servo(SERVOUID) # Create device object
    ipcon.add_device(servo) # Add device to IP connection
    # Don't use device before it is added to a connection
    servo.set_degree(motor, -9000, 9000)
    servo.set_pulse_width(motor, 950, 1950)
    servo.set_period(motor, 20000)
    servo.set_acceleration(motor, 7000)
    servo.set_velocity(motor, 0xFFFF) # Full speed
    servo.set_degree(steeringsrv, -3600, 3600)
    servo.set_pulse_width(steeringsrv, 955, 2000)
    servo.set_period(steeringsrv, 20000)
    servo.set_acceleration(steeringsrv, 7000) # Full acceleration 0xFFFF
    servo.set_velocity(steeringsrv, 0xFFFF) # Full speed
    
    # hier soll spaeter die Webcam gezeigt werden
    screen = pygame.display.set_mode((200, 200))
    # Wird benoetigt um die KeyWiederholung zu realisieren
    clock = pygame.time.Clock()

    # Create IP connection to brickd
    ipcon = IPConnection(HOST, PORT)

    # Create device objects
    dc0 = DC(UIDdc0)
    dc1 = DC(UIDdc1)
    lcd = LCD20x4(UIDdpl)
    mst = Master(UIDmaster)

    # Connect to devices
    ipcon.add_device(dc0)
    ipcon.add_device(dc1)
    ipcon.add_device(lcd)
    ipcon.add_device(mst)

    # Hier wird ein Initial Program gestartet um eventuelle sonderwuensche entgegenzunehmen
    velol = 0
    velor = 0
    hz = 0
    acc = 0
    getVars(velol, velor, hz, acc)

    # Hier der zweite thread der sich um die Steuerung kuemmert, er laueft deutlich schneller!
    # Hier wird nun auch der Status abgefragt - ca. je 1sec * 6 / 1000 !!
    Control(velol, velor, hz, acc)
UID_TEMPERATURE = "bPf"
UID_BAROMETER   = "bNs"

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_master import Master
from tinkerforge.bricklet_lcd_20x4 import LCD20x4
from tinkerforge.bricklet_temperature import Temperature
from tinkerforge.bricklet_barometer import Barometer

from string_magic import unicode_to_ks0066u

if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brick

    master = Master(UID_MASTER) # Create device object
    ipcon.add_device(master) # Add device to IP connection
    # Don't use device before it is added to a connection

    ## Get voltage and current from stack (in mV / mA)
    #voltage = master.get_stack_voltage()
    #current = master.get_stack_current()

    ##print('Stack Voltage: ' + str(voltage / 1000.0) + ' V')
    ##print('Stack Current: ' + str(current / 1000.0) + ' A')
 
    t = Temperature(UID_TEMPERATURE) # Create temperature object
    ipcon.add_device(t) # Add device to IP connection
    # Don't use device before it is added to connection

    # Get current temperature (unit is °C/ 100)
    temperature = t.get_temperature() / 100.0
#!/usr/bin/env python
# -*- coding: utf-8 -*-  

HOST = "localhost"
PORT = 4223
UIDm = "9p19drqHQdS" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_master import Master
#from tinkerforge.brick_dc import DC
#from tinkerforge.bricklet_lcd_20x4 import LCD20x4

ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

master = Master(UIDm) # Create device object

ipcon.add_device(master) # Add device to IP connection

# Get voltage and current from stack (in mV/mA)
voltage = master.get_stack_voltage()
current = master.get_stack_current()
tempera = master.get_chip_temperature()

# Print Voltage, Current and Temperature from Master
print('Stack Voltage: ' + str(voltage/1000.0) + ' V')
print('Stack Current: ' + str(current/1000.0) + ' A')
print('Stack Tempera: ' + str(tempera/10) + ' °C')