9#include <boost/thread.hpp>
17long mapData(
float x,
long in_min,
long in_max,
long out_min,
long out_max)
19 return (
long)((x - (float)in_min) * ((float)out_max - (
float)out_min) / ((
float)in_max - (float)in_min) + (float)out_min);
30RpiServo::RpiServo(
int pin,
float value):
38 pinMode(mPin, OUTPUT);
39 digitalWrite(mPin, LOW);
43 boost::thread::attributes attrs;
44 struct sched_param param;
45 param.sched_priority = 50;
46 pthread_attr_setschedparam(attrs.native_handle(), ¶m);
48 mThread = boost::thread(attrs, boost::bind(&RpiServo::threadLoop,
this));
61 digitalWrite(mPin, LOW);
65void RpiServo::threadLoop()
71 digitalWrite(mPin, HIGH);
72 delayMicroseconds(mValue);
73 digitalWrite(mPin, LOW);
76 boost::this_thread::sleep_for(boost::chrono::microseconds(20000 - mValue));
81void RpiServo::WriteValue(
float value)
85 else if (value > 180.0f)
88 mValue = mapData(value, 0, 180, -250, 1250) + 1000;