GetSpeed.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. % GetSpeed.m
  2. %
  3. % This function gets a list of vectors together with their timestamps and
  4. % returns a speed vector in degrees per second.
  5. %
  6. % input:
  7. % vecList - nx3 (x,y,z) list of vectors. Taken from GetCartVectors.m
  8. % time - timestamp of vectors in us (microseconds)
  9. % step - (optional, default=1) distance of samples for speed calculation
  10. %
  11. % output:
  12. % speed - speed in deg/sec
  13. function speed = GetSpeed(vecList, time, step)
  14. if (nargin < 3)
  15. step = 1;
  16. end
  17. c_maxDelta = 0.001;
  18. c_usToSec = 1000000;
  19. assert(size(time,1) == size(vecList,1), 'Provided number of vectors and timestamps should be the same')
  20. speed = zeros(size(vecList,1),1);
  21. for ind=step+1:size(vecList,1)
  22. dotProd = sum(vecList(ind,:) .* vecList(ind-step,:));
  23. assert(dotProd < 1+c_maxDelta, 'Provided vectors are not normalized');
  24. % account for double roundings
  25. if (dotProd > 1)
  26. dotProd = 1;
  27. end
  28. rads = acos(dotProd);
  29. degs = rads * 180 / pi;
  30. elapsedTime = (time(ind) - time(ind-step)) / c_usToSec;
  31. speed(ind - round(step/2)) = degs/elapsedTime;
  32. assert(speed(ind) >= 0 && ~isinf(speed(ind)), ['Provided timestamps are not monotonic ' num2str(ind)]);
  33. end
  34. end