"2.2 = fall over, 2.5 = dr jones, 2.7=competent, 3.5 = too good";

θ0=0;ϕ0=0;g=9.81;L=0.8;r=.3;M=70;m=5;tmax=10;tmin=-5;feedbackStiffness=2.7;

startDuration=.1;v=1;maxLean=0.1;velDiscrep=.2;stop=False;

x[t_]:=r ϕ[t]+L Sin[θ[t]]

y[t_]:=r+L Cos[θ[t]]

eqs={

M x''[t] == -T[t]/L Cos[θ[t]]  +  T[t]/r    -   2 m r ϕ''[t],

M y''[t] == +T[t]/L Sin[θ[t]]  -  M g   +   (T[t]/r-2 m r ϕ''[t]) Cot[θ[t]]


}

targetVelocity[t_]:=v(ArcTan[t/startDuration]/π+1/2)

targetVelocity[t_]:=v(ArcTan[t/startDuration]/π + 1/2 -If[stop,(ArcTan[(t-5)/startDuration]/π+1/2),0] )

Plot[targetVelocity[t],{t,tmin,tmax}]

targetLean[t_]:=ArcTan[(targetVelocity[t]-x'[t])/velDiscrep]*2/π*maxLean;

TT= feedbackStiffness*(M/r)*(θ[t]-targetLean[t])

eqs2=eqs/.T[t]->TT

ans=NDSolve[{eqs2[[1]] && eqs2[[2]] && θ[tmin]==0.1 && ϕ[tmin]==0.0002 && θ'[tmin]==0.0004 && ϕ'[tmin]==0.0002 },{θ[t],ϕ[t]},{t,tmin,tmax}]

θ[t]/.ans[[1]]

ϕResult[k_]:=((ϕ[t]/.ans[[1]])/.t->k)

θResult[k_]:=((θ[t]/.ans[[1]])/.t->k)

{θResult[tmin],ϕResult[tmin]}

Print["Lean"];Plot[θResult[t]*180/Pi, {t,tmin,tmax},PlotRange->All]

Print["Revs"];Plot[ϕResult[t]/(2 Pi),{t,tmin,tmax}]

θResult[.2]

axle[t_]:={r ϕResult[t], r}

pole[t_]:=L{Sin[θResult[t]],Cos[θResult[t]]}

crank[t_]:=r/2{Cos[ϕResult[t]+π/4], -Sin[ϕResult[t]+π/4]}

crankEndA[t_]:=axle[t]+crank[t]

crankEndB[t_]:=axle[t]-crank[t]

semiPedal[t_]:=r/6{+1,0}

top[t_]:=axle[t]+pole[t]

semiSeat[t_]:=L/8{-Cos[θResult[t]],+Sin[θResult[t]]}

contents[t_]:={


	Circle[{r ϕResult[t],r},r],
	Circle[{r ϕResult[t],r},r*.9],
	
	
	Line[{crankEndA[t], crankEndB[t]}],

	{
	Thickness[0.01],
	Line[{crankEndA[t]+semiPedal[t], crankEndA[t]-semiPedal[t]}],
	Line[{crankEndB[t]+semiPedal[t], crankEndB[t]-semiPedal[t]}]
	},
	
	{Thickness[0.005],
	Line[{axle[t], top[t]}]},
	
	{Thickness[0.01],
	Line[{
	top[t]+semiSeat[t],
	top[t]-semiSeat[t]
	}
	]}
	
	}

frame[t_]:=Graphics[contents[t],AspectRatio->2/8,Axes->True,PlotRange->{{-1,7},{0,2}},ImageSize->800]

Show[frame[0]]

Animate[frame[t],{t,tmin,tmax,0.01},AnimationRate->0.9,AnimationRunning->False]