Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- GitHub Community Forum
- :
- Conversations
- :
- Project Development Help and Advice
- :
- Help converting python math code to arduino code

Options

- Subscribe to RSS feed
- Mark all as new
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer friendly page

Copilot Lvl 2

2 weeks ago

- Mark as new
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Message 1 of 3

Help converting python math code to arduino code

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; } }

2 Replies

Copilot Lvl 2

yesterday

- Mark as new
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Message 2 of 3

Re: Help converting python math code to arduino code

Pilot Lvl 3

yesterday

- Mark as new
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Message 3 of 3

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!

New solutions

Top Kudoed Posts