Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # stick is a tuple of the form (x, y), that comes from the 2-axis controller (wii nunchuk)
- # Both x and y are values from 0 - 255
- # x=0 means "all the way left"
- # y=0 means "all the way down"
- # motors are controlled by PWM
- # The only thing we change is the duty cycle,
- # which is a value from 0 - 100
- # Even though the duty cycle can only be 0 - 100, my move_motors() function
- # takes values from -100 - 100. This is because if the value you pass
- # for a motor is below 0 (a negative number), it will flip the pin that
- # controls direction, and always pass the PWM pin a positive value
- # Eg:
- # passing move_motors() the argument -58 for left_motor means:
- # flip the direction pin so motor spins backwards, and set duty cycle to 58
- # Turn the 0-255 value of joystick into -128 - 127 value
- left_right = stick[0] - 128
- front_back = stick[1] - 128
- left_motor = front_back + left_right
- right_motor = front_back - left_right
- # Scale factor defaults to 1
- scale_factor = 1
- # Calculate scale factor
- if abs(left_motor) > 100 or abs(right_motor) > 100:
- # Find highest of the 2 values, since both could be above 100
- x = max(abs(left_motor), abs(right_motor))
- # Calculate scale factor
- scale_factor = 100.0 / x
- # Use scale factor, and turn values back into integers
- left_motor = int(left_motor * scale_factor)
- right_motor = int(right_motor * scale_factor)
- # Actually move the motors
- move_motors(left_motor, right_motor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement