| 1 | # Writing Programs for KuttyPy |
| 2 | |
| 3 | ## Two options are available |
| 4 | |
| 5 | + Run the kuttypy as an accessory for controlling real world events from a Python code running on your laptop. |
| 6 | |
| 7 | For example, you could use opencv to track an object, and kuttypy to control a robotic arm that interacts with it. |
| 8 | |
| 9 | + Write C code, compile it with avr-gcc, upload it to KuttyPy, and use KuttyPy as an independent processor. Example, a burglar alarm where kuttypy monitors a switch hooked up to a door, and enables an alarm when it is triggered. skip to [the C code section](../../programming/c) for details. |
| 10 | |
| 11 | |
| 12 | ## Python Code Example |
| 13 | |
| 14 | ### Elementary Example : Blink an LED ( Red onboard LED connected to PB3 ) |
| 15 | |
| 16 | ```python |
| 17 | import time |
| 18 | from kuttyPy import * # The import statement automatically connects to an available device. |
| 19 | |
| 20 | setReg('DDRB',8) # PB3 made output. 0b00001000 = 8 |
| 21 | |
| 22 | for a in range(5): #Blink 5 times |
| 23 | setReg('PORTB', 8) #PB3 to 5 Volt supply. LED turns on |
| 24 | time.sleep(0.5) # Wait for 0.5 second |
| 25 | setReg('PORTD', 0) # PB3 set to 0 . LED is now off |
| 26 | time.sleep(0.5) # Wait for another 0.5 second |
| 27 | ``` |
| 28 | ### Slightly Complex Example |
| 29 | Control a MeArm robotic arm via a 16-channel PWM generator(PCA9685), and an MPU6050 accelerometer as user input. |
| 30 | ```python |
| 31 | import kuttyPy as kp |
| 32 | kp.PCA9685_init() # Initialize the I2C based 16-channel PWM generator |
| 33 | kp.MPU6050_init() # Init the MPU6050 3-axis accelerometer+ 3-axis gyro |
| 34 | kp.MPU6050_kalman_set(1) #Enable a moving average on MPU6050 to reduce jitter |
| 35 | |
| 36 | # We need to map accelerometer values to motor angles of the robot. |
| 37 | # These values were obtained manually via the KuttyPy GUI, and now we'll calculate slopes and offsets |
| 38 | m1 = (138-64.)/(16700-9700) |
| 39 | c1 = 64. - 9700*m1 |
| 40 | m2 = (33-131.)/(16700-9700) |
| 41 | c2 = 131. - 9700*m2 |
| 42 | m3 = (160-20.)/(10000. + 10000) |
| 43 | c3 = 20. + 10000*m3 |
| 44 | |
| 45 | while(1): |
| 46 | x = kp.MPU6050_all() # read values from the MPU6050. total 7 parameters |
| 47 | if x is not None: # Ensure no packet drop event |
| 48 | kp.PCA9685_set(1, int(m1*x[0]+c1)) # Ax controls forearm |
| 49 | kp.PCA9685_set(2, int(m2*x[0]+c2)) # Ax controls upper arm |
| 50 | kp.PCA9685_set(3, int(m3*x[1]+c3)) # Ay controls rotation |
| 51 | |
| 52 | ``` |
| 53 | |
| 54 | !!! info "Manually controlling a Robotic Arm based on SG90 servos!" |
| 55 | <video controls > |
| 56 | <source src="../../images/robot.mp4" |
| 57 | type="video/mp4"> |
| 58 | Sorry, your browser doesn't support embedded videos. |
| 59 | </video> |
| 60 | The robot is being controlled from the KuttyPy GUI by manually adjusting the PWM outputs of the PCA9685 PWM generator IC. |
| 61 | This is super helpful to note which values correspond to which angles of the robot. |
| 62 |