I've been working on using my Arduino to control a motor driver made by Roboteq, but it has built in scripting, so I think it can do everything I need done on it's own. Expect this: I need to calc. the distance travelled, via to inputs. Xval and Yval. Normally, it'd just be the sort(sq(xVal) + sq(yVal)). But this script editor only allows the abs() function, and + - * / and Mod. Is it possible to get the hypotenuse with only using these??? I sure am hoping so, as that will mean this controller can function in a stand alone setup. Thanks.

You could use Newton's Method. See, for example, http://mathworld.wolfram.com/NewtonsIteration.html

Pete

Whoa! Ok, do you know of any example of that written in code? I'm not finding any. I'll keep searching.

I found this: http://codereview.stackexchange.com/questions/43456/square-root-approximation-with-newtons-method

But I don't understand most of it. I don't really understand C++. I'm a self taught Arduino user. Would this process work without using sqrt?

a binary search, although slow, works straightforward

```
//
// FILE: findHypot.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo
// DATE: 2016-01-09
// URL: http://forum.arduino.cc/index.php?topic=370692.0
//
// Released to the public domain
//
uint32_t start;
uint32_t stop;
volatile float h;
volatile float a = 3, b = 4;
void setup()
{
Serial.begin(115200);
Serial.print("Start ");
Serial.println(__FILE__);
start = micros();
h = hypot(a, b);
stop = micros();
Serial.print("time:\t");
Serial.println(stop - start);
Serial.print("val:\t");
Serial.println(h, 4);
delay(1000);
start = micros();
h = findHypot(a, b);
stop = micros();
Serial.print("time:\t");
Serial.println(stop - start);
Serial.print("val:\t");
Serial.println(h, 4);
delay(1000);
start = micros();
h = findHypot(1, 1000); // extreme triangle
stop = micros();
Serial.print("time:\t");
Serial.println(stop - start);
Serial.print("val:\t");
Serial.println(h, 4);
}
void loop()
{
}
float findHypot(float a, float b)
{
float mi = 0;
float ma = a + b;
float t = a * a + b * b;
float mid;
while (ma - mi > 0.0001) // about max accuracy feasable
{
mid = (mi + ma) / 2;
float mid2 = mid * mid;
if (mid2 < t)
{
mi = mid;
}
else
{
ma = mid;
}
}
return mid;
}
```

output

```
Start findHypot.ino
time: 56
val: 5.0000
time: 748
val: 5.0000
time: 1072
val: 1000.0004
```

The idea is that the hypot is always between 0 and the sum of the two sides.

Instead of calculating the sqrt(sum of squares) the code compares the square of the (possible) hypot with the sum of squares. The latter need only be calculated once.

If less precision is needed, the binary search will perform faster.

Is there are particular reason why you don't want to use the avr libc math.h sqrt() function?

https://www.arduino.cc/en/Math/H

...I mean - aside from potentially not knowing about it, which, given what you say about yourself, seems likely...?

Google "arduino math.h" - you'll find more info...

But this script editor only allows the abs() function, and + - * / and Mod.

think newton raphson is the way to go, and then enroll the loop (e.g.3x same code iso for 1..3)

As I read it, the Roboteq scripting language doesn't have floating point, either, which will make things a bit harder. Check out these: http://www.codecodex.com/wiki/Calculate_an_integer_square_root; the Muntsinger algorithm looks particularly compact, which may be important for a scripted/interpreted implementation. It may be annoying to translate from C to the basic-like scripting code, but it looks like it should be possible.

I'm sitting here wondering why all the complexity, when there is a standard solution (for getting a square root) available (unless the OP needs a faster working method, which is possible - but we have yet to hear anything back).

when there is a standard solution

The OP isn't asking about Arduino code; he has a motor controller with a scripting language that does NOT have a sqrt() function, and was hoping that he could get it to do all the work independently...

Although, since the Arduino side DOES have a sqrt() function, perhaps it would make more sense to do that part of the calculations on the arduino anyway...

westfw: The OP isn't asking about Arduino code; he has a motor controller with a scripting language that does NOT have a sqrt() function, and was hoping that he could get it to do all the work independently...

Ah...now it is clear; my lack of reading comprehension...

:confused:

OP - can you tell us which controller you have? I only ask because the code for this example:

...seems to indicate that MicroBasic (the scripting language for Roboteq's controllers) has a sqrt() function - while this page:

http://www.roboteq.com/index.php/support/microbasic-reference

...seems to state what the OP stated - that only abs() is the only supported function in MicroBasic.

WTF.

OP - have you contacted Roboteq about this discrepancy? Maybe there's a firmware update for the controller, to give more commands to MicroBasic - and the docs haven't been updated to reflect this?

In some googling - I found a "blurb" next to a found link that states "Microbasic Scripting ... New mathematic and trigonometric functions (sqrt, sin, cos and atan) have been added to the language (firmware version 1.6 and above) in order to ..." - of course, you click the link, and the website is gone from the host, and nothing is cached...

...and the max version is 1.5 (downloads page)?

But - check this:

Care to try beta software? If so - you can get the sqrt() function, it seems...

Good luck.

:)