Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "dongfangMath.h"
#include <inttypes.h>
#include <avr/pgmspace.h>

// For scope debugging only!
#include "output.h"

// Debug
#include "uart0.h"

const int16_t SIN_TABLE[] PROGMEM = { (int16_t) (0.0 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.01745240643728351 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.03489949670250097 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.05233595624294383 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.0697564737441253 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.08715574274765817 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.10452846326765346 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.12186934340514748 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.13917310096006544 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.15643446504023087 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.17364817766693033 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.1908089953765448 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.20791169081775931 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.224951054343865 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.24192189559966773 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.25881904510252074 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.27563735581699916 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.29237170472273677 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.3090169943749474 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.32556815445715664 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.3420201433256687 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.35836794954530027 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.374606593415912 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.3907311284892737 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.40673664307580015 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.42261826174069944 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.4383711467890774 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.45399049973954675 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.4694715627858908 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.48480962024633706 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.49999999999999994 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.5150380749100542 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.5299192642332049 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.5446390350150271 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.5591929034707469 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.573576436351046 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.5877852522924731 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6018150231520483 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6156614753256582 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6293203910498374 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6427876096865393 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6560590289905072 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6691306063588582 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6819983600624985 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.6946583704589973 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.7071067811865475 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.7193398003386511 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.7313537016191705 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.7431448254773942 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.754709580222772 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.766044443118978 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.7771459614569708 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.788010753606722 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.7986355100472928 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8090169943749475 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8191520442889918 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8290375725550417 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8386705679454239 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8480480961564261 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8571673007021122 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8660254037844386 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8746197071393957 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8829475928589269 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.8910065241883678 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.898794046299167 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9063077870366499 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9135454576426009 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9205048534524403 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9271838545667874 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9335804264972017 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9396926207859083 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9455185755993167 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9510565162951535 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9563047559630354 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9612616959383189 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9659258262890683 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9702957262759965 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9743700647852352 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9781476007338056 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.981627183447664 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.984807753012208 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9876883405951378 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9902680687415703 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.992546151641322 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9945218953682733 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9961946980917455 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9975640502598242 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9986295347545738 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9993908270190958 * MATH_UNIT_FACTOR + 0.5),
    (int16_t) (0.9998476951563913 * MATH_UNIT_FACTOR + 0.5), (int16_t) (1.0
        * MATH_UNIT_FACTOR) };

const int16_t TAN_TABLE[] PROGMEM
    = { (int16_t) (0.0 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.017455064928217585 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.03492076949174773 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.0524077792830412 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.06992681194351041 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.08748866352592401 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.10510423526567646 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.1227845609029046 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.14054083470239145 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.15838444032453627 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.17632698070846498 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.19438030913771848 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.2125565616700221 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.23086819112556312 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.24932800284318068 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.2679491924311227 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.2867453857588079 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.3057306814586604 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.3249196962329063 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.3443276132896652 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.36397023426620234 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.3838640350354158 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.4040262258351568 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.4244748162096047 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.4452286853085361 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.4663076581549986 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.48773258856586144 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.5095254494944288 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.5317094316614788 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.554309051452769 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.5773502691896257 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.6008606190275604 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.6248693519093275 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.6494075931975106 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.6745085168424267 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.7002075382097097 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.7265425280053608 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.7535540501027942 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.7812856265067173 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.809784033195007 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.8390996311772799 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.8692867378162265 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.9004040442978399 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.9325150861376615 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.9656887748070739 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (0.9999999999999999 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.0355303137905694 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.0723687100246826 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.1106125148291928 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.1503684072210094 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.19175359259421 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.234897156535051 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.2799416321930788 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.3270448216204098 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.3763819204711734 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.4281480067421144 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.4825609685127403 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.5398649638145825 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.6003345290410504 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.6642794823505174 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.7320508075688767 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.8040477552714236 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.8807264653463318 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (1.9626105055051504 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.050303841579296 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.1445069205095586 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.2460367739042164 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.355852365823752 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.4750868534162964 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.6050890646938005 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.7474774194546216 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (2.904210877675822 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (3.0776835371752527 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (3.2708526184841404 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (3.4874144438409087 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (3.7320508075688776 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (4.010780933535842 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (4.331475874284157 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (4.704630109478451 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (5.144554015970307 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (5.671281819617707 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (6.313751514675041 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (7.115369722384195 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (8.144346427974593 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (9.514364454222587 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (11.430052302761348 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (14.300666256711896 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (19.08113668772816 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (28.636253282915515 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (57.289961630759876 * MATH_UNIT_FACTOR + 0.5),
        (int16_t) (32767) };

int16_t int_sin(int32_t arg) {
  int8_t sign;
  int16_t result;
  int16_t argp = arg / MATH_DRG_FACTOR;
  argp %= 360;
  if (argp < 0) {
    argp = -argp;
    sign = -1;
  } else {
    sign = 1;
  }
  if (argp >= 90) {
    argp = 180 - argp;
  }
  result = pgm_read_word(&SIN_TABLE[(uint8_t) argp]);
  return (sign == 1) ? result : -result;
}

int16_t int_cos(int32_t arg) {
  if (arg > 90L * MATH_DRG_FACTOR)
    return int_sin(arg + (90L - 360L) * MATH_DRG_FACTOR);
  return int_sin(arg + 90L * MATH_DRG_FACTOR);
}

int16_t int_tan(int32_t arg) {
  int8_t sign = 1;
  int16_t result;
  int16_t argp = arg / MATH_DRG_FACTOR;
  if (argp >= 90) {
    argp = 180 - argp;
    sign = -1;
  } else if (argp < -90) {
    argp += 180;
  } else if (argp < 0) {
    argp = -argp;
    sign = -1;
  }
  result = pgm_read_word(&TAN_TABLE[(uint8_t) argp]);
  return (sign == 1) ? result : -result;
}