Login required to started new threads

Login required to post replies

Software API for Speed from Watts?
Quote | Reply
Anyone have an api similar to bikecalculator that gives speed from power, grade, etc? Don't care what language it's in.
Quote Reply
Re: Software API for Speed from Watts? [fisherman76] [ In reply to ]
Quote | Reply
this should get you started:

package com.aeroweenie.yawangle;

/**
* Created by jack on 7/4/13.
*/
public class CyclingPowerModel {



private double dragCoefficient; //drag coefficient Cd
private double area; //frontal area
private double airDensity; //air density
private double crr;
private double mass;

private double g = 9.80665;
private double momentOfInertiaWheels = 0.14;
private double tireRadius = .7;
private double drivetrainEfficiency = 1;
private double dragAreaSpokes = 0.0000;




public CyclingPowerModel(double mass, double crr, double dragCoefficient,double area, double airDensity )
{
this.airDensity = airDensity;
this.mass = mass;
this.crr = crr;
this.dragCoefficient = dragCoefficient;
this.area = area;

}



public double aeroSpeed(double windDirection, double riderDirection, double windVelocity,double power)
{
return 0;
}
public double aeroPower(double airVelocity, double groundVelocity)
{
return (airVelocity*airVelocity) * groundVelocity * 0.5 * airDensity * (dragCoefficient*area + dragAreaSpokes);
}

public double crrPower(double groundVelocity, double gradient)
{
return groundVelocity * Math.cos(Math.atan(gradient))*crr*mass*g;
}

public double bearingPower(double groundVelocity)
{
return groundVelocity*(91+8.7*groundVelocity)*.001;
}

public double pePower(double groundVelocity,double gradient)
{
return groundVelocity*mass*g*Math.sin(Math.atan(gradient));
}

public double kePower(double vf, double vi, double time)
{
return 0.5*(mass + momentOfInertiaWheels/(tireRadius*tireRadius)) * (vf*vf-vi*vi)/(time);
}

public double computeTotalPowerStatic(double windDirection, double riderDirection, double windVelocity, double groundVelocity, double gradient)
{
double vwtan = windVelocity * Math.cos(riderDirection-windDirection);
double airVelocity = groundVelocity+vwtan;


double aeroPower = aeroPower(airVelocity,groundVelocity);
double crrPower = crrPower(groundVelocity,gradient);
double bearingPower = bearingPower(groundVelocity);

double pePower = pePower(groundVelocity,gradient);

double netPower = aeroPower+crrPower+bearingPower+pePower;
double totalPower = netPower/drivetrainEfficiency;
return totalPower;

}
public double computeTotalPowerDynamic(double windDirection, double riderDirection, double windVelocity, double time, double distance, double vi, double vf, double gradient)
{
double groundVelocity = distance/time;
double vwtan = windVelocity * Math.cos(riderDirection-windDirection);
double airVelocity = groundVelocity+vwtan;

double aeroPower = aeroPower(airVelocity,groundVelocity);
double crrPower = crrPower(groundVelocity,gradient);
double bearingPower = bearingPower(groundVelocity);

double pePower = pePower(groundVelocity,gradient);
double kePower = kePower(vf,vi,time);

double netPower = aeroPower+crrPower+bearingPower+pePower+kePower;
double totalPower = netPower/drivetrainEfficiency;

return totalPower;
}










}



Kat Hunter reports on the San Dimas Stage Race from inside the GC winning team
Aeroweenie.com -Compendium of Aero Data and Knowledge
Freelance sports & outdoors writer Kathryn Hunter
Quote Reply
Re: Software API for Speed from Watts? [jackmott] [ In reply to ]
Quote | Reply
thanks, that saves me a bunch of work. appreciated!
Quote Reply
Re: Software API for Speed from Watts? [fisherman76] [ In reply to ]
Quote | Reply
Unless I am misunderstanding, it sounds like you want to be able to calculate speed, given the power as input. I think Jack's code is giving you the power if you input the speed. Here is some Python to calculate the speed given the other inputs. (Actually it doesn't look like it handles wind direction, but you can adapt it using the wind components from Jack's code. Let me know if this doesn't make sense and I can add in the wind to this code.) I use this code to estimate how far I have ridden on trainer rides. It could also be used to show a "virtual speed" using real-time trainer data.


Code
import math, cmath 


def cbrt(x): return math.copysign(math.pow(abs(x), 1.0/3.0), x)
def polar(x, y): return math.hypot(x, y), math.atan2(y, x)


def quadratic(a, b, c=None):
if c is not None: a, b = b / float(a), c / float(a)
t = a / 2.0
r = t**2 - b
y1 = math.sqrt(r) if r >= 0 else cmath.sqrt(r)
return y1 - t, -y1 - t


def cubic(a, b, c, d=None):
if d is not None: a, b, c = b / float(a), c / float(a), d / float(a)
t = a / 3.0
p, q = b - 3 * t**2, c - b * t + 2 * t**3
u, v = quadratic(q, -(p/3.0)**3)
if type(u) == type(0j): # complex cubic root
r, w = polar(u.real, u.imag)
y1 = 2 * cbrt(r) * math.cos(w / 3.0)
else: # real root
y1 = cbrt(u) + cbrt(v)
y2, y3 = quadratic(y1, p + y1**2)
return y1 - t, y2 - t, y3 - t


# assume 0 wind, perfect drive train efficiency
def simulate(watts, CdA=0.300, rho=1.2250, CRR=0.003, m=65, FW=0.0044, g=9.81, GR=0.0, I=0.14, r=.334, D=10) :
KA = 0.5 * rho * (CdA + FW)
FRR = m * g * CRR * math.cos(math.atan(GR))
FWB1 = 91*1.0E-3
FWB2 = 8.7*1.0E-3
FPE = m * g * math.sin(math.atan(GR))
FKE = 2.0 * (m + I/(r*r)) * float(D)
speed = [0.0] * (D*len(watts))
cur_speed = 0.0
print "t,v,w,d"
dist = 0.0
for idx in xrange(len(speed)) :
speed[idx] = cubic(KA, FWB2 + FKE, FRR+FWB1+FPE - FKE*cur_speed, -watts[idx/D])[0]
dist += speed[idx] / float(D)
cur_speed = 2*speed[idx] - cur_speed
print "%f,%f,%f,%f" % (idx/float(D), speed[idx], watts[idx/D], dist)
return speed




watts = [250]*3600
speed = simulate(watts, CdA=0.388, rho=1.226, CRR=0.005, m=79, GR=0.0, FW=0, I=0, D=10, g=9.81)

Quote Reply
Re: Software API for Speed from Watts? [jackmott] [ In reply to ]
Quote | Reply
Jack, couple of questions:
1) What are you using as a plug for area\what unit? How would you go about calculating actual frontal area? I tried calibrating using CdA = .3 (drag coefficient set to .3,area at 1), but that leads me to...
2) Same for air density, what unit is the input expressed in? I tried 1.2754 (dry air) and got some really wild numbers, figured my unit is off.
Last edited by: fisherman76: Aug 21, 14 5:19
Quote Reply
Re: Software API for Speed from Watts? [jackmott] [ In reply to ]
Quote | Reply
Never mind, I was able to answer my own questions once I realized I wasn't converting kph to m\s. Everything lines up beautifully now. thanks again Jack.
Quote Reply
Re: Software API for Speed from Watts? [fisherman76] [ In reply to ]
Quote | Reply
Those inputs should be correct. velocity are all in meters/second, mass all in kilograms. Would that explain it?


fisherman76 wrote:
Jack, couple of questions:
1) What are you using as a plug for area\what unit? How would you go about calculating actual frontal area? I tried calibrating using CdA = .3 (drag coefficient set to .3,area at 1), but that leads me to...
2) Same for air density, what unit is the input expressed in? I tried 1.2754 (dry air) and got some really wild numbers, figured my unit is off.



Kat Hunter reports on the San Dimas Stage Race from inside the GC winning team
Aeroweenie.com -Compendium of Aero Data and Knowledge
Freelance sports & outdoors writer Kathryn Hunter
Quote Reply