Kyle Chuang | ME405 Mechatronics Portfolio
balance.py File Reference

Balancing Ball Main File. More...

Variables

 balance.pinB6 = pyb.Pin.cpu.B6
 
 balance.pinB7 = pyb.Pin.cpu.B7
 
 balance.e1 = e(pinB6, pinB7, 4)
 
 balance.pinC6 = pyb.Pin.cpu.C6
 
 balance.pinC7 = pyb.Pin.cpu.C7
 
 balance.e2 = e(pinC6, pinC7, 8)
 
 balance.pinA15 = pyb.Pin.cpu.A15
 
 balance.pinB2 = pyb.Pin.board.PB2
 
 balance.pinB4 = pyb.Pin.cpu.B4
 
int balance.ch1 = 1
 
 balance.pinB5 = pyb.Pin.cpu.B5
 
int balance.ch2 = 2
 
bool balance.motor1 = True
 
 balance.pinB0 = pyb.Pin.cpu.B0
 
int balance.ch3 = 3
 
 balance.pinB1 = pyb.Pin.cpu.B1
 
int balance.ch4 = 4
 
bool balance.motor2 = False
 
int balance.tim = 3
 
 balance.m1 = m(motor1, pinA15, pinB2, pinB4, ch1, pinB5, ch2, tim)
 
 balance.m2 = m(motor2, pinA15, pinB2, pinB0, ch3, pinB1, ch4, tim)
 
 balance.xp = Pin.cpu.A0
 
 balance.xm = Pin.cpu.A6
 
 balance.yp = Pin.cpu.A7
 
 balance.ym = Pin.cpu.A1
 
float balance.w = 0.1
 
float balance.l = 0.176
 
float balance.xc = l/2
 
float balance.yc = w/2
 
 balance.ts = t(xp, xm, yp, ym, w, l, xc, yc)
 
int balance.K1 = -100
 
int balance.K2 = -50
 
int balance.K3 = -500
 
int balance.K4 = -20
 
int balance.x_0 = 0
 
int balance.y_0 = 0
 
int balance.V_DC = 12
 
float balance.R = 2.21
 
float balance.K_T = 13.8
 
int balance.duty_conv = 100*R/(V_DC*K_T)
 
 balance.start_time = utime.ticks_ms()
 
 balance.inpt = input('Manually level the platform. Press "0" when platform is ready: ')
 
 balance.val = int(inpt)
 
 balance.theta_y_0 = e1.set_position(0)
 
 balance.theta_x_0 = e2.set_position(0)
 
 balance.curr_time = utime.ticks_ms()
 
 balance.delta_t = utime.ticks_diff(curr_time, start_time)
 
 balance.P = ts.coord()
 
int balance.theta_y_1 = 2*3.14*e1.get_position()/4000
 
tuple balance.thetadot_y = (theta_y_1 - theta_y_0)/ delta_t
 
 balance.x_1 = P[0]
 
tuple balance.xdot = (x_1 - x_0)/delta_t
 
int balance.T_x = -K1*xdot - K2*thetadot_y - K3*x_1 - K4*theta_y_1
 
 balance.Duty_x = int(-1*duty_conv*T_x)
 
int balance.theta_x_1 = 2*3.14*e2.get_position()/4000
 
tuple balance.thetadot_x = (theta_x_1 - theta_x_0)/ delta_t
 
 balance.y_1 = P[1]
 
tuple balance.ydot = (y_1 - y_0)/delta_t
 
int balance.T_y = -K1*ydot - K2*thetadot_x - K3*y_1 - K4*theta_x_1
 
 balance.Duty_y = int(duty_conv*T_y)
 

Detailed Description

Balancing Ball Main File.

This code is Kyle's attempt at balancing the ball. It involves manually zeroing the platform then inputting 0 to begin the balancing process. There is a failsafe where the motors will disable when there is no input to the touchscreen, so the platform balancing will start once the ball is placed onto the platform.

This code requires the encoder, motor, and touchscreen drivers made previously for successsful operation, as they provide inputs (ball position data from the touchscreen and the theta data from the encoders) to our control system. The output of the control system is torque supplied to the motor shafts (T_x and T_y) which are converted to motor duty cycle values using a constant gain conversion derived from the motor data sheet. The result is two motor objects that each control an axis of tilt for the platform which react to the location of the ball.

Author
Kyle Chuang
Enoch Nicholson
Date
March 15, 2021