collapse

Welcome!



Welcome to Robot Rebels, an online robot maker community.

Register today to post your projects, ask questions, share knowledge and meet like-minded people from around the world.


The RobotRebel.org Community

Author Topic: CORDIC algorithms  (Read 2171 times)

deshipu

  • Member
  • *
  • Posts: 113
  • Radomir Dopieralski
    • Homepage
CORDIC algorithms
« on: December 21, 2016, 04:46:58 PM »
We often need to use trigonometric and inverse trigonometric functions in our microcontroller projects. Whether it's calculating inverse kinematics, figuring out angles and distances, stabilizing a quadcopter or otherwise dealing with angles -- those functions are indispensable. Personally I'm using the atan2 and acos functions in my robots for calculating inverse kinematics of the legs. I'm using the standard floating-point functions from math.h, but that is suboptimal.

First of all, the implementations of those functions are rather large. Just adding them to my AVR code makes it swell by some 3000 bytes. That is partially because they probably have some lookup tables and complicated logic, but also because they are floating-point functions, and pull in all the floating point multiplication, division and other operations, which are quite costly on a poor microcontroller without a co-processor.

Second, they take and return values that are scaled rather arbitrarily (-1:1, -PI:PI, etc.), so afterwards I have to do some more floating point operations to get them to the 0-1800 range that I need for my servos.

So I decided to try and find integer replacements for those functions. I couldn't find any that would be suitable. They were either floating-point, using some strange fixed-point types, using huge lookup tables, etc. However, during my search I discovered a family of algorithms called CORDIC, which allows me to calculate trigonometric and inverse trigonometric functions with minimal code, using only a lookup into a tiny table, additions and bit shifts. If you are interested about how those algorithms work, I wrote a short article about it at https://hackaday.io/project/6050-tote/log/50664-cordic

Anyways, I still couldn't find a decent implementation of those algorithms for the AVR, so I set out to write my own. I started by prototyping them in Python (because it's easy to debug for me and fast to write), and when I got them to work with a reasonable accuracy (I don't need much, 0.1° is enough, my servos won't move at better accuracy anyways), I re-wrote them in AVR C. The code is at https://bitbucket.org/thesheep/imath/src/tip/imath.c and it currently takes about 514 bytes when compiled with my avr-gcc. I suspect a couple of bytes could be shaved here and there from it, but I'm pretty happy with it already. Oh, I also added a square root function in there, for completeness.

Enjoy!

Protowrxs

  • Member
  • *
  • Posts: 144
    • The Protowrxs World
Re: CORDIC algorithms
« Reply #1 on: December 21, 2016, 10:27:17 PM »
Although I really don't understand it all it has always been a pain trying to do many math functions on an Arduino even for dead reckoning, etc for me. I understand the concept of IK but not the method. I followed up on some of your links and it does help me some, just have much more to read to try to fully understand.

Thanks for sharing your info. Can this stuff be added as an Arduino library in any manner?
(Sorry, more stuff I do not understand)

I'd really like to try again to build a four legged walker but do it right this time. Maybe someday.

Stephen
The past is done, the future is not here, live today...
My Bots: http://www.Protowrxs.com/Robotics

deshipu

  • Member
  • *
  • Posts: 113
  • Radomir Dopieralski
    • Homepage
Re: CORDIC algorithms
« Reply #2 on: December 22, 2016, 04:40:14 AM »
Can this stuff be added as an Arduino library in any manner?

Not sure what you mean. If you are asking if you can use this in your Arduino programs, then yes, the easiest way is to just copy the imath.h and imath.c files into the directory of your sketch, and do #include "imath.h" in your code. If you are asking whether I plan to make a pull request to the Arduino core to have this code included by default, then the answer is no.

 

* Search


* Recent Topics

NASA bot by ossipee
[August 05, 2018, 06:52:36 PM]


ESP32 - Stepper - LIDAR - Platform by Gareth
[July 28, 2018, 09:57:11 AM]


Android Adware by Pittmanz
[July 24, 2018, 12:40:23 AM]


Greetings! by Pittmanz
[July 24, 2018, 12:39:20 AM]


Sloooow progress on the I3 clone by KingBeetle
[July 16, 2018, 04:20:59 AM]


MKS Gen L 1.0 by jinx
[July 15, 2018, 03:55:57 AM]


Motor rewinding machine by 1 what
[July 10, 2018, 06:03:46 PM]


All the stories in the forum are most important. by ZeroMax
[July 01, 2018, 09:44:53 PM]


Parallax CR Servo with Built-In Encoder by 1 what
[June 28, 2018, 12:13:32 PM]


He's Back...BoB V3.0 by k120189
[June 21, 2018, 07:03:17 PM]


Funding by MEgg
[June 21, 2018, 04:34:36 PM]


Robot Hive by jinx
[June 12, 2018, 03:58:03 AM]


Hi, Mushroom glue here. by Andrewsz
[June 11, 2018, 12:23:20 AM]


CZ-1 2.0: problems, calibration and printing, part2 by MEgg
[June 10, 2018, 06:10:53 AM]


Raspberry Pi / Python eBook - free for the next 11 hours! by MEgg
[June 04, 2018, 05:21:07 PM]

* Recent Posts

NASA bot by ossipee
[August 05, 2018, 06:52:36 PM]


Updates 20180728 :- new Steppers - Perfboard Migration - wheel inserts by Gareth
[July 28, 2018, 09:57:11 AM]


Re: Android Adware by Pittmanz
[July 24, 2018, 12:40:23 AM]


Re: Greetings! by Pittmanz
[July 24, 2018, 12:39:20 AM]


Re: Sloooow progress on the I3 clone by KingBeetle
[July 16, 2018, 04:20:59 AM]


Re: Sloooow progress on the I3 clone by MEgg
[July 15, 2018, 08:44:53 AM]


Re: ESP32 - Stepper - LIDAR - Platform by jinx
[July 15, 2018, 04:58:51 AM]


Re: MKS Gen L 1.0 by jinx
[July 15, 2018, 03:55:57 AM]


Re: Sloooow progress on the I3 clone by jinx
[July 15, 2018, 03:46:18 AM]


Re: MKS Gen L 1.0 by KingBeetle
[July 14, 2018, 04:05:49 PM]


Re: Sloooow progress on the I3 clone by KingBeetle
[July 14, 2018, 11:13:35 AM]


Re: ESP32 - Stepper - LIDAR - Platform by 1 what
[July 14, 2018, 07:02:46 AM]


Re: Sloooow progress on the I3 clone by MEgg
[July 14, 2018, 05:38:34 AM]


Re: Sloooow progress on the I3 clone by KingBeetle
[July 14, 2018, 03:45:21 AM]


Re: ESP32 - Stepper - LIDAR - Platform by jinx
[July 14, 2018, 02:49:04 AM]