def proximitySensorCytronController(self): cyprus.setup_servo(1) # cyprus.set_servo_position(1, 0.5) print("function is working") while True: if cyprus.read_gpio( ) & 0b0010: # HIGH (True) means not detecting metal sleep(1) if cyprus.read_gpio() & 0b0010: print("Metal not detected") cyprus.set_pwm_values( 1, period_value=100000, compare_value=50000, compare_mode=cyprus.LESS_THAN_OR_EQUAL) if not (cyprus.read_gpio() & 0b0010): sleep(1) if not (cyprus.read_gpio() & 0b0010): cyprus.set_pwm_values( 1, period_value=100000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL) print("Metal detected")
def initialize(): ramp.free() ramp.set_speed(3.5) cyprus.initialize() cyprus.setup_servo(2) cyprus.set_motor_speed(1, 0) cyprus.set_servo_position(2, 0)
def initialize(self): print("Home arm and turn off magnet") cyprus.initialize() cyprus.setup_servo(1) cyprus.setup_servo(2) cyprus.set_servo_position(2, .5) cyprus.set_pwm_values(1, period_value=100000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL) self.homeArm()
def cyprus_setup(self): global servo_state servo_state = 0 cyprus.initialize() cyprus.setup_servo(1) cyprus.set_servo_position(1, 0) sleep(0.2) self.start_cyprus_thread()
def ramp_up(self): cyprus.initialize() cyprus.setup_servo(1) for i in range(2, 4, 6, 8, 10): cyprus.set_servo_speed(1, i/10.0) sleep(4) cyprus.set_servo_position(1, 0.5) cyprus.close()
def talon_switch(self): cyprus.initialize() cyprus.setup_servo(1) def isGPIO_P6_HIGH(self): return (cyprus.read_gpio() & 0b0001) == 1 if isGPIO_P6_HIGH(self): cyprus.set_servo_speed(1, 0) else: cyprus.set_servo_speed(1, 1)
def limit_switch(self): cyprus.initialize() cyprus.setup_servo(1) def isGPIO_P6_HIGH(self): return (cyprus.read_gpio() & 0b0001) == 1 if isGPIO_P6_HIGH(self): cyprus.set_servo_position(1, 0) else: cyprus.set_servo_position(1, 1)
def talonDCMotorSpeedUp(self): # cyprus.initialize() cyprus.setup_servo(1) cyprus.set_servo_position(1, 0.5) for i in range(60, 100, 1): cyprus.set_servo_position(1, i / 100.0) sleep(0.5) cyprus.set_servo_position(1, 0.5)
def talon_dc(self): cyprus.initialize() cyprus.setup_servo(1) cyprus.set_servo_speed(1, 1) sleep(1) cyprus.set_servo_speed(1, 0) sleep(5) cyprus.set_servo_speed(1, -1) sleep(1) cyprus.set_servo_speed(1, 0) cyprus.close()
def talonDCMotorFullSpeedWhenPressed(self): # talon motor is in 1 (port 4) cyprus.setup_servo(1) cyprus.set_servo_position(1, 0.5) while True: if cyprus.read_gpio() & 0b0001: sleep(0.05) if cyprus.read_gpio() & 0b0001: cyprus.set_servo_position(1, 0.5) else: cyprus.set_servo_position(1, 1)
def talonDCMotor(self): cyprus.initialize() cyprus.setup_servo(1) cyprus.set_servo_position(1, 1) sleep(5) cyprus.set_servo_position(1, 0.5) sleep(5) cyprus.set_servo_position(1, 0) sleep(5) cyprus.set_servo_position(1, 0.5)
def servoMotorBinaryState(self): cyprus.initialize() cyprus.setup_servo(2) while True: if cyprus.read_gpio() & 0b0001: cyprus.set_servo_position(2, 0) # close gate sleep(1) else: cyprus.set_servo_position(2, .7) # open gate sleep(1)
def servoPressed(self): global pos cyprus.initialize( ) # initialize the RPiMIB and establish communication cyprus.setup_servo( 2) # sets up P4 on the RPiMIB as a RC servo style output if pos: cyprus.set_servo_position(1, 0) pos = False else: cyprus.set_servo_position(1, 1) pos = True
def moveServo(self, param): global servoPos if param == "move": cyprus.setup_servo(1) if servoPos == 1: cyprus.set_servo_position(1, 0) servoPos = 0 else: cyprus.set_servo_position(1, 1) servoPos = 1 else: cyprus.set_servo_position(1, .5) servoPos = 0
def cytronControllerFN(self): cyprus.setup_servo(1) # cyprus.set_servo_position(1, 0.5) cyprus.set_pwm_values(1, period_value=100000, compare_value=50000, compare_mode=cyprus.LESS_THAN_OR_EQUAL) sleep(5) cyprus.set_pwm_values(1, period_value=100000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL) sleep(5)
def tickArrows(self, way): cyprus.initialize() cyprus.setup_servo(2) num = self.ids.PWM_slider.value if way == "up": if self.ids.cytron_label.text == "Neutral": num = 55 elif num == 45: num = 50 elif num == 100: num = 100 else: num += 1 elif way == "down": if self.ids.cytron_label.text == "Neutral": num = 45 elif num == 55: num = 50 elif num == 0: num = 0 else: num -= 1 if (num > 45) and (num < 55): self.ids.cytron_label.text = "Neutral" self.ids.cytron_label.color = (1, .65, 0, .8) self.ids.PWM_slider.value = 50 val = 0 elif num > 54: s = "Forward " + str(num - 50) self.ids.cytron_label.text = s self.ids.cytron_label.color = (0, 1, 0, .8) self.ids.PWM_slider.value = num val = (self.ids.PWM_slider.value - 50) * 200 direction = 1 else: s = "Backward " + str(num - 50) self.ids.cytron_label.text = s self.ids.cytron_label.color = (1, 0, 0, .8) self.ids.PWM_slider.value = num val = (50 - self.ids.PWM_slider.value) * 200 direction = 0 cyprus.set_pwm_values(2, period_value=10000, compare_value=val, compare_mode=cyprus.LESS_THAN_OR_EQUAL)
def initialize(self): global s0 cyprus.initialize() cyprus.setup_servo(1) cyprus.setup_servo(2) cyprus.set_pwm_values(1, period_value=100000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL) cyprus.set_servo_position(2, 0.25) s0 = stepper(port=0, micro_steps=32, hold_current=20, run_current=20, accel_current=20, deaccel_current=20, steps_per_unit=200, speed=8) s0.set_as_home()
def tickArrows(self, way): cyprus.initialize() cyprus.setup_servo(2) num = self.ids.PWM_slider.value if way == "up": if self.ids.talon_label.text == "Neutral": num = 55 elif num == 45: num = 50 elif num == 100: num = 100 else: num += 1 elif way == "down": if self.ids.talon_label.text == "Neutral": num = 45 elif num == 55: num = 50 elif num == 0: num = 0 else: num -= 1 spd = num / 100 if (num > 45) and (num < 55): self.ids.talon_label.text = "Neutral" self.ids.talon_label.color = (1, .65, 0, .8) self.ids.PWM_slider.value = 50 spd = .5 elif num > 54: s = "Forward " + str(num - 50) self.ids.talon_label.text = s self.ids.talon_label.color = (0, 1, 0, .8) self.ids.PWM_slider.value = num spd = spd - .03 else: s = "Backward " + str(num - 50) self.ids.talon_label.text = s self.ids.talon_label.color = (1, 0, 0, .8) self.ids.PWM_slider.value = num spd = spd + .03 cyprus.set_servo_position(2, spd)
def updateLabel(self): cyprus.initialize() cyprus.setup_servo(2) spd = self.ids.PWM_slider.value / 100 if (self.ids.PWM_slider.value > 45) and (self.ids.PWM_slider.value < 55): self.ids.talon_label.text = "Neutral" self.ids.talon_label.color = (1, .65, 0, .8) spd = .5 elif self.ids.PWM_slider.value > 54: s = "Forward " + str(self.ids.PWM_slider.value - 50) self.ids.talon_label.text = s self.ids.talon_label.color = (0, 1, 0, .8) spd = spd - .03 else: s = "Backward " + str(self.ids.PWM_slider.value - 50) self.ids.talon_label.text = s self.ids.talon_label.color = (1, 0, 0, .8) spd = spd + .03 cyprus.set_servo_position(2, spd)
def setup(): cyprus.initialize() cyprus.setup_servo(1) cyprus.set_servo_speed(1, 0) sleep(0.05)
# # As mentioned, the Software library is in RaspberryPiCommon/pidev/Cyprus_Commands/Cyprus_Commands.py # The code is readable - and there is a very well written README.md with example usage in the same folder as the library. # # The PWM Ports on the RPiMIB are P4 and P5. The software library can be used to control both servo motors and motor # controllers like the Talon that use a servo motor input and convert that to a high current drive that can control # large motors. # # In addition to creating PWM that is used to control servo motors and servo motor style motor controllers the software # library has been designed to also create non servo specific PWM for controlling industry standard motor controllers. # Basically the Servo PWM is a special case of industry standard PWM, hopefully we will get to more on this later. # # To get a RC servo motor signal out of P4 of the RPiMIB do the following: cyprus.initialize() # initialize the RPiMIB and establish communication cyprus.setup_servo(1) # sets up P4 on the RPiMIB as a RC servo style output cyprus.set_servo_position( 1, 0 ) # 1 specifies port P4, 0 is a float from 0-1 that specifies servo position ~(0-180deg) sleep( 1 ) # wait one second for the servo to get there... minimum here should be about sleep(0.05) cyprus.set_servo_position( 1, .5 ) # 1 specifies port P4, 0.5 specifies servo position ~(0-180deg) range of (0-1) # when done disconnect the RPiMIB communication cyprus.close() # To get a RC servo motor CONTROLLER signal out of P5 of the RPiMIB do the following: cyprus.initialize() # initialize the RPiMIB and establish communication cyprus.setup_servo(
#MOTOR 2 SETUP motor_2 = stepper(port=0, speed=10, micro_steps=128) motor_2.home(0) print("homed motor 2") sleep(.5) home_pos_2 = 24.123 motor_2.go_to_position(home_pos_2) increment = .035 current_pos_x = home_pos_2 current_pos_y = home_pos_1 ccrpi.open_spi() print("opened spi") ##ccrpi.write_gpio(0) #### ccrpi.setup_servo() ccrpi.write_pwm(6, 1, .5) clamp = lambda n, min_n, max_n: max(min(max_n, n), min_n) try: while True: pygame.event.pump() joy_val_x = joystick.get_axis(HORIZONTAL_AXIS) joy_val_y = joystick.get_axis(VERTICAL_AXIS) current_pos_y = current_pos_y + joy_val_y * increment current_pos_x = current_pos_x + joy_val_x * increment current_pos_y = clamp(current_pos_y, home_pos_1 - 13, home_pos_1 + 13) current_pos_x = clamp(current_pos_x, home_pos_2 - 15.77, home_pos_2 + 15.77) motor_1.start_go_to_position(current_pos_y)
def initialize(self): cyprus.setup_servo(2) cyprus.setup_servo(1) print("Close gate, stop staircase and home ramp here")
Builder.load_file('main.kv') Window.clearcolor = (.1, .1, .1, 1) # (WHITE) cyprus.open_spi() ballOnTallTower = False # //////////////////////////////////////////////////////////////// # // SLUSH/HARDWARE SETUP // # //////////////////////////////////////////////////////////////// sm = ScreenManager() arm = stepper(port=0, speed=10) cyprus.initialize() cyprus.setup_servo(1) # cytron cyprus.setup_servo(2) # talon """ Port 4: Cytron Port 5: Talon Port 6: Tall Tower Sensor Port 7: Short Tower Sensor Port 8: N/A 38.9 is the tall tower 30.25 is the short tower """ # //////////////////////////////////////////////////////////////// # // MAIN FUNCTIONS // # // SHOULD INTERACT DIRECTLY WITH HARDWARE //
# // SLUSH/HARDWARE SETUP // # //////////////////////////////////////////////////////////////// sm = ScreenManager() ramp = stepper(port=0, micro_steps=32, hold_current=20, run_current=20, accel_current=20, deaccel_current=20, steps_per_unit=25, speed=INIT_RAMP_SPEED) ramp.go_until_press(0, 20000) ramp.set_as_home() cyprus.initialize() cyprus.setup_servo(2) cyprus.set_servo_position(2, 0) cyprus.setup_servo(1) cyprus.set_pwm_values(1, period_value=100000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL) # //////////////////////////////////////////////////////////////// # // MAIN FUNCTIONS // # // SHOULD INTERACT DIRECTLY WITH HARDWARE // # //////////////////////////////////////////////////////////////// # ////////////////////////////////////////////////////////////////
def binary_state(self): cyprus.initialize() cyprus.setup_servo(1) cyprus.set_servo_position(1, 0) sleep(1) cyprus.set_servo_position(1, 1)
class MainScreen(Screen): """ Class to handle the main screen and its associated touch events """ s0 = stepper(port=0, micro_steps=32, hold_current=20, run_current=20, accel_current=20, deaccel_current=20, steps_per_unit=200, speed=8) cyprus.initialize() cyprus.setup_servo(1) go = False direction_pin = 1 def thread_flip(self): y = threading.Thread(target=self.flip, daemon=True) y.start() def flip(self): if cyprus.read_gpio() & 0B0001: time.sleep(.05) if cyprus.read_gpio() & 0B0001: cyprus.set_servo_position(1, 1) self.ids.flip.text = "180 Degrees" else: cyprus.set_servo_position(1, 0) self.ids.flip.text = "0 Degrees" def pressed(self): self.go = not self.go if self.go: self.s0.run(self.direction_pin, int(self.ids.slider.value)) self.ids.motor.text = "Motor On" else: self.s0.softStop() self.ids.motor.text = "Motor Off" def direction(self): if self.go: if self.direction_pin == 1: self.direction_pin = 0 self.ids.direction.text = "Clockwise" self.s0.run(self.direction_pin, int(self.ids.slider.value)) else: self.direction_pin = 1 self.ids.direction.text = "Counter-Clockwise" self.s0.run(self.direction_pin, int(self.ids.slider.value)) def motor_thread(self): y = threading.Thread(target=self.motor, daemon=True) y.start() def motor(self): self.s0.set_as_home() print(self.s0.get_position_in_units()) self.ids.updates.text = str(self.s0.get_position_in_units()) self.s0.set_speed(1) self.s0.go_to_position(15) print(self.s0.get_position_in_units()) self.ids.updates.text = str(self.s0.get_position_in_units()) time.sleep(10) self.s0.set_speed(5) self.s0.relative_move(10) self.ids.updates.text = str(self.s0.get_position_in_units()) time.sleep(8) self.s0.relative_move(-25) self.ids.updates.text = str(self.s0.get_position_in_units()) time.sleep(30) self.s0.set_speed(8) self.s0.relative_move(-100) self.ids.updates.text = str(self.s0.get_position_in_units()) time.sleep(10) self.s0.relative_move(100) self.ids.updates.text = "Finished: " + str( self.s0.get_position_in_units()) def admin_action(self): """ Hidden admin button touch event. Transitions to passCodeScreen. This method is called from pidev/kivy/PassCodeScreen.kv :return: None """ SCREEN_MANAGER.current = 'passCode'