Exemplo n.º 1
0
 def testInitializePosition(self):
     """
     Confirm that the LightSensor initializes the position of the object
     based on the provided coordinates.
     """
     initial_location = (1, 5, 3)
     initial_input_range = (1, 5)
     initial_output_range = (1, 5)
     sensor_under_test = LightSensor(initial_location, initial_input_range, \
     initial_output_range)
     self.assertEqual(sensor_under_test.current_position(),
                      initial_location)
Exemplo n.º 2
0
    def testMovePosition(self):
        """
        Confirm that when a LightSensor is moved after creation that the object
        reports the updated location rather than the initial one.
        """
        initial_location = (1, 5, 3)
        initial_input_range = (1, 5)
        initial_output_range = (1, 5)

        new_location = (2, 3, 2)
        sensor_under_test = LightSensor(initial_location, initial_input_range, \
        initial_output_range)
        sensor_under_test.move_to_position(new_location)
        self.assertEqual(sensor_under_test.current_position(), new_location)
Exemplo n.º 3
0
    def testIdenticalTranslation(self):
        """
        Confirm the trivial case that when a light sensors input range and
        output range are identical that it returns an output that matches the
        provided input.
        """
        initial_location = (1, 5, 3)
        initial_input_range = (1, 5)
        initial_output_range = (1, 5)
        sensor_under_test = LightSensor(initial_location, initial_input_range, \
        initial_output_range)

        incident_light = 4
        self.assertEqual(sensor_under_test.output_from_sources(incident_light), \
        incident_light)
Exemplo n.º 4
0
    def testTranslationAndRotationWithSensor(self):
        """
        Confirm that a SensorMount with a single sensor properly updates its
        Sensor's location when the SensorMount is both rotated and translated.
        """
        mount_under_test = SensorMount()

        sensor_location = (100, 0, 0)
        attached_sensor = LightSensor(sensor_location, (), ())

        mount_location = (2, 5, 7)

        """
        We calculate the expected result manually as
        expected X = (100 * cos(32) + y * sin(32)) + 2
                                rotation             translation
        expected Y = (-100 * sin(32) + y * cos(32)) + 5
                                rotation              translation
        expected Z = z + 7

        Where all trig functions are using degrees,
        x = 100
        y = 0
        z = 0
        """
        resultant_location = (86.804809616, -47.991926423, 7)
        mount_under_test.add_new_sensor(attached_sensor)
        mount_under_test.move_to_position(mount_location, 32)
        self.assertAlmostEqual(mount_under_test.get_sensor(0).current_position()[0],\
            resultant_location[0], 6)
        self.assertAlmostEqual(mount_under_test.get_sensor(0).current_position()[1],\
            resultant_location[1], 6)
        self.assertAlmostEqual(mount_under_test.get_sensor(0).current_position()[2],\
            resultant_location[2], 6)
Exemplo n.º 5
0
 def loadSettings(self):
     exist = os.path.isfile("settings.json")
     if exist:
         with open("settings.json") as f:
             self.__items = json.load(f)
             if "ledStrips" in self.__items:
                 ledStripsLen = len(self.__items["ledStrips"])
                 if (ledStripsLen > 0):
                     for ledStrip in self.__items["ledStrips"]:
                         ledSetting = LedStripSettings(ledStrip["redPin"],ledStrip["greenPin"],ledStrip["bluePin"])
                         ledSetting.currentColor = ledStrip["currentColor"]
                         ledSetting.currentIntensity = ledStrip["currentIntensity"]
                         ledSetting.name = ledStrip["name"]
                         ledSetting.id = self.getValue(ledStrip,"id") 
                         led = LedStrip(ledSetting)
                         self.__ledStripsSettings.append(ledSetting)
                         self.__ledStrips.append(led)
                 print("Ledstrip settings loaded...")
             if "lightSensor" in self.__items:
                 self.__lightSensor = LightSensor(int(self.__items["lightSensor"]["pin"]))
                 print("Light sensor settings loaded")
             if "motionSensor" in self.__items:
                 self.__motionSensor = MotionSensor(int(self.__items["motionSensor"]["pin"]))
                 self.__motionSensor.movementThreshold = int(self.__items["motionSensor"]["movementThreshold"])
                 self.__motionSensor.readingDelay = int(self.__items["motionSensor"]["readingDelay"])
                 print("motion sensor settings loaded")
Exemplo n.º 6
0
    def __init__(self, ip_address, port, name, config):
        self.client = Client(ip_address, port, timeout=1)
        self.name = name

        self.ledstrip = None
        self.temperature_sensor = None
        self.humidity_sensor = None
        self.light_sensor = None
        self.motion_sensor = None
        self.light_control = None

        for node in self.client.root.get_children():
            if node.get_name() == 'LEDSTRIP':
                self.ledstrip = LedStrip(node)
            elif node.get_name() == 'HTU':
                self.temperature_sensor = TemperatureSensor(node)
                self.humidity_sensor = HumiditySensor(node)
            elif node.get_name() == 'LIGHT':
                self.light_sensor = LightSensor(self.client.root.LIGHT)
            elif node.get_name() == 'MOTION':
                self.motion_sensor = MotionSensor(self.client.root.MOTION)

        if self.ledstrip and self.light_sensor and self.motion_sensor:
            self.light_control = LightControl(config, self.ledstrip,
                                              self.light_sensor,
                                              self.motion_sensor)
Exemplo n.º 7
0
    def testScaledTranslation(self):
        """
        Confirm the common case that when the output range represents a shift
        and a scaling of the input, the LightSensor will return a properly
        translated output for a given input.
        """
        initial_location = (1, 5, 3)
        initial_input_range = (1, 5)
        initial_output_range = (2, 10)
        sensor_under_test = LightSensor(initial_location, initial_input_range, \
        initial_output_range)

        incident_light = 4
        expected_output_light = 8
        self.assertEqual(sensor_under_test.output_from_sources(incident_light), \
        expected_output_light)
Exemplo n.º 8
0
    def testShiftTranslation(self):
        """
        Confirm the case that when the output range reflects an offset from the
        input range that the LightSensor will return a shifted output relative
        to the provided input
        """
        initial_location = (1, 5, 3)
        initial_input_range = (1, 5)
        initial_output_range = (2, 6)
        sensor_under_test = LightSensor(initial_location, initial_input_range, \
        initial_output_range)

        incident_light = 4
        expected_output_light = 5
        self.assertEqual(sensor_under_test.output_from_sources(incident_light), \
        expected_output_light)
Exemplo n.º 9
0
def main():
    ble = bluetooth.BLE()
    ble.active(True)
    ble_light = BLELightSensor(ble)

    light = LightSensor(36)
    light_density = light.value()
    i = 0

    while True:
        # Write every second, notify every 10 seconds.
        i = (i + 1) % 10
        ble_light.set_light(light_density, notify=i == 0)
        print("Light Lux:", light_density)

        light_density = light.value()
        time.sleep_ms(1000)
Exemplo n.º 10
0
    def testMoveAndRotateMountWithMultipleSensors(self):
        """
        Confirm that a SensorMount with a multiple sensors properly updates its
        Sensor's location when the SensorMount is both rotated and translated.
        """
        mount_under_test = SensorMount()

        attached_sensor = []
        attached_sensor.append(LightSensor((100, 0, 0), (), ()))
        attached_sensor.append(LightSensor((70.710678119, 70.710678119, 0), (), ()))
        attached_sensor.append(LightSensor((0, 100, 0), (), ()))
        attached_sensor.append(LightSensor((-70.710678119, 70.710678119, 0), (), ()))
        attached_sensor.append(LightSensor((-100, 0, 0), (), ()))
        attached_sensor.append(LightSensor((-70.710678119, -70.710678119, 0), (), ()))
        attached_sensor.append(LightSensor((0, -100, 0), (), ()))
        attached_sensor.append(LightSensor((70.710678119, -70.710678119, 0), (), ()))

        mount_location = (2, 5, 7)
        resultant_location = []
        """
        Since we are rotating by 45 degress, each sensor should be rotated to the
        position of the previous one before being translated.
        For example: the second element will end up being rotated to (100, 0, 0)
        then translated by the mount_location (2, 5, 7) to become (102, 5, 7)
        """
        resultant_location.append((72.710678119, -65.710678119, 7))
        resultant_location.append((102, 5, 7))
        resultant_location.append((72.710678119, 75.710678119, 7))
        resultant_location.append((2, 105, 7))
        resultant_location.append((-68.710678119, 75.710678119, 7))
        resultant_location.append((-98, 5, 7))
        resultant_location.append((-68.710678119, -65.710678119, 7))
        resultant_location.append((72.710678119, -65.710678119, 7))

        mount_under_test.add_new_sensor(attached_sensor[0])
        mount_under_test.add_new_sensor(attached_sensor[1])
        mount_under_test.add_new_sensor(attached_sensor[2])
        mount_under_test.add_new_sensor(attached_sensor[3])
        mount_under_test.add_new_sensor(attached_sensor[4])
        mount_under_test.add_new_sensor(attached_sensor[5])
        mount_under_test.add_new_sensor(attached_sensor[6])
        mount_under_test.add_new_sensor(attached_sensor[7])

        mount_under_test.move_to_position(mount_location, 45)
        for i in range(0, 7):
            self.assertAlmostEqual(mount_under_test.get_sensor(i).current_position()[0],\
                resultant_location[i][0], 6)
            self.assertAlmostEqual(mount_under_test.get_sensor(i).current_position()[1],\
                resultant_location[i][1], 6)
            self.assertAlmostEqual(mount_under_test.get_sensor(i).current_position()[2],\
                resultant_location[i][2], 6)
Exemplo n.º 11
0
    def testSimpleTranslationWithSensor(self):
        """
        Confirm that a SensorMount with a single sensor properly translates its
        Sensor object's location when it is moved without a rotation
        """
        mount_under_test = SensorMount()

        sensor_location = (1, 0, 0)
        attached_sensor = LightSensor(sensor_location, (), ())

        mount_location = (1, 2, 3)
        mount_under_test.add_new_sensor(attached_sensor)
        mount_under_test.move_to_position(mount_location, 0)
        self.assertEqual(mount_under_test.get_sensor(0).current_position(), (2, 2, 3))
Exemplo n.º 12
0
    def test270DegreeRotation(self):
        """
        Confirm that a SensorMount with a single sensor properly rotates its
        Sensor object's location when it is rotated 270 degrees without a
        translation.
        """
        mount_under_test = SensorMount()

        sensor_location = (1, 0, 0)
        attached_sensor = LightSensor(sensor_location, (), ())

        mount_location = (0, 0, 0)
        rotated_location = (0, 1, 0)
        mount_under_test.add_new_sensor(attached_sensor)
        mount_under_test.move_to_position(mount_location, 270)
        self.assertAlmostEqual(mount_under_test.get_sensor(0).current_position()[0],\
            rotated_location[0], 6)
        self.assertAlmostEqual(mount_under_test.get_sensor(0).current_position()[1],\
            rotated_location[1], 6)
        self.assertAlmostEqual(mount_under_test.get_sensor(0).current_position()[2],\
            rotated_location[2], 6)
Exemplo n.º 13
0
        # Initialize WebApp
        self.webApp = WebApp(self)

        self.humidity = 100
        self.temp = 100
        self.level = 100
        self.brightness = 100


if __name__ == '__main__':

    ib = IntelligenterBlumentopf()
    tempHumiditySensor = TempHumiditySensor()
    remotePowerSupplyController = RemotePowerSupplyController()
    groundHumiditySensor = GroundHumiditySensor()
    lightSensor = LightSensor()
    levelSensor = LevelSensor()
    sun = sun(lat=ib.coordsLat, long=ib.coordsLong)
    csvData = CSVData()

    ib.logger.debug('IntelligenterBlumentopf is up and running...')
    ib.logger.debug('self.criticalHumidity:' + str(ib.criticalHumidity))
    ib.logger.debug('self.criticalBrightness:' + str(ib.criticalBrightness))
    ib.logger.debug('self.checkSensorsInterval:' +
                    str(ib.checkSensorsInterval))
    ib.logger.debug('self.additionalLightingDuration:' +
                    str(ib.additionalLightingDuration))

    while True:

        #check sensors
def run_model():
    # Put a light-bulb 10 feet high in the center of the room
    light_source_location = (0, 0, 304)
    light_source_intensity_lux = 500
    source = LightSource(light_source_location, light_source_intensity_lux)

    # Initialize Sensors centered around the center of the room in the pattern
    # we want our array to use
    sensor_array = []
    sensor_input_range = (0, 304)
    sensor_output_range = (0, 1023)
    sensor_array.append(LightSensor((0, 30, 10),  \
        sensor_input_range, sensor_output_range))

    sensor_array.append(LightSensor((30 * math.sqrt(2)/2, \
        30 * math.sqrt(2)/2, 10),  sensor_input_range, \
        sensor_output_range))

    sensor_array.append(LightSensor((30, 0, 10), \
        sensor_input_range, sensor_output_range))

    sensor_array.append(LightSensor((30 * math.sqrt(2)/2, \
        -30 * math.sqrt(2)/2, 10), \
         sensor_input_range, sensor_output_range))

    sensor_array.append(LightSensor((0, -30, 10), \
        sensor_input_range, sensor_output_range))

    sensor_array.append(LightSensor((-30 * math.sqrt(2)/2, \
        -30 * math.sqrt(2)/2, 10), \
        sensor_input_range, sensor_output_range))

    sensor_array.append(LightSensor((-30, 0, 10), \
        sensor_input_range, sensor_output_range))

    sensor_array.append(LightSensor((-30 * math.sqrt(2)/2, \
        30 * math.sqrt(2)/2, 10), \
        sensor_input_range, sensor_output_range))

    # Initialize our sensor mount and attach all our sensors
    cart = SensorMount()
    for sensor in sensor_array:
        cart.add_new_sensor(sensor)

    # Move the mount to somewhere away from just below the light
    cart.move_to_position((60, 0, 0), 0)

    #Initialize DirectionFinder to be evaluated
    direction_finder = DirectionFinder()

    # Keep iterating on getting the next direction and moving until we either
    # stabilize or we have tried more than the maximum specified times
    for iteration in range(1, 25):
        # Get all the sensor location and outputs to feed into the DF
        current_sensor_data = []
        cart_location = cart.current_position()
        print(str(iteration) + ": Cart Location: " + str(cart_location))

        for sensor_index in range(0, 7):
            current_sensor = cart.get_sensor(sensor_index)
            global_sensor_location = current_sensor.current_position()

            incident_light = source.get_intensity_at_location(
                global_sensor_location)
            measured_light = current_sensor.output_from_sources(incident_light)
            relative_sensor_location =  (global_sensor_location[0] - cart_location[0], \
                global_sensor_location[1] - cart_location[1], \
                cart_location[2])
            sensor_element = {
                'amp': measured_light,
                'location': relative_sensor_location
            }
            current_sensor_data.append(sensor_element)

        next_move = direction_finder.FindDirection(current_sensor_data)
        print("Next Move: {} ".format(next_move))
        current_sensor_data.clear()

        # We only want to move 5 cm in that direction, so figure out that point
        move_vector = [
            next_move[0] - cart_location[0], next_move[1] - cart_location[1]
        ]
        orig_magnitude = math.sqrt(
            math.pow(move_vector[0], 2) + math.pow(move_vector[1], 2))
        scaled_move = [0, 0]
        if orig_magnitude != 0.0:
            scaled_move = [
                5 * (move_vector[0]) / orig_magnitude,
                5 * (move_vector[1]) / orig_magnitude
            ]

        print("Scaled Move: {}".format(scaled_move))

        # Calculate the translation the move represents
        next_translation = ( cart_location[0] + scaled_move[0], \
            cart_location[1] + scaled_move[1], \
            0)

        # Create a csv file to hold the data generated
        with open('DirectionFinderResults.csv', mode='a') as df_results:
            df_results = csv.writer(df_results,
                                    delimiter=',',
                                    quotechar='"',
                                    quoting=csv.QUOTE_MINIMAL)
            df_results.writerow([
                cart.current_position()[0],
                cart.current_position()[1], scaled_move[0], scaled_move[1],
                math.sqrt(
                    math.pow(next_translation[0], 2) +
                    math.pow(next_translation[1], 2))
            ])

        # The SensorMount doesn't have a defined front so by convention we'll
        # say that the first sensor attached to it represents the front.
        current_cart_front = cart.get_sensor(0).current_position()
        current_cart_angle = math.atan2(current_cart_front[1],
                                        current_cart_front[2])
        final_cart_angle = math.atan2(next_move[1], next_move[0])
        next_rotation = round(final_cart_angle - current_cart_angle, 2)

        cart.move_to_position(next_translation, next_rotation)

        if scaled_move == [0, 0]:
            print("Reached ideal spot in " + str(iteration) + " iterations")
            return

        print("   Current Error: " + str(math.sqrt(math.pow(next_translation[0], 2) \
            + math.pow(next_translation[1], 2))))