protocol detected after 2 fixes
[u360gts.git] / src / main / tracker / interpolation.c
blobc864f2ba5bd474fcc44d98af646fc0d6cbbd4a2c
1 /*
2 * This file is part of u360gts, aka amv-open360tracker 32bits:
3 * https://github.com/raul-ortega/amv-open360tracker-32bits
5 * u360gts is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * u360gts is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with u360gts. If not, see <http://www.gnu.org/licenses/>.
20 #include "interpolation.h"
22 #define INT_ELEMENTS 10
24 iPoint_t iPoints[INT_ELEMENTS];
26 uint8_t INT_QSIZE;
27 uint8_t iQIn;
28 uint8_t iQOut;
30 void iInit(uint8_t n)
32 INT_QSIZE = n;
33 iQIn = 0;
34 iQOut = 0;
37 bool iPutPoint(uint32_t time,float heading,float speed){
38 iPoint_t point;
39 point.time = time;
40 point.heading = heading;
41 point.speed = speed;
42 if(iPut(point)) {
43 iGet();
44 iPut(point);
46 return iFull();
49 bool iFull(){
50 return (iQIn == (( iQOut - 1 + INT_QSIZE) % INT_QSIZE));
53 bool iPut(iPoint_t point){
54 if(iFull())
56 return true; /* Full */
59 iPoints[iQIn].time = point.time;
60 iPoints[iQIn].heading = point.heading;
61 iPoints[iQIn].speed = point.speed;
63 iQIn = (iQIn + 1) % INT_QSIZE;
65 return false; // No errors
68 bool iGet(){
69 if(iQIn == iQOut)
71 return true; /* Queue Empty - nothing to get*/
74 iQOut = (iQOut + 1) % INT_QSIZE;
76 return false; // No errors
79 iPoint_t iEval(float A){
81 int i;
82 int j;
83 int index_i = iQIn - 1;
84 int index_j = index_i;
85 iPoint_t l;
86 iPoint_t v;
87 v.heading = 0;
88 v.speed = 0;
89 for(i=1; i<INT_QSIZE + 1; i++)
91 index_i = (index_i + 1) % INT_QSIZE;
92 index_j = iQIn - 1;;
93 l.heading = iPoints[index_i].heading;
94 l.speed = iPoints[index_i].speed;
95 for(j=1; j<INT_QSIZE + 1; j++)
97 index_j = (index_j + 1) % INT_QSIZE;
99 if(index_j!=index_i)
101 l.heading = (l.heading * (A - iPoints[index_j].time * 1.0f))/(iPoints[index_i].time * 1.0f - iPoints[index_j].time * 1.0f);
102 l.speed = (l.speed * (A - iPoints[index_j].time))/(iPoints[index_i].time - iPoints[index_j].time);
103 //printf("%d %d %d %d %.2f %d %d %d\n",i,j,index_i,index_j,A,iPoints[index_i].time,iPoints[index_j].time);
106 v.heading = v.heading + l.heading;
107 v.speed = v.speed + l.speed;
109 v.time = A;
110 return v;