def test_update_board_config(self):
        # add board configuration
        mlc_repo = self.__get_new_repo()
        board_id = mlc_repo.save_board_configuration(
            board_config=MLCRepositoryTest.test_board_config)

        # update board configuration
        new_board_config = ProtocolConfig(connection=None,
                                          board_type=Due,
                                          report_mode=REPORT_MODES.AVERAGE,
                                          read_count=2000,
                                          read_delay=3000,
                                          analog_resolution=10)
        updated_board_id = mlc_repo.save_board_configuration(
            board_config=new_board_config, board_id=board_id)
        self.assertEqual(updated_board_id, board_id)

        # load board configuration and check values
        board_config = mlc_repo.load_board_configuration(updated_board_id)
        self.assertEqual(board_config.board_type, new_board_config.board_type)
        self.assertEqual(board_config.report_mode,
                         new_board_config.report_mode)
        self.assertEqual(board_config.read_count, new_board_config.read_count)
        self.assertEqual(board_config.read_delay, new_board_config.read_delay)
        self.assertEqual(board_config.analog_resolution,
                         new_board_config.analog_resolution)
Exemple #2
0
    def test_update_board_configuration(self):
        try:
            mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
            mlc.new_experiment(MLCWorkspaceTest.NEW_EXPERIMENT, MLCWorkspaceTest.NEW_CONFIGURATION)
            mlc.open_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)

            board_configuration, serial_connection = mlc.get_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT)

            new_board_config = ProtocolConfig(None, report_mode=12, read_count=10, read_delay=11, board_type=boards.Leonardo, analog_resolution=13)
            new_connection = SerialConnectionConfig("/dev/ttyACM1", baudrate=5000, parity="P", stopbits=2, bytesize=6)
            mlc.save_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT, new_board_config, new_connection)

            loaded_board_configuration, loaded_connection = mlc.get_board_configuration(MLCWorkspaceTest.NEW_EXPERIMENT)


            self.assertEqual(loaded_board_configuration.board_type, boards.Leonardo)
            self.assertEqual(loaded_board_configuration.read_count, 10)
            self.assertEqual(loaded_board_configuration.read_delay, 11)
            self.assertEqual(loaded_board_configuration.report_mode, 12)
            self.assertEqual(loaded_board_configuration.analog_resolution, 13)

            self.assertEqual(loaded_connection.port, "/dev/ttyACM1")
            self.assertEqual(loaded_connection.baudrate, 5000)
            self.assertEqual(loaded_connection.parity, "P")
            self.assertEqual(loaded_connection.stopbits, 2)
            self.assertEqual(loaded_connection.bytesize, 6)

        finally:
            # FIXME: use Setup/TearDown testcase
            mlc.delete_experiment(MLCWorkspaceTest.NEW_EXPERIMENT)
 def conn_check(self, conn):
     try:
         config = ProtocolConfig(conn)
         arduino_if = init_interface(config)
         version = arduino_if.get_version()
         self.__connection_status.set_ok()
     except ConnectionTimeoutException:
         self.__connection_status.set_error("Error: connection timeout")
     except ConnectionException:
         self.__connection_status.set_error("Error: Board unreachable")
     except ValueError, err:
         self.__connection_status.set_error("Error: {0}".format(err))
 def __create_board_config(self,
                           digital_input_pins=[],
                           digital_output_pins=[],
                           analog_input_pins=[],
                           analog_output_pins=[],
                           pwm_pins=[]):
     return ProtocolConfig(connection=None,
                           board_type=Mega,
                           report_mode=REPORT_MODES.BULK,
                           read_count=1000,
                           read_delay=2000,
                           analog_resolution=12,
                           digital_input_pins=digital_input_pins,
                           digital_output_pins=digital_output_pins,
                           analog_input_pins=analog_input_pins,
                           analog_output_pins=analog_output_pins,
                           pwm_pins=pwm_pins)
    def load_board_configuration(self, board_id):
        protocol = None
        conn = self.__get_db_connection()
        cursor = conn.execute(stmt_get_board(board_id))

        for row in cursor:
            board_type = filter(lambda x: x["SHORT_NAME"] == row[0], types)

            protocol = ProtocolConfig(connection=None,
                                      board_type=board_type[0],
                                      report_mode=row[1],
                                      read_count=row[2],
                                      read_delay=row[3],
                                      analog_resolution=row[4])
            break

        if protocol is None:
            raise KeyError("Board %s dows not exists" % board_id)

        # load pins
        input_pins, output_pins = self.__get_pins(cursor, stmt_get_analog_pins,
                                                  board_id)
        protocol.analog_input_pins.extend(input_pins)
        protocol.analog_output_pins.extend(output_pins)

        input_pins, output_pins = self.__get_pins(cursor,
                                                  stmt_get_digital_pins,
                                                  board_id)
        protocol.digital_input_pins.extend(input_pins)
        protocol.digital_output_pins.extend(output_pins)

        for row in cursor.execute(stmt_get_pwm_pins(board_id)):
            protocol.pwm_pins.append(row[0])

        cursor.close()

        return protocol
Exemple #6
0
from ArduinoBoardManager import ArduinoBoardManager


def showPinout():
    dialog = ArduinoBoardDialog("images/uno.jpg")
    dialog.exec_()


#   window = QDialog()
#   ui = Ui_BoardPinout()
#   ui.setupUi(window)
#   window.exec_()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    # window = BoardConfigurationWindow()
    protocol_cfg = ProtocolConfig(None)
    connection_cfg = SerialConnectionConfig('/dev/ttyACM0')
    manager = ArduinoBoardManager(protocol_cfg, connection_cfg)
    # ui = Ui_BoardConfigurationFrame()
    # ui.setupUi(window)
    # ui.showPinout.clicked.connect(showPinout)

    manager.start()
    app.exec_()

    manager = ArduinoBoardManager(protocol_cfg, connection_cfg)
    manager.start()
    app.exec_()
class MLCRepositoryTest(unittest.TestCase):
    WORKSPACE_DIR = os.path.abspath("/tmp/")
    EXPERIMENT_NAME = "test_mlc_repository"

    test_board_config = ProtocolConfig(connection=None,
                                       board_type=Mega,
                                       report_mode=REPORT_MODES.BULK,
                                       read_count=1000,
                                       read_delay=2000,
                                       analog_resolution=12)

    def __create_board_config(self,
                              digital_input_pins=[],
                              digital_output_pins=[],
                              analog_input_pins=[],
                              analog_output_pins=[],
                              pwm_pins=[]):
        return ProtocolConfig(connection=None,
                              board_type=Mega,
                              report_mode=REPORT_MODES.BULK,
                              read_count=1000,
                              read_delay=2000,
                              analog_resolution=12,
                              digital_input_pins=digital_input_pins,
                              digital_output_pins=digital_output_pins,
                              analog_input_pins=analog_input_pins,
                              analog_output_pins=analog_output_pins,
                              pwm_pins=pwm_pins)

    def __create_serial_connection(self):
        return SerialConnectionConfig(baudrate=1,
                                      parity=2,
                                      stopbits=3,
                                      bytesize=4,
                                      port=5)

    @classmethod
    def setUpClass(cls):
        Config._instance = Config.from_dictionary({
            "BEHAVIOUR": {
                "save": "false"
            },
            "POPULATION": {
                "size": "3",
                "range": 0,
                "precision": 0
            },
            "OPTIMIZATION": {
                "probrep": 0,
                "probmut": 0,
                "probcro": 0,
                "cascade": "1,1",
                "simplify": "false"
            }
        })

        set_working_directory(MLCRepositoryTest.WORKSPACE_DIR)
        if not os.path.exists(MLCRepositoryTest.WORKSPACE_DIR):
            os.makedirs(MLCRepositoryTest.WORKSPACE_DIR)

        os.makedirs(
            os.path.join(MLCRepositoryTest.WORKSPACE_DIR,
                         MLCRepositoryTest.EXPERIMENT_NAME))

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(
            os.path.join(MLCRepositoryTest.WORKSPACE_DIR,
                         MLCRepositoryTest.EXPERIMENT_NAME))

    def __get_new_repo(self):
        MLCRepository._instance = None
        MLCRepository.make("test_mlc_repository")
        return MLCRepository.get_instance()

    def test_add_individual(self):
        mlc_repo = self.__get_new_repo()

        # mlc repository is empty
        self.assertEqual(mlc_repo.count_individual(), 0)

        # add the first individual
        indiv_id, exists = mlc_repo.add_individual(
            Individual("(root (+ 1 1))"))
        self.assertEqual(indiv_id, 1)
        self.assertFalse(exists)
        self.assertEqual(mlc_repo.count_individual(), 1)

        # trying to add an Individual with the same value
        indiv_id, exists = mlc_repo.add_individual(
            Individual("(root (+ 1 1))"))
        self.assertEqual(indiv_id, 1)
        self.assertTrue(exists)
        self.assertEqual(mlc_repo.count_individual(), 1)

        # adds another individual
        indiv_id, exists = mlc_repo.add_individual(
            Individual("(root (+ 2 2))"))
        self.assertEqual(indiv_id, 2)
        self.assertFalse(exists)
        self.assertEqual(mlc_repo.count_individual(), 2)

    def test_get_individuals(self):
        mlc_repo = self.__get_new_repo()
        mlc_repo.add_individual(Individual("(root (+ 1 1))"))
        mlc_repo.add_individual(Individual("(root (+ 2 2))"))

        # get individuals
        individual = mlc_repo.get_individual(1)
        self.assertEqual(individual.get_value(), "(root (+ 1 1))")

        individual = mlc_repo.get_individual(2)
        self.assertEqual(individual.get_value(), "(root (+ 2 2))")

        # get individual data
        data = mlc_repo.get_individual_data(1)
        self.assertEqual(data.get_appearances(), 0)
        self.assertEqual(data.get_value(), "(root (+ 1 1))")
        self.assertEqual(data.get_cost_history(), {})

        data = mlc_repo.get_individual_data(2)
        self.assertEqual(data.get_appearances(), 0)
        self.assertEqual(data.get_value(), "(root (+ 2 2))")
        self.assertEqual(data.get_cost_history(), {})

        # invalid id
        try:
            individual = mlc_repo.get_individual(3)
            self.assertTrue(False)
        except KeyError:
            self.assertTrue(True)

    def test_add_population(self):
        mlc_repo = self.__get_new_repo()

        mlc_repo.add_individual(Individual("(root (+ 1 1))"))
        mlc_repo.add_individual(Individual("(root (+ 2 2))"))
        mlc_repo.add_individual(Individual("(root (+ 3 3))"))
        self.assertEqual(mlc_repo.count_individual(), 3)

        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [1, 2, 3]
        p._costs = [4, 5, 6]
        p._ev_time = [7, 8, 9]
        p._gen_method = [10, 11, 12]

        # add population to the mlc_repository
        self.assertEqual(mlc_repo.count_population(), 0)
        mlc_repo.add_population(p)
        self.assertEqual(mlc_repo.count_population(), 1)

        # obtain population
        p_from_repo = mlc_repo.get_population(1)

        # check population content
        self.assertEqual(p_from_repo._individuals, p._individuals)
        self.assertEqual(p_from_repo._costs, p._costs)
        self.assertEqual(p_from_repo._ev_time, p._ev_time)
        self.assertEqual(p_from_repo._gen_method, p._gen_method)

    def test_add_population_with_invalid_individual(self):
        mlc_repo = self.__get_new_repo()

        # add population with invalid individual
        p = Population(1, 0, Config.get_instance(), mlc_repo)
        p._individuals = [100]

        try:
            mlc_repo.add_population(p)
            self.assertTrue(False)
        except KeyError:
            self.assertTrue(True)

    def test_get_individual_data(self):
        mlc_repo = self.__get_new_repo()

        # add individuals
        mlc_repo.add_individual(Individual("(root (+ 1 1))"))
        mlc_repo.add_individual(Individual("(root (+ 2 2))"))
        mlc_repo.add_individual(Individual("(root (+ 3 3))"))
        mlc_repo.add_individual(Individual("(root (+ 4 4))"))

        # first population
        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [1, 2, 1]
        p._costs = [4, 5, 6]
        p._ev_time = [5, 6, 7]
        mlc_repo.add_population(p)

        # second population
        p = Population(3, 0, Config.get_instance(), mlc_repo)
        p._individuals = [3, 4, 2]
        p._costs = [7, 4, 9]
        p._ev_time = [8, 5, 10]

        mlc_repo.add_population(p)

        # check idividuals data loaded from the mlc_repo
        self.assertEqual(mlc_repo.count_population(), 2)

        # Individual 1 have two appearances in the first generation
        data = mlc_repo.get_individual_data(1)
        self.assertEqual(data.get_value(), "(root (+ 1 1))")
        self.assertEqual(data.get_appearances(), 2)
        self.assertEqual(data.get_cost_history(), {1: [(4.0, 5), (6.0, 7)]})

        # Individual 2 have two appearances
        data = mlc_repo.get_individual_data(2)
        self.assertEqual(data.get_value(), "(root (+ 2 2))")
        self.assertEqual(data.get_appearances(), 2)
        self.assertEqual(data.get_cost_history(), {
            1: [(5.0, 6)],
            2: [(9.0, 10)]
        })

        # Individual 3 have one appearances
        data = mlc_repo.get_individual_data(3)
        self.assertEqual(data.get_value(), "(root (+ 3 3))")
        self.assertEqual(data.get_appearances(), 1)
        self.assertEqual(data.get_cost_history(), {2: [(7.0, 8)]})

        # Individual 4 have one appearances
        data = mlc_repo.get_individual_data(4)
        self.assertEqual(data.get_value(), "(root (+ 4 4))")
        self.assertEqual(data.get_appearances(), 1)
        self.assertEqual(data.get_cost_history(), {2: [(4.0, 5)]})

        # get individual data from invalid individual
        try:
            data = mlc_repo.get_individual_data(100)
            self.assertTrue(False)
        except KeyError, ex:
            self.assertTrue(True)