This repository has been archived by the owner on Mar 18, 2020. It is now read-only.
forked from LeoRover/core2_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
86 lines (73 loc) · 1.48 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef LEO_FIRMWARE_UTILS_H_
#define LEO_FIRMWARE_UTILS_H_
#include "hFramework.h"
#include "ros.h"
#include "std_msgs/Int16.h"
#include "std_msgs/UInt16MultiArray.h"
#include "params.h"
inline float clamp(float value, float limit)
{
if (value > limit)
return limit;
else if (value < -limit)
return -limit;
else
return value;
}
template<class T>
class CircularBuffer
{
T *values_;
size_t size_;
size_t iter_;
public:
CircularBuffer(uint16_t size)
: size_(size),
iter_(0),
values_(new T[size])
{ }
T push_back(T val)
{
T tmp = values_[iter_];
values_[iter_++] = val;
if (iter_ >= size_)
iter_ = 0;
return tmp;
}
};
class ServoWrapper
{
int num;
int per;
IServo& servo;
public:
ServoWrapper(int num, IServo& servo)
: num(num),
servo(servo) {}
void angleCallback(const std_msgs::Int16& msg)
{
if (per!=SERVO_PERIOD)
{
servo.setPeriod(SERVO_PERIOD);
per=SERVO_PERIOD;
}
servo.rotAbs(msg.data);
#ifdef DEBUG
Serial.printf("[servo%dAngleCallback] angle: %d\r\n", num, msg.data);
#endif
}
void pwmCallback(const std_msgs::UInt16MultiArray& msg)
{
if (msg.data_length >= 2){
per=msg.data[0];
servo.setPeriod(msg.data[0]);
servo.setWidth(msg.data[1]);
#ifdef DEBUG
Serial.printf("[servo%dPWMCallback] period: %d width: %d\r\n", num, msg.data[0], msg.data[1]);
} else {
Serial.printf("ERROR: [servo%dPWMCallback] data array should have 2 members\r\n", num);
#endif
}
}
};
#endif