 # Help editing Self Balancing Motorcycle code/ algorithms

Can someone please check if (1) my math makes sense and (2) if i coded it correctly. Please show me what formula my code whould traslate into when written out. The output is the number of seconds that the motors will run to put the handle bars into the correct possition. Feel free to ask any questions!

``````from math import sin, cos, atan, asin, sqrt, radians

w = 1.2 #wheelbase
v = 6 #speed in meters/sec of bike
g = 9.8 #constant of gravity in meters/sec
r = 1000000000 #radius of the turn
height = 0.75 #height of the center of mass
h = .5 #half of the length of the handlebar
t = 0.035 #half of the width of the tire
l = 0.8381 #length of the body of the actuator + 1.5 of the length of the fully extended actuator
c = 72 #caster angle in degrees
rate = 0.22987 #rate of motor
cel = 0.83 #current extention length of actuator

angle = (w*cos(atan(v**2/g*r)+asin(t*(sin(c)/(height-t)))))/r*cos(c) #needed steer angle of bike for desired turn

rawdist = (sqrt((l+h*sin(angle))**2+((l+h*sin(angle)*(h-h*cos(angle))/(h*sin(angle))**2)))) #distance that the actuator must travel

dist = rawdist-cel #total distance from actuator mount to new place to move

sec = dist/rate #number of seconds for the motors to run to make the needed changes to the bike handle bar poss

print(sec)
``````

P.S. This is python 3 code.

I found one glitch! (the first of many!) Instead of “rawdist = (sqrt((l+h*sin(angle))**2+((l+h*sin(angle)*(h-h*cos(angle))/(h*sin(angle))**2)))) #distance that the actuator must travel”, it should just be “rawdist = (sqrt((l+h*sin(angle))**2+((h-h*cos(angle))**2))) #distance that the actuator must travel”. Right?!