def run(self):
     pncLibrary.printTerminalString(self.machine.thread_launch_string,
                                    current_process().name, self.name)
     self.startup_event.set()
     while True:
         #FIXME handle rx_received_time accurately here
         pass
Example #2
0
    def run(self):
        #while self._running:
        pncLibrary.printTerminalString(self.machine.thread_launch_string,
                                       current_process().name, self.name)
        self.startup_event.set()

        while self.synchronizer.t_run_motion_controller_event.is_set():
            if self._running_motion:
                print('length of move queue is ' +
                      str(self.move_queue.qsize()))
                #Prepare for direct control
                #self.machine_controller.waitForSet(self.machine_controller.setCommMode,1,self.machine_controller.getCommMode)

                #There are new moves in the queue, find the one to be executed next
                while not self.move_queue.empty(
                ) and not self.machine.rsh_error:
                    #for move_queue_position in range(self.last_move_serial_number,current_queue_length):
                    move_to_execute = self.move_queue.get()
                    self.current_move_serial_number += 1
                    move_to_execute.serial_number = self.current_move_serial_number

                    #move_to_execute = self.move_queue[self.last_move_serial_number + 1 - 1]
                    print('executing move ' +
                          str(move_to_execute.serial_number))
                    self.move_in_progress = move_to_execute
                    while not self.commandPoints(
                            move_to_execute.servo_tx_array, self.polylines,
                            self.blocklength):
                        if self.machine.rsh_error or not self._running_motion or not self._running_thread:
                            print(
                                'MOTION CONTROLLER: Exiting motion execution')
                            break
                        else:
                            pass
                    self.last_move_serial_number = move_to_execute.serial_number
                    self.move_queue.task_done()
                    #FIXME use data_store_manager thread
                    self.parent.database_push_queue.put(
                        {"EXECUTED_MOVES": move_to_execute})
                    print('done. last_move_serial_number is now ' +
                          str(self.last_move_serial_number))

                #self.machine_controller.setBinaryMode(0)
                self._running_motion = False

                if self.machine.rsh_error:
                    #Clear out move queue
                    print('dumping move queue')
                    self.move_queue = queue.Queue()

            else:
                #thread idle, not running motion
                #print('motion controller thread idling')
                pass

        #Thread signaled to shutdown
        print('MOTION CONTROLLER: thread shut down')
Example #3
0
def openNetworkConnection(machine, control_client_ip, control_client_port):
    try:
        machine.rsh_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        machine.rsh_socket.connect((control_client_ip, control_client_port))
        pncLibrary.printTerminalString(machine.connection_string, machine.name,
                                       control_client_ip, control_client_port)
        return True
    except socket.error:
        pncLibrary.printTerminalString(machine.failed_connection_string,
                                       machine.name, control_client_ip,
                                       control_client_port)
        return False
Example #4
0
    def run(self):
        self.startup_event.set()
        try:
            self.log_file_handle = open(
                self.output_directory +
                datetime.datetime.now().strftime("%Y.%m.%d-%H.%M.%S") + '.txt',
                'w')
            pncLibrary.printTerminalString(self.machine.thread_launch_string,
                                           current_process().name, self.name)
        except Exception as log_open_error:
            print('Log file open error: ' + str(log_open_error))
            return

        while self.synchronizer.t_run_logger_event.is_set():
            if not self.log_queue.empty():
                log_time, log_message = self.log_queue.get()
                self.log_file_handle.write(
                    str(log_time) + ': ' + str(log_message))
                self.log_file_handle.flush()
        self.log_file_handle.close()
Example #5
0
def appInit(type='pocketnc', control_client_ip=-1, control_client_port=-1):
    global pnc_app_manager
    print('Current process: ' + str(multiprocessing.current_process().pid) +
          ', current thread: ' + str(current_thread().name))
    registerProxy('MachineModel', MachineModel, MachineModelProxy,
                  PNCAppManager)
    #registerProxy('Synchronizers', Synchronizers, SynchronizersProxy, PNCAppManager)
    #registerProxy('DatabaseServer', DatabaseServer, DatabaseServerProxy)

    #FIXME ignore SIGINT
    pnc_app_manager = PNCAppManager()  #(mgr_init)
    pnc_app_manager.name = 'pnc_app_manager'
    pnc_app_manager.start()

    #synchronizers = Synchronizers(pnc_app_manager)
    machine = pnc_app_manager.MachineModel()
    synchronizer = pncLibrary.Synchronizer(pnc_app_manager)
    pncLibrary.setTaskRunFlags(synchronizer)
    machine.sculptprint_interface = SculptPrintInterface()
    machine.local_epoch = time.clock()

    #print(machine.manager_launch_string.format(pnc_app_manager.name, str(pnc_app_manager._process.pid)))
    pncLibrary.printTerminalString(machine.manager_launch_string,
                                   pnc_app_manager.name,
                                   pnc_app_manager._process.pid)
    #pncLibrary.printTerminalString(machine.manager_launch_string, pnc_app_manager.name, multiprocessing.current_process().pid)

    #machine.synchronizers = Synchronizers(pnc_app_manager)

    if type == 'simulator':
        control_client_ip = machine.simulator_ip_address
        machine.name = machine.simulator_name
    elif type == 'pocketnc':
        control_client_ip = machine.ip_address
        machine.name = machine.machine_name
    else:
        print('Unrecognized type ' + type +
              ' for machine initialization, aborting')
        return
    control_client_port = machine.tcp_port

    # database_push_queue_proxy = pnc_app_manager.Queue()
    # database_pull_queue_proxy = pnc_app_manager.Queue()
    # database_output_queue_proxy = pnc_app_manager.Queue()
    #testevent = pnc_app_manager.Event()

    database = DatabaseServer(machine, synchronizer)
    database.start()
    pncLibrary.printTerminalString(machine.process_launch_string,
                                   database.name, database.pid,
                                   pnc_app_manager.name,
                                   pnc_app_manager._process.pid)

    encoder_interface = EncoderInterface(machine, synchronizer)
    encoder_interface.start()
    pncLibrary.printTerminalString(machine.process_launch_string,
                                   encoder_interface.name,
                                   encoder_interface.pid, pnc_app_manager.name,
                                   pnc_app_manager._process.pid)

    feedback_handler = MachineFeedbackListener(machine, synchronizer)
    feedback_handler.start()
    pncLibrary.printTerminalString(machine.process_launch_string,
                                   feedback_handler.name, feedback_handler.pid,
                                   pnc_app_manager.name,
                                   pnc_app_manager._process.pid)

    machine_controller = MachineController(machine, synchronizer)
    machine_controller.start()
    pncLibrary.printTerminalString(machine.process_launch_string,
                                   machine_controller.name,
                                   machine_controller.pid,
                                   pnc_app_manager.name,
                                   pnc_app_manager._process.pid)

    openNetworkConnection(machine, control_client_ip, control_client_port)
    waitForAppStart(synchronizer)

    pnc_app_manager.machine = machine
    pnc_app_manager.database = database
    pnc_app_manager.encoder_interface = encoder_interface
    pnc_app_manager.machine_controller = machine_controller
    pnc_app_manager.feedback_handler = feedback_handler

    signalProcessWake(synchronizer)
    pncLibrary.printTerminalString(machine.pncApp_launch_string,
                                   multiprocessing.cpu_count())

    return pnc_app_manager, database, encoder_interface, machine_controller, feedback_handler, synchronizer
Example #6
0
    def run(self):
        current_thread().name = self.main_thread_name
        print('break')
        try:
            if self.serial_port.isOpen():
                print('Serial port already open')
                self.serial_port.close()
                self.serial_port.open()
            else:
                self.serial_port.open()

            time.sleep(0.5)

            if not self.acknowledgeBoot():
                print('Decoder board not responsive')
                return
            else:
                pncLibrary.printTerminalString(self.machine.device_boot_string,
                                               self.device_name,
                                               self.serial_port.name)
            # serial_port.open()
            # time.sleep(0.5)
            #self._running_process = True
        except Exception as error:
            print('Could not open serial port, error: ' + str(error))

        self.synchronizer.ei_startup_event.set()
        self.synchronizer.process_start_signal.wait()
        time.clock()

        #FIXME this logic is not necessary
        if self.serial_port.isOpen():
            #print('Successful launch of encoder interface process, PID: ' + str(self.pid) + ', Process: ' + str(current_process()))
            #global encoderData, serialLock
            #self.setEncoderCount(self.machine.encoder_init)

            #FIXME uncomment for actual run
            # if self.synchronizer.mc_initial_position_set_event.wait() or 1:
            #     self.setAllEncoderCounts(self.machine.axes, self.positionsToCounts(self.machine.axes,list(map(lambda cp,mz: cp-mz,self.machine.current_position,self.machine.machine_zero))))
            #     self.machine.encoder_init_event.set()

            self.setBaudrate(250000)
            self.serial_port.flushInput()

            if self.synchronizer.p_enable_encoder_event.is_set():
                self.synchronizer.p_run_encoder_interface_event.wait()
                while self.synchronizer.p_run_encoder_interface_event.is_set():
                    #FIXME move to getEncoderCounts
                    request_time = time.clock()
                    encoder_counts = self.getEncoderCounts('current')
                    #print('serial read time is: ' + str(time.clock()-rx_received_time))

                    if encoder_counts[0]:
                        #Got good data, push to DB
                        self.encoder_reading_time += (time.clock() -
                                                      request_time)
                        self.encoder_reads += 1
                        self.encoder_read_time_moving_average = self.encoder_reading_time / self.encoder_reads
                        self.machine.average_encoder_read_frequency = 1.0 / self.encoder_read_time_moving_average

                        #FIXME should probably push packets of data instead of every sample
                        encoder_data_record = {
                            'ENCODER_FEEDBACK_POSITIONS':
                            np.asarray(
                                self.countsToPositions(self.machine.axes,
                                                       encoder_counts[1])),
                            'SERIAL_RECEIVED_TIMES':
                            pncLibrary.estimateMachineClock(
                                self.machine, encoder_counts[2])
                        }
                        self.synchronizer.q_database_command_queue_proxy.put(
                            pncLibrary.DatabaseCommand('push',
                                                       encoder_data_record))
                        time.sleep(1)
                        # self.machine.data_store_manager_thread_handle.push(
                        #     {'ENCODER_FEEDBACK_POSITIONS': np.asarray(self.countsToPositions(self.machine.axes, encoder_counts[1])), 'SERIAL_RECEIVED_TIMES': self.machine.estimateMachineClock(rx_received_time)})

            #Flag set, shutdown
            #print('Closing serial port')
            self.serial_port.close()
            print('ENCODER INTERFACE: Serial port closed')

            #self._shutdown = True
        else:
            print('closing serial interface process because port is not open')
            pncLibrary.printTerminalString(
                self.machine.process_terminate_string, self.name, self.pid)
Example #7
0
 def run(self):
     self.startup_event.set()
     pncLibrary.printTerminalString(self.machine.thread_launch_string,
                                    current_process().name, self.name)
     while True:
         pass