Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 8

Help converting python math code to arduino code

Solved! Go to Solution.

Hi! This is my problem. I have created stability codes for my self driving motercycle in python. I am by far most comfortable in python. However, I need this particular math to run on my arduino uno. I did my best to convert it, and it failed. I have added print() and serial.println() commands to track bugs. Here is the python code and my arduino attempt. Please help me make my serial.println() results exactly match the print() results. Thanks!

Python:

 

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 = 10000000000 #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.8381 #current extention length of actuator

c = radians(c)



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
print(angle)
rawdist = (sqrt((l+h*sin(angle))**2+((h-h*cos(angle))**2))) #distance that the actuator must travel
print(rawdist)
rawdist = float('{:.3f}'.format(rawdist))
print(rawdist)
dist = rawdist-cel #total distance from actuator mount to new place to move
print(dist)
sec = dist/rate #number of seconds for the motors to run to make the needed changes to the bike handle bar poss
print(sec)

arduino:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

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




void loop() {
  if (once == false){
    
    c = radians(c);
    
    float angle = (w*cos(atan(sq(v)/g*r)+asin(t*(sin(c)/(height-t)))))/r*cos(c); //needed steer angle of bike for desired turn
    Serial.println(angle);
    float rawdist = (sqrt(sq(l+h*sin(angle))+(sq(h-h*cos(angle))))); //distance that the actuator must travel
    Serial.println(rawdist);
    rawdist = float(rawdist, 3);
    Serial.println(rawdist);
    float dist = rawdist-cel; //total distance from actuator mount to new place to move
    Serial.println(dist);
    float sec = dist/rate; //number of seconds for the motors to run to make the needed changes to the bike handle bar poss

    Serial.print(sec);
    
    once = true;
  }
}

 

 

7 Replies
Copilot Lvl 2
Message 2 of 8
Commander Lvl 1
Message 3 of 8

Re: Help converting python math code to arduino code

What are the current discrepancies?  If the "angle" output is "wrong" then all subsequent values will be "wrong".  Show us the current values, so those of us without Arduinos can make constructive suggestions.

 

Keep in mind too that I _think_ there is a discrepancy between floating point representations of Python (64-bit?) and Arduino (32-bit). I'm not sure you're going to get the "same" results.

 

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Copilot Lvl 2
Message 4 of 8

Re: Help converting python math code to arduino code

What do you mean by "the current values"?

Commander Lvl 1
Message 5 of 8

Re: Help converting python math code to arduino code


@cdiener1 wrote:

What do you mean by "the current values"?


 

Run the program in python, and show us the output.

 

Run the program under Arduino, and show us the output.

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Copilot Lvl 2
Message 6 of 8

Re: Help converting python math code to arduino code

In python print(angle) gives -1.726362278452049e-12, print(rawdist) gives 0.8380999999991368, the next print(rawdist) gives 0.838, print(dist) gives -9.999999999998899e-05, and print(sec) gives -0.0004350284943663331. I think some of my variables need adjusting, but the the math is running correctly. The arduino values: Serial.println(angle); gives -0.00, Serial.println(rawdist); gives 0.84, Serial.println(rawdist); gives 3.00, Serial.println(dist); gives 2.16, and Serial.print(sec); gives the impossible and incorrect 9.40. Thank you SO much for trying to help; I am much better with python than with arduino.

Solution
Commander Lvl 1
Message 7 of 8

Re: Help converting python math code to arduino code

Great!  So it looks to me like the first two steps are sorta working on Arduino.  At least within the likely accuracy of the floating point processor.

 

-1.72xxxe-12 is pretty much 0 (zero). The second set of values are equivalent (0.8380xxxx is pretty much 0.84).

 

So step 3 is the first problem. Looking again at your Arduino code, the statement

rawdist = float(rawdist, 3);

is unnecessary: rawdist is already a float. I'm guessing (as the docs don't help) the '3' parameter is why rawdist gets the value 3!  I think if you disable that statement, I think the remaining outputs will be much closer to correct!

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Copilot Lvl 2
Message 8 of 8

Re: Help converting python math code to arduino code

Thanks! If the first parts of the arduino code are working, then I will have to think about about what to do. I need the values to be accurate.