class SteeringController():
    def __init__(self):
        # Set control mode
        # 0 = step mode (target position)
        # 1 = run mode (target velocity)
        self.control_mode = 1
        self.velocity = 0  # rad/s, starting velocity
        self.angle = 0  # current steering angle
        self.angle_setpoint = 0  # current steering angle setpoint

        # TODO: makes these ROS parameters
        self.angle_tolerance = 0.02  # stops moving the motor when the angle is within +/- the tolerance of the desired setpoint
        # Max and Min angles to turn of the velocity if they are reached
        self.max_angle = rospy.get_param("/forklift/steering/max_angle",
                                         75 * (math.pi / 180.))
        self.min_angle = rospy.get_param("/forklift/steering/min_angle",
                                         -75 * (math.pi / 180.))
        self.velocity_tolerance = 0.01

        # DEBUG: print max angle values used
        print(
            "[steering_node] Bounding setpoint angles to, Max: {0:0.3f} ({1:0.3f} deg) Min: {2:0.3f} ({3:0.3f} deg)"
            .format(self.max_angle, self.max_angle * (180 / math.pi),
                    self.min_angle, self.min_angle * (180 / math.pi)))

        self.max_accel_scale = 0.003
        self.max_vel_scale = 0.17

        self.velocity_current = 0  # current velocity from the motor controller
        self.gear = 0  # current gear of the forklift, should only steer if not in neutral (gear = 0)

        #===============================================================#
        # These parameters are used in the stall detection and handling
        #===============================================================#
        # Tuning parameters
        self.max_repeats = 5  # the maximum number of times the motor can be seen as not moving before reseting
        self.ramp_time_vel = 1.5  # number of seconds to ramp up to full velocity again
        self.ramp_time_accel = 1.5  # number of seconds to ramp up to full acceleration again

        # Operation states
        self.moving = False  # indicates whether the motor is currently moving
        self.repeats = 0  # adds the number of times the motor is seen as not moving
        # indicates whether the velocity command is sent as normal or if the
        # ramp-up prodecure should be used.
        # 0 = normal mode
        # 1 = ramp-up mode
        self.operation_mode = 0
        self.ramp_start = time.time()  # time when the ramp up procedure began

        #=========================#
        # Create ROS Node Objects
        #=========================#
        rospy.init_node("steering_node")
        # Specify general parameters
        self.rl_axes = 3
        self.manual_deadman_button = rospy.get_param("~manual_deadman", 4)
        # Indicates whether the deadman switch has been pressed on the joystick
        # It must be pressed in order for the system to be able to start running
        self.manual_deadman_on = False
        self.autonomous_deadman_button = rospy.get_param(
            "~autonomous_deadman", 5)
        self.autonomous_deadman_on = False
        self.timeout = rospy.get_param(
            "~timeout", 1
        )  # number of seconds allowed since the last setpoint message before sending a 0 command
        self.timeout_start = time.time()
        self.scale_angle = rospy.get_param("~scale_angle", 1)
        self.scale_angle = min(self.scale_angle, 1)
        print("Manual deadman button: " + str(self.manual_deadman_button))
        print("Autonomous deadman button: " +
              str(self.autonomous_deadman_button))
        print("Scale angle: " + str(self.scale_angle))

        # Publishers and Subscribers
        rospy.on_shutdown(self.close)  # shuts down the Phidget properly
        self.setpoint_sub = rospy.Subscriber("/steering_node/angle_setpoint",
                                             Float64,
                                             self.setpoint_callback,
                                             queue_size=1)
        self.position_pub = rospy.Publisher("~motor/position",
                                            Float64,
                                            queue_size=10)
        self.velocity_pub = rospy.Publisher("~motor/velocity",
                                            Float64,
                                            queue_size=10)
        self.moving_sub = rospy.Subscriber("/steering_node/motor/is_moving",
                                           Bool,
                                           self.moving_callback,
                                           queue_size=3)
        self.angle_sub = rospy.Subscriber("/steering_node/filtered_angle",
                                          Float64,
                                          self.angle_callback,
                                          queue_size=3)
        self.gear_sub = rospy.Subscriber("/velocity_node/gear",
                                         Int8,
                                         self.gear_callback,
                                         queue_size=3)
        self.joystick_sub = rospy.Subscriber("/joy",
                                             Joy,
                                             self.joystick_callback,
                                             queue_size=1)
        # Run 'spin' loop at 30Hz
        self.rate = rospy.Rate(30)

        #================================#
        # Create phidget stepper channel
        #================================#
        try:
            self.ch = Stepper()
        except PhidgetException as e:
            sys.stderr.write("Runtime Error -> Creating Stepper")
            raise
        except RuntimeError as e:
            sys.stderr.write("Runtime Error -> Creating Stepper")
            raise

        # Serial number of previous phidget that broke
        #self.ch.setDeviceSerialNumber(522972)
        # Current Serial number
        self.ch.setDeviceSerialNumber(522722)
        self.ch.setChannel(0)

        # Set handlers, these are called when certain Phidget events happen
        print("\n--------------------------------------")
        print("Starting up Phidget controller")
        print("* Setting OnAttachHandler...")
        self.ch.setOnAttachHandler(self.onAttachHandler)

        print("* Setting OnDetachHandler...")
        self.ch.setOnDetachHandler(self.onDetachHandler)

        print("* Setting OnErrorHandler...")
        self.ch.setOnErrorHandler(self.onErrorHandler)

        print("* Setting OnPositionChangeHandler...")
        self.ch.setOnPositionChangeHandler(self.onPositionChangeHandler)

        print("* Setting OnVelocityChangeHandler...")
        self.ch.setOnVelocityChangeHandler(self.onVelocityChangeHandler)

        # Attach to Phidget
        print("* Opening and Waiting for Attachment...")
        try:
            self.ch.openWaitForAttachment(1000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, self.ch)
            raise EndProgramSignal("Program Terminated: Open Failed")

        # Set Rescale Factor
        # (pi rad / 180 deg) * (1.8deg/step * (1/16) step) / (Gear Ratio = 26 + (103/121))
        self.rescale_factor = (math.pi / 180.) * (1.8 / 16) / (26. +
                                                               (103. / 121.))
        self.ch.setRescaleFactor(
            self.rescale_factor)  # converts steps to radians

        # Set control mode (You must either uncomment the line below or do not set the control mode at all and leave let it be the default of 0, or "step" mode. Setting self.ch.setControlMode(ControlMode.CONTROL_MODE_STEP) does not work for some reason)
        if (self.control_mode == 1):
            self.ch.setControlMode(ControlMode.CONTROL_MODE_RUN)

        # Define max acceleration and velocity
        self.max_velocity = self.max_vel_scale * self.ch.getMaxVelocityLimit()
        self.max_acceleration = self.max_accel_scale * self.ch.getMaxAcceleration(
        )
        self.ch.setAcceleration(self.max_acceleration)

        #===============#
        # Run main loop
        #===============#
        # self.mainLoop()
        self.spin()

    def onAttachHandler(self, channel):
        ph = channel

        try:
            # Get channel info
            channelClassName = ph.getChannelClassName()
            serialNumber = ph.getDeviceSerialNumber()
            channel_num = ph.getChannel()

            # DEBUG: print channel info
            print("\nAttaching Channel")
            print("* Channel Class: " + channelClassName +
                  "\n* Serial Number: " + str(serialNumber) + "\n* Channel: " +
                  str(channel_num) + "\n")

            # Set data time interval
            interval_time = 32  # ms (this will publish at roughly 30Hz)
            print("Setting DataInterval to %ims" % interval_time)
            try:
                ph.setDataInterval(interval_time)
            except PhidgetException as e:
                sys.stderr.write("Runtime Error -> Setting DataInterval\n")
                return

            # Engage stepper
            print("Engaging Stepper")
            try:
                ph.setEngaged(True)
            except PhidgetException as e:
                sys.stderr.write("Runtime Error -> Setting Engaged\n")
                return

        except PhidgetException as e:
            print("Error in attach event:")
            traceback.print_exc()
            return

    def onDetachHandler(self, channel):
        ph = channel

        try:
            # Get channel info
            channelClassName = ph.getChannelClassName()
            serialNumber = ph.getDeviceSerialNumber()
            channel_num = ph.getChannel()

            # DEBUG: print channel info
            print("\nDetaching Channel")
            print("\n\t-> Channel Class: " + channelClassName +
                  "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Channel: " + str(channel_num) + "\n")

        except PhidgetException as e:
            print("\nError in Detach Event:")
            traceback.print_exc()
            return

    def onErrorHandler(self, channel, errorCode, errorString):
        sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" +
                         str(errorCode) + ")\n")

    def onPositionChangeHandler(self, channel, position):
        self.position_pub.publish(Float64(position))

    def onVelocityChangeHandler(self, channel, velocity):
        self.velocity_current = velocity
        self.velocity_pub.publish(Float64(velocity))

    def close(self):
        print("\n" + 30 * "-")
        print("Closing down Phidget controller")
        self.ch.setOnPositionChangeHandler(None)
        print("Cleaning up...")
        self.ch.close()
        print("Exiting...")
        return 0

    def spin(self):
        while not rospy.is_shutdown():
            self.control_loop()
            self.rate.sleep()

    #===================================#
    # Velocity Set Function
    # * change this function whenever you use a different source for setting the
    # * velocity besides the controller.
    #===================================#
    def control_loop(self):
        # Check if deadman switch is pressed
        if ((self.manual_deadman_on or self.autonomous_deadman_on)
                and (time.time() - self.timeout_start) < self.timeout
                and (self.gear != 0)):
            # FIXME: Uncomment this line if you are able to test it. Also uncomment the corresponding line in the 'else' condition.
            # self.ch.setEngaged(True)

            # Determine direction
            error = self.angle_setpoint - self.angle

            if not (error == 0):
                direction = abs(error) / error
            else:
                direction = 1

            if (abs(error) > self.angle_tolerance):
                self.velocity = direction * self.scale_angle * self.max_velocity
            else:
                self.velocity = 0

            #===== Stall Check and Set Velocity =====#
            try:
                # Check if the system is stalled
                if (self.check_stall()):
                    # Initiate "ramp-up" mode
                    self.reset_rampup()

                if (self.operation_mode == 0):
                    # Normal mode
                    self.ch.setVelocityLimit(self.velocity)

                    # Scale down the acceleration as the velocity increases
                    # # (Linear)
                    # accel_vel_scale = (self.max_velocity - abs(self.velocity_current))/(self.max_velocity)
                    # accel_vel_scale = min(accel_vel_scale, 1)
                    # accel_vel_scale = max(accel_vel_scale, 0)

                    # (Inverse)
                    # parameters
                    unchanged_length = 0.75  # increase this value to increase the range where the accelerations remains unreduced
                    final_scale = 10  # increase this value to decrease the final scale value at max velocity
                    try:
                        accel_vel_scale = 1 / (
                            final_scale *
                            (abs(self.velocity_current) / self.max_velocity)**
                            unchanged_length)
                    except ZeroDivisionError:
                        accel_vel_scale = 1
                    accel_vel_scale = min(accel_vel_scale, 1)
                    accel_vel_scale = max(accel_vel_scale, 0)

                    # Set acceleration
                    print("Current velocity: %f, max: %f" %
                          (self.velocity_current, self.max_velocity))
                    print("Accel scale: %f" % accel_vel_scale)
                    self.ch.setAcceleration(accel_vel_scale *
                                            self.max_acceleration)

                else:
                    # Ramp-up mode
                    t_curr = time.time()
                    scale_vel = min(
                        (t_curr - self.ramp_start) / self.ramp_time_vel, 1)**3
                    scale_accel = min(
                        (t_curr - self.ramp_start) / self.ramp_time_accel, 1)
                    scale_accel = max(scale_accel, 0.001)

                    # DEBUG: print accel scaling
                    print("t_curr: %f" % t_curr)
                    print("time diff: %f" % (t_curr - self.ramp_start))
                    print("accel scale: %f" % scale_accel)
                    print("vel scale: %f" % scale_vel)
                    print("Accel: %f" % (scale_accel * self.max_acceleration))
                    print("Vel: %f" % (scale_vel * self.velocity))

                    self.ch.setAcceleration(scale_accel *
                                            self.max_acceleration)
                    self.ch.setVelocityLimit(scale_vel * self.velocity)

                    # When ramping has finished resume normal operation
                    if (scale_vel == 1 and scale_accel == 1):
                        self.operation_mode = 0
            except PhidgetException as e:
                DisplayError(e)
        else:
            self.ch.setVelocityLimit(0)

            # FIXME: Uncomment this line if you are able to test it. Also uncomment the corresponding line in the 'if' condition.
            # self.ch.setEngaged(False)

    def setpoint_callback(self, msg):
        # Read in new setpoint and saturate against the bounds
        self.angle_setpoint = min(msg.data, self.max_angle)
        self.angle_setpoint = max(self.angle_setpoint, self.min_angle)

    def angle_callback(self, msg):
        # Read the current steering angle
        self.angle = msg.data

    def moving_callback(self, msg):
        # Update 'moving' to indicate whether the motor is moving or not
        self.moving = msg.data

    def gear_callback(self, msg):
        # Update gear
        self.gear = msg.data

    def check_stall(self):
        stalled = False
        if (abs(self.ch.getVelocity()) > self.velocity_tolerance
                and self.moving == False):
            self.repeats += 1
            if (self.repeats > self.max_repeats):
                stalled = True
        else:
            self.repeats = 0

        return stalled

    def reset_rampup(self):
        if (self.operation_mode == 0):
            self.ch.setVelocityLimit(0)
        self.ramp_start = time.time()
        self.operation_mode = 1
        self.ch.setAcceleration(self.max_acceleration)

    def joystick_callback(self, msg):
        # Update timeout time
        self.timeout_start = time.time()

        # One of these buttons must be on for this node to send a steering command
        if (msg.buttons[self.manual_deadman_button]):
            self.manual_deadman_on = True
        else:
            self.manual_deadman_on = False

        if (msg.buttons[self.autonomous_deadman_button]):
            self.autonomous_deadman_on = True
        else:
            self.autonomous_deadman_on = False
class SteeringController():
    def __init__(self):
        # Create phidget stepper channel
        try:
            self.ch = Stepper()
        except PhidgetException as e:
            sys.stderr.write("Runtime Error -> Creating Stepper")
            raise
        except RuntimeError as e:
            sys.stderr.write("Runtime Error -> Creating Stepper")
            raise

        self.ch.setDeviceSerialNumber(522972)
        self.ch.setChannel(0)

        # Set handlers
        print("\n--------------------------------------")
        print("\nSetting OnAttachHandler...")
        self.ch.setOnAttachHandler(self.onAttachHandler)

        print("Setting OnDetachHandler...")
        self.ch.setOnDetachHandler(self.onDetachHandler)

        print("Setting OnErrorHandler...")
        self.ch.setOnErrorHandler(self.onErrorHandler)

        print("\nSetting OnPositionChangeHandler...")
        self.ch.setOnPositionChangeHandler(self.onPositionChangeHandler)

        print("\nOpening and Waiting for Attachment...")
        try:
            self.ch.openWaitForAttachment(5000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, self.ch)
            raise EndProgramSignal("Program Terminated: Open Failed")

        # Set Rescale Factor
        # 1.8deg/step * (1/16) step / (Gear Ratio = 26 + (103/121))
        self.ch.setRescaleFactor((1.8 / 16) / (26. + (103. / 121.)))

        # Set control mode
        # self.ch.setControlMode(ControlMode.CONTROL_MODE_STEP)
        #self.ch.setControlMode(ControlMode.CONTROL_MODE_RUN)

        #===== Run main loop
        self.mainLoop()

    def onAttachHandler(self, channel):
        ph = channel

        try:
            # Get channel info
            channelClassName = ph.getChannelClassName()
            serialNumber = ph.getDeviceSerialNumber()
            channel_num = ph.getChannel()

            # DEBUG: print channel info
            print("\nAttaching Channel")
            print("\n\t-> Channel Class: " + channelClassName +
                  "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Channel: " + str(channel_num) + "\n")

            # Set data time interval
            interval_time = 500  # ms
            print("Setting DataInterval to %ims" % interval_time)
            try:
                ph.setDataInterval(interval_time)
            except PhidgetException as e:
                sys.stderr.write("Runtime Error -> Setting DataInterval\n")
                return

            # Engage stepper
            print("Engaging Stepper")
            try:
                ph.setEngaged(True)
            except PhidgetException as e:
                sys.stderr.write("Runtime Error -> Setting Engaged\n")
                return

        except PhidgetException as e:
            print("Error in attach event:")
            traceback.print_exc()
            return

    def onDetachHandler(self, channel):
        ph = channel

        try:
            # Get channel info
            channelClassName = ph.getChannelClassName()
            serialNumber = ph.getDeviceSerialNumber()
            channel_num = ph.getChannel()

            # DEBUG: print channel info
            print("\nDetaching Channel")
            print("\n\t-> Channel Class: " + channelClassName +
                  "\n\t-> Serial Number: " + str(serialNumber) +
                  "\n\t-> Channel: " + str(channel_num) + "\n")

        except PhidgetException as e:
            print("\nError in Detach Event:")
            traceback.print_exc()
            return

    def onErrorHandler(self, channel, errorCode, errorString):
        sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" +
                         str(errorCode) + ")\n")

    def onPositionChangeHandler(self, channel, Position):
        print("[Position Event] -> Position: " + str(Position))

    def mainLoop(self):
        end = False
        while (end != True):
            buf = sys.stdin.readline(100)
            if not buf:
                continue

            if (buf[0] == 'Q' or buf[0] == 'q'):
                end = True
                continue

            try:
                targetPosition = float(buf)
            except ValueError as e:
                print("Input must be a number, or Q to quit.")
                continue

            if (targetPosition > self.ch.getMaxPosition()
                    or targetPosition < self.ch.getMinPosition()):
                print("TargetPosition must be between %.2f and %.2f\n" %
                      (self.ch.getMinPosition(), self.ch.getMaxPosition()))
                continue

            print("Setting Stepper TargetPosition to " + str(targetPosition))
            try:
                self.ch.setTargetPosition(targetPosition)
            except PhidgetException as e:
                DisplayError(e)

            print(self.ch.getTargetPosition())
            print(self.ch.getVelocity())

        self.close()

    def close(self):
        self.ch.setOnPositionChangeHandler(None)
        print("Cleaning up...")
        self.ch.close()
        print("\nExiting...")
        return 0
class SteeringController():
    def __init__(self):
        # Set control mode
        # 0 = step mode (target position)
        # 1 = run mode (target velocity)
        self.control_mode = 1
        self.velocity = 0 # rad/s, starting velocity
        self.angle = 0 # rad, starting angle before joystick command received
        # Max and Min angles to turn of the velocity if they are reached
        self.max_angle = 2*math.pi
        self.min_angle = -2*math.pi

        self.max_accel_scale = 0.01
        self.max_vel_scale = -0.75 # negative value is used to reverse the steering direction, makes right direction on analog stick equal right turn going forward

        #===============================================================#
        # These parameters are used in the stall detection and handling
        #===============================================================#
        # Tuning parameters
        self.max_repeats = 5 # the maximum number of times the motor can be seen as not moving before reseting
        self.ramp_time_vel = 1 # number of seconds to ramp up to full velocity again
        self.ramp_time_accel = 1 # number of seconds to ramp up to full acceleration again

        # Operation states
        self.moving = False # indicates whether the motor is currently moving
        self.repeats = 0 # adds the number of times the motor is seen as not moving
        # indicates whether the velocity command is sent as normal or if the
        # ramp-up prodecure should be used.
        # 0 = normal mode
        # 1 = ramp-up mode
        self.operation_mode = 0
        self.ramp_start = time.time() # time when the ramp up procedure began

        #=========================#
        # Create ROS Node Objects
        #=========================#
        rospy.init_node("steering_controller")
        rospy.on_shutdown(self.close) # shuts down the Phidget properly

        # Specify general parameters
        self.rl_axes = 3
        self.deadman_button = rospy.get_param("~deadman", 4)
        self.scale_angle = rospy.get_param("~scale_angle", 1)
        self.scale_angle = min(self.scale_angle, 1)
        print("Deadman button: " + str(self.deadman_button))
        print("Scale angle: " + str(self.scale_angle))

        self.joy_sub = rospy.Subscriber("/joy", Joy, self.joy_callback, queue_size = 1)
        self.position_pub = rospy.Publisher("~motor_position", Float64, queue_size = 10)
        self.velocity_pub = rospy.Publisher("~motor_velocity", Float64, queue_size = 10)
        self.moving_sub = rospy.Subscriber("/steering_node/motor/is_moving", Bool, self.moving_callback, queue_size = 3)

        #================================#
        # Create phidget stepper channel
        #================================#
        try:
            self.ch = Stepper()
        except PhidgetException as e:
            sys.stderr.write("Runtime Error -> Creating Stepper")
            raise
        except RuntimeError as e:
            sys.stderr.write("Runtime Error -> Creating Stepper")
            raise

        self.ch.setDeviceSerialNumber(522972)
        self.ch.setChannel(0)

        # Set handlers, these are called when certain Phidget events happen
        print("\n--------------------------------------")
        print("Starting up Phidget controller")
        print("* Setting OnAttachHandler...")
        self.ch.setOnAttachHandler(self.onAttachHandler)

        print("* Setting OnDetachHandler...")
        self.ch.setOnDetachHandler(self.onDetachHandler)

        print("* Setting OnErrorHandler...")
        self.ch.setOnErrorHandler(self.onErrorHandler)

        print("* Setting OnPositionChangeHandler...")
        self.ch.setOnPositionChangeHandler(self.onPositionChangeHandler)

        print("* Setting OnVelocityChangeHandler...")
        self.ch.setOnVelocityChangeHandler(self.onVelocityChangeHandler)

        # Attach to Phidget
        print("* Opening and Waiting for Attachment...")
        try:
            self.ch.openWaitForAttachment(5000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, self.ch)
            raise EndProgramSignal("Program Terminated: Open Failed")

        # Set Rescale Factor
        # (pi rad / 180 deg) * (1.8deg/step * (1/16) step) / (Gear Ratio = 26 + (103/121))
        self.rescale_factor = (math.pi/180.)*(1.8/16)/(26.+(103./121.))
        self.ch.setRescaleFactor(self.rescale_factor) # converts steps to radians

        # Set control mode (You must either uncomment the line below or do not set the control mode at all and leave let it be the default of 0, or "step" mode. Setting self.ch.setControlMode(ControlMode.CONTROL_MODE_STEP) does not work for some reason)
        if (self.control_mode == 1):
            self.ch.setControlMode(ControlMode.CONTROL_MODE_RUN)

        # Set acceleration
        self.max_acceleration = self.max_accel_scale*self.ch.getMaxAcceleration()
        self.ch.setAcceleration(self.max_acceleration)

        #===============#
        # Run main loop
        #===============#
        # self.mainLoop()
        rospy.spin()

    def onAttachHandler(self, channel):
        ph = channel

        try:
            # Get channel info
            channelClassName = ph.getChannelClassName()
            serialNumber = ph.getDeviceSerialNumber()
            channel_num = ph.getChannel()

            # DEBUG: print channel info
            print("\nAttaching Channel")
            print("* Channel Class: " + channelClassName + "\n* Serial Number: " + str(serialNumber) + "\n* Channel: " + str(channel_num) + "\n")

            # Set data time interval
            interval_time = 32 # ms (this will publish at roughly 30Hz)
            print("Setting DataInterval to %ims" % interval_time)
            try:
                ph.setDataInterval(interval_time)
            except PhidgetException as e:
                sys.stderr.write("Runtime Error -> Setting DataInterval\n")
                return

            # Engage stepper
            print("Engaging Stepper")
            try:
                ph.setEngaged(True)
            except PhidgetException as e:
                sys.stderr.write("Runtime Error -> Setting Engaged\n")
                return

        except PhidgetException as e:
            print("Error in attach event:")
            traceback.print_exc()
            return

    def onDetachHandler(self, channel):
        ph = channel

        try:
            # Get channel info
            channelClassName = ph.getChannelClassName()
            serialNumber = ph.getDeviceSerialNumber()
            channel_num = ph.getChannel()

            # DEBUG: print channel info
            print("\nDetaching Channel")
            print("\n\t-> Channel Class: " + channelClassName + "\n\t-> Serial Number: " + str(serialNumber) + "\n\t-> Channel: " + str(channel_num) + "\n")

        except PhidgetException as e:
            print("\nError in Detach Event:")
            traceback.print_exc()
            return

    def onErrorHandler(self, channel, errorCode, errorString):
        sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" + str(errorCode) + ")\n")

    def onPositionChangeHandler(self, channel, position):
        self.position_pub.publish(Float64(position))

    def onVelocityChangeHandler(self, channel, velocity):
        self.velocity_pub.publish(Float64(velocity))

    def close(self):
        print("\n" + 30*"-")
        print("Closing down Phidget controller")
        self.ch.setOnPositionChangeHandler(None)
        print("Cleaning up...")
        self.ch.close()
        print("Exiting...")
        return 0

    #===================================#
    # Velocity Set Function
    # * change this function whenever you use a different source for setting the
    # * velocity besides the controller.
    #===================================#
    def joy_callback(self, msg):
        # Check if deadman switch is pressed
        if (msg.buttons[self.deadman_button]):
            # check if the current angle is beyond the bounds
            self.angle = self.ch.getPosition()
            # Read right joystick analog "Left/Right" value
            # (only go to 90% ov maximum velocity so it doesn't stall out)
            self.velocity = self.max_vel_scale*self.scale_angle*msg.axes[self.rl_axes]*self.ch.getMaxVelocityLimit()

            # DEBUG: Print
            # print("Angle: " + str(self.angle))
            # print("Velocity: " + str(self.angle))
            # print("Max: " + str(self.max_angle) + ", Min: " + str(self.min_angle))

            #===== Uses Min/Max =====#
            # if not ((self.angle > self.max_angle and self.velocity > 0) or (self.angle < self.min_angle and self.velocity < 0)):
            #     try:
            #         self.ch.setVelocityLimit(self.velocity)
            #     except PhidgetException as e:
            #         DisplayError(e)
            # else:
            #     try:
            #         self.ch.setVelocityLimit(0)
            #     except PhidgetException as e:
            #         DisplayError(e)

            #===== No Min/Max considered =====#
            try:
                # Check if the system is stalled
                if (self.check_stall()):
                    # Initiate "ramp-up" mode
                    self.reset_rampup()

                if (self.operation_mode == 0):
                    # Normal mode
                    self.ch.setVelocityLimit(self.velocity)
                else:
                    # Ramp-up mode
                    t_curr = time.time()
                    scale_vel = min((t_curr - self.ramp_start)/self.ramp_time_vel, 1)**2
                    scale_accel = min((t_curr - self.ramp_start)/self.ramp_time_accel, 1)
                    self.ch.setAcceleration(self.max_acceleration)
                    self.ch.setVelocityLimit(scale_vel*self.velocity)

                    # When ramping has finished resume normal operation
                    if (scale_vel == 1 and scale_accel == 1):
                        self.operation_mode = 0

            except PhidgetException as e:
                DisplayError(e)

    def moving_callback(self, msg):
        # Update 'moving' to indicate whether the motor is moving or not
        self.moving = msg.data

    def check_stall(self):
        stalled = False
        if (self.ch.getVelocity() != 0 and self.moving == False):
            self.repeats += 1
            if (self.repeats > self.max_repeats):
                stalled = True
        else:
            self.repeats = 0

        return stalled

    def reset_rampup(self):
        self.ramp_start = time.time()
        self.operation_mode = 1
Exemple #4
0
class PhidgetStepper(object):
    r"""The main UI class for the PhidgetStepper1067 interface.
    
    This class contains the ability to communicate wiht the interface and read card data for reference.
    """
    ########################################################################
    #        Initialize/Establish connection with the board                #
    ########################################################################
    def __init__(self):
        r""" The initialization of the card. 
        
        Tries to communicate to the board and returns an error if the card fails to establish.
        """
        
        self.errorvalue = False
        try:
            self.Stepper = Stepper()
            
        except RuntimeError as e:
            print("Runtime Exception: %s" % e.details)
            print("Exiting....")
            sys.exit(1)
        try:
            self.Stepper.setOnAttachHandler(StepperAttached)
            self.Stepper.setOnDetachHandler(StepperDetached)
            self.Stepper.setOnErrorHandler(ErrorEvent)

            self.Stepper.setOnPositionChangeHandler(PositionChangeHandler)

            print("Waiting for the Phidget Stepper Object to be attached...")
            self.Stepper.openWaitForAttachment(3000)

        except PhidgetException as e:
            print("Phidget Exception %i: %s" % (e.code, e.details))
            print("Press Enter to Exit...\n")
            readin = sys.stdin.read(1)
            sys.exit(1)

            

            
    ########################################################################
    #        DISPLAY: displays important information about the steppers    #
    ########################################################################
    # def displayDeviceInfo(self):
    #     r""" Displays card data. 
        
    #     If card is succesfully initialized, the data on the card is dsiplayed in the terminal.
    #     """
    #     print("|------------|----------------------------------|--------------|------------|")
    #     print("|- Attached -|-              Type              -|- Serial No. -|-  Version -|")
    #     print("|------------|----------------------------------|--------------|------------|")
    #     print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (self.Stepper.isAttached(), self.Stepper.getDeviceName(), self.Stepper.getSerialNum(), self.Stepper.getDeviceVersion()))
    #     print("|------------|----------------------------------|--------------|------------|")
    #     print("Number of Motors: %i" % (self.Stepper.getMotorCount()))
        
        
    ########################################################################
    #        SETUP: sets up the velocity and acceleration limits           #
    ########################################################################
    def stepperSetup(self, Velocity_limit_mm, Acceleration_limit_mm, Current_limit):
        """
        Set up the input parameters of the Stepper Motor.
        
        Selection of the velocity, acceleration, and current limits for the stepper.
            Args:
                Velocity_limit_mm (float): The maximum velocity of the sled in mm/sec. Optimal setting is ~15-20 mm/sec.
                Acceleration_limit_mm (float): The maximum acceleration of the sled in mm/sec^2. Optimal setting is ~50-70 mm/sec^2.
                Current_limit (float, [0-4]): The current limit (in Amps) delivered to the motor.                
            Returns:
                None            
            Raises:
                AttributeError: None
        """
        self.setEngaged(False)
        
        #VELOCITY: make sure the velocity input isnt out of bounds
        self.setVelocity(Velocity_limit_mm)
            
        #ACCELERATION: make sure the acceleration isnt out of bounds
        self.setAcceleration(Acceleration_limit_mm)
        #CURRENT: limits the amount of current supplied to the board
        self.setCurrentLimit(Current_limit)
        
        # Initialize the Stepper Position
        current_position = self.getCurrentPosition()
        self.setTargetPosition(current_position)
        self.setEngaged(False)
        
    ########################################################################   
    # GETTERS: Get all values relevant to the motor in form self.getter    #
    ########################################################################
    def getCurrentLimit(self):
        """
        Returns the Current Limit for the stepper motor.
        
            Args:
                None
            Returns:
                currentLimit (float): will be a value [0, 4].
            Raises:
                AttributeError: None
        """
        currentLimit = self.Stepper.getCurrentLimit(0)   
        return currentLimit
    def getVelocityLimit(self):
        """
        Returns the Velocity Limit for the stepper motor, essentially the upper bound for velocity during any operation of the stepper motor. (Not to be confused with self.getVelocity() which returns the current velocity of the motor (0 if not moving))
        
            Args:
                None
            Returns:
                velocityLimit (float): The velocity limit value will be in mm/sec.
            Raises:
                AttributeError: None
        """
        velocityLimit = self.Stepper.getVelocityLimit(0)*self.getConversionFactor()
        return velocityLimit
    def getVelocity(self):
        """
        Returns the current Velocity of the stepper motor. (Not to be confused with self.getVelocityLimit() which returns the upper bound for the velocity of the motor)
        
            Args:
                None
            Returns:
                velocity (float): The current velocity value will be in mm/sec.
            Raises:
                AttributeError: None
        """        
        velocity = self.Stepper.getVelocity(0)*self.getConversionFactor()
        return velocity
    def getAcceleration(self):
        """
        Returns the Acceleration limit of the stepper motor. 
        
            Args:
                None
            Returns:
                acceleration (float): The current acceleraion value will be in mm/sec^2.
            Raises:
                AttributeError: None
        """         
        acceleration = self.Stepper.getAcceleration(0)*self.getConversionFactor()
        return acceleration
    def getCurrentPosition(self):
        """
        Returns the current Position of the stepper motor. 
        
            Args:
                None
            Returns:
                currentPosition (float): The current position value will be in mm. Referenced from the zero value, wherever it is defined to be.
            Raises:
                AttributeError: None
        """           
        currentPosition = self.Stepper.getCurrentPosition(0)*self.getConversionFactor()
        return currentPosition
    def getConversionFactor(self):
        """
        Returns the Conversion Factor for the stepper motor/threaded rod combo. If those two things are changed, conversion factor wil change.
        
            Args:
                None
            Returns:
                conversionFactor (float): The conversion factor value will be in mm/steps.
            Raises:
                AttributeError: None
        """   
        conversionFactor = self.setConversionFactor() 
        return conversionFactor 
        
    ########################################################################
    #    SETTERS: Set all values for motor in form self.setter(value)      #
    ########################################################################
    def setEngaged(self, state): #state is a True or False Boolean
        """
        Sets the state (connectedness) of the stepper motor. 
        
            Args:
                State (bool): True means that the stepper will be engaged, False means it will be disengaged.
            Returns:
                None.
            Raises:
                AttributeError: None
        """   
        self.Stepper.setEngaged(0, state)
    def setConversionFactor(self, factor=2*16*1.016/(1000*200)):
        """
        Sets the Conversion Factor of the stepper motor. This is used to convert the TPI (Threads Per Inch) into mm for many getFunctions.
        
            Args:
                factor (float): The factor needs to be calculated by hand per rig setup, but only needs to be done once. DefaultValue = 2*16*1.016/(1000*200).
            Returns:
                None.
            Raises:
                AttributeError: None
        """           
        return factor
    def setCurrentLimit(self, current):
        """
        Sets the Current Limit for the stepper motor.
        
            Args:
                current (float): Any value is accepted, but if a value is > maxCurrent or < minCurrent, then 0, 4 are set respectively. Unit is in amps.
            Returns:
                None.
            Raises:
                AttributeError: None
        """
        min_current = self.Stepper.getCurrentMin(0)
        max_current = self.Stepper.getCurrentMax(0)

        if current < min_current:
            self.Stepper.setCurrentLimit(0, min_current)
        elif current > max_current:
            self.Stepper.setCurrentLimit(0, max_current)
        else:
            self.Stepper.setCurrentLimit(0, current)
    def setVelocity(self, velocity):
        """
        Sets the Velocity of the stepper motor. During operation, the velocity of the motor, will not exceed this value.
        
            Args:
                velocity (float): Any value is accepted, but if a value is > maxVelocity or < minVelocity, then minVelocity/maxVelocity are set respectively. Units are in mm/sec.
            Returns:
                None.
            Raises:
                AttributeError: None
        """
        velocity_steps = velocity/self.getConversionFactor() #converts from mm/sec -> 1/16th step/sec     
        max_V = self.Stepper.getVelocityMax(0)
        min_V = self.Stepper.getVelocityMin(0)
        
        if velocity_steps > max_V:
            self.Stepper.setVelocityLimit(0,max_V)
        elif velocity_steps < min_V:
            self.Stepper.setVelocityLimit(0,min_V)
        else:
            self.Stepper.setVelocityLimit(0, int(velocity_steps))    
    def setAcceleration(self, acceleration):
        """
        Sets the Acceleration of the stepper motor. During operation, the acceleration of the motor, will not exceed this value.
        
            Args:
                acceleration (float): Any value is accepted, but if a value is > maxAcceleration or < minAcceleration, then minAcceleration/maxAcceleration are set respectively. Units are in mm/sec^2.
            Returns:
                None.
            Raises:
                AttributeError: None
        """        
        acceleration = acceleration/self.getConversionFactor() #converts from mm/sec -> 1/16th step/sec
        max_A = self.Stepper.getAccelerationMax(0)
        min_A = self.Stepper.getAccelerationMin(0)
           
        if acceleration > max_A:
            self.Stepper.setAcceleration(0,max_A)
        elif acceleration < min_A:
            self.Stepper.setAcceleration(0,min_A)
        else:
            self.Stepper.setAcceleration(0, int(acceleration))           
    def setCurrentPosition(self, position):
        """
        Sets the Current Position of the stepper motor. This is used for "zero"ing out the motor position. Should only be called when setEngaged(False)
        
            Args:
                position (float): Any value is accepted, but if position is < minPosition or > maxPosition, set min/max respectively. Motor will know that it is in that new, set, position.
            Returns:
                None.
            Raises:
                AttributeError: None
        """
        self.setEngaged(False)
        position = position/self.getConversionFactor()
        min_position = 0            
        max_position = self.Stepper.getPositionMax(0)
                
        if position < 0:
            self.Stepper.setCurrentPosition(0, min_position)
        elif position > max_position:
            self.Stepper.setCurrentPosition(0, max_position)
        else:
            self.Stepper.setCurrentPosition(0, int(position))
            
        self.setEngaged(True)
    def setTargetPosition(self, targetPosition):
        """
        Sets the Target Podiyion of the stepper motor. One of the main methods in home and jog. By having the current position be different from the target position, the motor will move.
        
            Args:
                targetPosition (float): Any value is accepted, but if a value is > maxVelocity or < minVelocity, then minVelocity/maxVelocity are set respectively. Units are in mm/sec.
            Returns:
                None.
            Raises:
                AttributeError: None
        """
        #Large positive Value will move steppers counterclockwise
        #Large negative value will move steppers clockwise
        #target position is in mm
        targetPosition = targetPosition/self.getConversionFactor()
        self.setEngaged(True)
        self.Stepper.setTargetPosition(0, int(targetPosition))

        
    ########################################################################    
    #                            MOVEMENT FEATURES                         #
    ########################################################################        
        
    def jog(self, mm_interval):
        """
        Jogs the motor a certain distance in a certain direction.

        The motor will jog forward (towards the center) if the mm_interval is positive, and backward (away from center) if the mm_interval is negative.        
            Args:
                mm_interval (float): The distance in mm that you would like to jog the motor.
            Returns:
                None.
            Raises:
                AttributeError: None
        """
        self.setEngaged(True)        
        self.setTargetPosition(self.getCurrentPosition()+mm_interval)
        self.setEngaged(False)
            
    def errorOut(self):
        self.errorvalue = True