This is an old revision of the document!
#WEEK 1 REPORT
# Finished building the structure.
# Sensor limitations: I got the Syntax to get the reading of this sensor in the seller's website (I didn’t find a function to retrieve the value, like the US sensor (just put SensorUS(port) and you get the sensor reading) website here.Running some tests , the sensor does not provides good reading when the ball is < 150mm . So I will put a brick in 150mm from the sensor to avoid these wrong readings. # code sketch:
# Attempt to use PID:
Researching on the internet i found that the integral term is not necessary. ( possible to see after obtaining the transfer function of the B&B system , the integral term will add another integrator , making the system more unstable).
* Proportional control: The bar will lift up or down as fast as how far is the ball from the setpoint.Power output command proportional to distance from sensor.
* Derivative control: to avoid oscillations from pure proportional control ( helps to break the ball when close to setpoint).Power output command proportional to velocity ( d/dt distance).
# Code Sketch
I'm thinking in something like this: ( i'm not sure if this works or not , it's just my first notes ). The motor will rotate faster when the ball is far from the setpoint (proportional) , and even faster if the ball is far from the setpoint and still moving (derivative).
Define the variable : d_setpoint ( where the ball will balance )
Function to calculate the distance and the velocity of the ball.
d_actual = sensor reading // unity is mm error = d_actual – d_prev dt= (currenttick – prevtick ) // unity is ms velocity = error/dt d_prev= d_actual prevtick=currenttick
Task to calculate the motor output to keep the balance
kp= ... kd=.... while true power= Kp*error + Kd*velocity if power <0 // as the OnFwd does not accept negative values for the power output OnRev(port , ABS(power)) Else OnFwd(port,power) end if End while