Thymio II balancing on a ball
A ball is a surface with a variable slope where a robot can navigate. As in the example of slope avoidance the Thymio II can use the accelerometers to determine if it is horizontal on the top of the ball or if it is on a lateral slope. If it is on a slope, the accelerometers allow to know in which direction it needs to go to reach the top again. The lateral accelerometer is used to rotate, the one in the axis of the robot front direction to move forward and backwards.
A very simple code very close to the example of slope avoidance but inversed allows to get the balancing behaviour:
var accTampon0[4] = 0,0,0,0
var accTamponPos0 = 0
var accTampon1[4] = 0,0,0,0
var accTamponPos1 = 0
var sumacc0
var sumacc1
onevent acc
accTampon0[accTamponPos0] = acc[0]-2 #measure lateral tilt
accTamponPos0 = (accTamponPos0 + 1) % 4
sumacc0 = accTampon0[0] + accTampon0[1] + accTampon0[2] + accTampon0[3]
accTampon1[accTamponPos1] = acc[1] #measure front tilt
accTamponPos1 = (accTamponPos1 + 1) % 4
sumacc1 = accTampon1[0] + accTampon1[1] + accTampon1[2] + accTampon1[3]
motor.left.target=sumacc0*15+sumacc1*15 # acc0 gives rotation, acc1 gives forward movement
motor.right.target=-sumacc0*15+sumacc1*15
This is a video of the result:
Another video showing the same behavior but with a weight to help the adhesion of the wheels on the ball: