Exemple #1
0
	def __init__(self):
		threading.Thread.__init__(self)
		self.shutdown_flag = threading.Event()
		self.motor = Motorcontroller()
		self.buzzer = Buzzer()
		self.xbee = Xbee()
		self.decoder = Decoder()
		self.servo1 = 96
		self.servo2 = 75
		self.joycalc = Joystick()
		self.motor.setServo1(self.servo1)
		self.motor.setServo2(self.servo2)
		self.lastSavedTime = 0
    def __init__(self, waypointFile=None):
        """Constructor for the boat object"""
        self.arduino = Arduino()
        self.gps = Gps()
        self.xbee = Xbee()
        self._waypointN = 0
        self._waypointE = 0
        self._waypointNumber = 0
        self._waypointDist = 0
        self._waypointHeading = 0
        self._targetHeading = 0
        self._targetDistance = 0

        self.s = 0
        self.c = 0
        self.r = 250
Exemple #3
0
class XbeeSensorMesh(SensorMesh):
    """Creates a new Xbee instance and registers with the digital callback.
    When a digital reading comes in, it uses the "sensors" dictionary to map
    Xbee addresses to web server sensor ids"""
    def __init__(self, sensors):
        super(XbeeSensorMesh, self).__init__()
        self.xbee = Xbee()
        self.xbee.dig_cb = self.dig_cb
        self.sensors = sensors

    def dig_cb(self, addr, network, pin, value):
        try:
            if pin == 0:
                num = self.sensors[addr]
                if self.sensor_cb:
                    self.sensor_cb(num, not value) #Invert for active low port
        except KeyError:
            print "Got reading from unknown sensor 0x%x" % addr

    def recv(self, data):
        """Eats input data from the Xbee module and generates events"""
        self.xbee.recv(data)
    def __init__(self, waypointFile=None):
        """Constructor for the boat object"""
        self.arduino = Arduino()
        self.gps = Gps()
        self.xbee = Xbee()
        self._waypointN = 0
        self._waypointE = 0
        self._waypointNumber = 0
        self._waypointDist = 0
        self._waypointHeading = 0
        self._targetHeading = 0
        self._targetDistance = 0

        self.s = 0
        self.c = 0
        self.r = 250
Exemple #5
0
        pg.key.set_repeat()
        # game loop
        while self._running:
            # handle events
            for event in pg.event.get():
                self.on_event(event)
            # update game state
            self.on_loop()
            # render or flush outputs
            self.on_render()
        self.on_cleanup()


def keyUpdate(kold, knew):
    res = []
    for i in range(len(knew)):
        if kold[i] != knew[i]:
            res.append(pg.key.name(i) + ['u', 'd'][knew[i]])
    return res


if __name__ == "__main__":

    xb = Xbee(SRV_IP, SRV_PT)
    xb.connect()

    theApp = App(xb.sk)
    theApp.on_execute()

    xb.close()
Exemple #6
0
 def __init__(self, sensors):
     super(XbeeSensorMesh, self).__init__()
     self.xbee = Xbee()
     self.xbee.dig_cb = self.dig_cb
     self.sensors = sensors
Exemple #7
0
 def setUp(self):
     self.xbee = Xbee()
     self.xbee.write_cb = self.write
Exemple #8
0
class TestXbeeTransmit:
    def setUp(self):
        self.xbee = Xbee()
        self.xbee.write_cb = self.write

    def open(self, file, mode):
        return open(OUTPUT_DIR + file, mode)

    def close(self):
        self.f.close()

    def compare(self, file):
        ifile = OUTPUT_DIR + file
        ofile = ifile+"_lastrun"
        output = open(ofile, "w")
        output.write(self.data)
        output.close()
        assert filecmp.cmp(ifile, ofile)

    def write(self, data):
        self.data = data

    def do_transmit(self):
        self.xbee.transmit(0xffff, 0xffff, "Hello")

    def do_at_command(self):
        self.xbee.at_command("D0" + chr(0x4))

    def do_at_command_remote(self):
        self.xbee.at_command_remote(0x123, 0xfffe, "D0" + chr(0x4))

    def test_at_command(self):
        self.do_at_command()
        self.compare("at_command")

    def test_at_command_remote(self):
        self.do_at_command_remote()
        self.compare("at_command_remote")

    def test_transmit(self):
        self.do_transmit()
        self.compare("transmit")

    def gen_output(self):
        self.setUp()

        f = self.open("transmit", "w")
        self.do_transmit()
        f.write(self.data)
        f.close()

        self.setUp()
        f = self.open("at_command", "w")
        self.do_at_command()
        f.write(self.data)
        f.close()
        print self.data

        self.setUp()
        f = self.open("at_command_remote", "w")
        self.do_at_command_remote()
        f.write(self.data)
        f.close()
        print self.data
Exemple #9
0
class Serialcom(threading.Thread):

	def __init__(self):
		threading.Thread.__init__(self)
		self.shutdown_flag = threading.Event()
		self.motor = Motorcontroller()
		self.buzzer = Buzzer()
		self.xbee = Xbee()
		self.decoder = Decoder()
		self.servo1 = 96
		self.servo2 = 75
		self.joycalc = Joystick()
		self.motor.setServo1(self.servo1)
		self.motor.setServo2(self.servo2)
		self.lastSavedTime = 0


	def run(self):
		print('Thread #%s started' % self.ident)
		self.motor.timeout(1)
		while not self.shutdown_flag.is_set():
			rcvdata = self.xbee.read()
			self.decoder.decode(rcvdata)
			self.motor.recalCommand()
			currenttime = time.time()
			if currenttime - self.lastSavedTime > 1.0:
				self.lastSavedTime = time.time()
				self.xbee.sendBat(self.decoder.getRfrating())
			if self.decoder.getStatus() and self.decoder.checkCRC():
				if self.decoder.getJoyStickPB1() == 0:
					self.motor.EmergyStop()
					self.buzzer.beep(300)

				elif self.decoder.getJoystickM1() > 248 and self.decoder.getJoystickM2() > 248:
					self.joycalc.calculateReg(255)
					self.motor.Motor1MC2(255 - self.joycalc.cor1)
					self.motor.Motor2MC2(255 - self.joycalc.cor2)

				elif (abs(self.decoder.getJoystickM1() - self.decoder.getJoystickM2()) <= 3) and (self.decoder.getJoystickM1() > 50):
					self.joycalc.calculateReg(self.decoder.getJoystickM1())
					self.motor.Motor1MC2(self.decoder.getJoystickM1() - self.joycalc.cor1)
					self.motor.Motor2MC2(self.decoder.getJoystickM1() - self.joycalc.cor2)
					#print "drive forward without full speed"
				else:
					self.motor.Motor1MC2(self.decoder.getJoystickM1())
					self.motor.Motor2MC2(self.decoder.getJoystickM2())
					#print "other speeds"

				if self.decoder.getJoystickPB2() == 0:
					self.servo1 = 96
					self.motor.setServo1(self.servo1)
					self.buzzer.beep(300)

				elif self.decoder.getJoystickVRX2() > 1000:
					if(self.servo1 > 0):
						self.servo1 = self.servo1 - 1
						self.motor.setServo1(self.servo1)
				elif self.decoder.getJoystickVRX2() < 24:
					if(self.servo1 < 180):
						self.servo1 = self.servo1 + 1
						self.motor.setServo1(self.servo1)

				if self.decoder.getJoystickPB2() == 0:
					self.servo2 = 75
					self.motor.setServo2(self.servo2)

				elif self.decoder.joystick_VRY2 > 1000:
					if(self.servo2 > 0):
						self.servo2 = self.servo2 - 1
						self.motor.setServo2(self.servo2)
				elif self.decoder.getJoystickVRY2() < 24:
					if(self.servo2 < 180):
						self.servo2 = self.servo2 + 1
						self.motor.setServo2(self.servo2)

			time.sleep(0.001)

		# ... Clean shutdown code here ...
		self.xbee.close()
		self.motor.close()
		print('Thread #%s stopped' % self.ident)
class Boat(object):
    def __init__(self, waypointFile=None):
        """Constructor for the boat object"""
        self.arduino = Arduino()
        self.gps = Gps()
        self.xbee = Xbee()
        self._waypointN = 0
        self._waypointE = 0
        self._waypointNumber = 0
        self._waypointDist = 0
        self._waypointHeading = 0
        self._targetHeading = 0
        self._targetDistance = 0

        self.s = 0
        self.c = 0
        self.r = 250

    def log(self, logfilename='logfile'):
        """
        Log the output of the sensors to stdout, a logfile and down the xbee
        """
        try:
            l = 'time={time}\
 bhead={head}\
 wind={wind}\
 lat={pos.lat}\
 lon={pos.long}\
 nwlat={wpn}\
 nwlon={wpe}\
 nwn={num}\
 spos={sail}\
 rpos={rudder}\
 whead={waypointHeading}\
 distance={waypointDistance}\
 speed={speed}\
 thead={targetHeading}\
 tdist={targetDistance}\
 temp={temp}\n\r'.format(
                    time = int(time.time()),
                    head = self.arduino.get_compass(),
                    wind = self.get_wind_average(),
                    pos = self.gps.position,
                    wpn = self._waypointN,
                    wpe= self._waypointE,
                    num = self._waypointNumber,
                    sail = self.arduino.sailAngle,
                    rudder = self.arduino.rudderAngle,
                    waypointHeading = self._waypointHeading,
                    waypointDistance = self._waypointDist,
                    speed = self.gps.speed,
                    targetHeading = self._targetHeading,
                    targetDistance = self._targetDistance,
                    temp = self.arduino.get_compass(),
                )

            # write to log file
            with open(logfilename, 'a') as f:
                f.write(l)

            # write to xbee
            self.xbee.send(l)
            
           # write to console
            log(l)

        except:
            trace = traceback.format_exc()
            with open('errors', 'a') as f:
                f.write(str(time.time()) + ':\n' + trace + '\n')

    def get_wind_bearing(self):
        """Return the absolute bearing of the wind"""
        wind = Bearing(self.arduino.get_wind())
        bearing = Bearing(self.arduino.get_compass())
        return wind + bearing

    def get_wind_average(self):
        self.s += (sin(math.radians(self.get_wind_bearing())) - self.s) / self.r
        self.c += (cos(math.radians(self.get_wind_bearing())) - self.c) / self.r
        a = int(math.degrees(atan2(self.s, self.c)))
        if a < 0:
            return a + 360
        else:
            return a

    def set_waypoint_northing(self, v):
        self._waypointN = v

    def set_waypoint_easting(self, v):
        self._waypointE = v

    def get_waypoint_number(self):
        return self._waypointNumber

    def set_waypoint_number(self, v):
        self._waypointNumber = v

    def set_waypoint_distance(self, v):
        self._waypointDist = v

    def set_waypoint_heading(self, v):
        self._waypointHeading = v

    def set_target_heading(self, v):
        self._targetHeading = v

    def set_target_distance(self, v):
        self._targetDistance = v
class Boat(object):
    def __init__(self, waypointFile=None):
        """Constructor for the boat object"""
        self.arduino = Arduino()
        self.gps = Gps()
        self.xbee = Xbee()
        self._waypointN = 0
        self._waypointE = 0
        self._waypointNumber = 0
        self._waypointDist = 0
        self._waypointHeading = 0
        self._targetHeading = 0
        self._targetDistance = 0

        self.s = 0
        self.c = 0
        self.r = 250

    def log(self, logfilename='logfile'):
        """
        Log the output of the sensors to stdout, a logfile and down the xbee
        """
        try:
            l = 'time={time}\
 bhead={head}\
 wind={wind}\
 lat={pos.lat}\
 lon={pos.long}\
 nwlat={wpn}\
 nwlon={wpe}\
 nwn={num}\
 spos={sail}\
 rpos={rudder}\
 whead={waypointHeading}\
 distance={waypointDistance}\
 speed={speed}\
 thead={targetHeading}\
 tdist={targetDistance}\
 temp={temp}\n\r'.format(
                time=int(time.time()),
                head=self.arduino.get_compass(),
                wind=self.get_wind_average(),
                pos=self.gps.position,
                wpn=self._waypointN,
                wpe=self._waypointE,
                num=self._waypointNumber,
                sail=self.arduino.sailAngle,
                rudder=self.arduino.rudderAngle,
                waypointHeading=self._waypointHeading,
                waypointDistance=self._waypointDist,
                speed=self.gps.speed,
                targetHeading=self._targetHeading,
                targetDistance=self._targetDistance,
                temp=self.arduino.get_compass(),
            )

            # write to log file
            with open(logfilename, 'a') as f:
                f.write(l)

            # write to xbee
            self.xbee.send(l)

            # write to console
            log(l)

        except:
            trace = traceback.format_exc()
            with open('errors', 'a') as f:
                f.write(str(time.time()) + ':\n' + trace + '\n')

    def get_wind_bearing(self):
        """Return the absolute bearing of the wind"""
        wind = Bearing(self.arduino.get_wind())
        bearing = Bearing(self.arduino.get_compass())
        return wind + bearing

    def get_wind_average(self):
        self.s += (sin(math.radians(self.get_wind_bearing())) -
                   self.s) / self.r
        self.c += (cos(math.radians(self.get_wind_bearing())) -
                   self.c) / self.r
        a = int(math.degrees(atan2(self.s, self.c)))
        if a < 0:
            return a + 360
        else:
            return a

    def set_waypoint_northing(self, v):
        self._waypointN = v

    def set_waypoint_easting(self, v):
        self._waypointE = v

    def get_waypoint_number(self):
        return self._waypointNumber

    def set_waypoint_number(self, v):
        self._waypointNumber = v

    def set_waypoint_distance(self, v):
        self._waypointDist = v

    def set_waypoint_heading(self, v):
        self._waypointHeading = v

    def set_target_heading(self, v):
        self._targetHeading = v

    def set_target_distance(self, v):
        self._targetDistance = v