-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbot-pwm.cpp
50 lines (40 loc) · 1.48 KB
/
mbot-pwm.cpp
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
#include "mbot-vision/mbot-pwm.h"
#include <Arduino.h>
#include "mbot-vision/detection/detector.h"
#include "mbot-vision/common.h"
#define MV_PWM_FREQ 1024
#define MV_PWM_BITS 10 // 10 bits gives (1 << 10) = 1024 values
#define MV_PWM_OFF 32 // Frequency indicating no detection
#define MV_PWM_MIN 256
#define MV_PWM_MAX 1024
#define MV_PWM_RANGE (MV_PWM_MAX - MV_PWM_MIN)
MBotPWM::MBotPWM(Detector &detector)
: _detector(detector) {}
void MBotPWM::begin() {
xTaskCreatePinnedToCore(runStatic, "mBotPWM", 10000, this, 2, &_task, 1);
}
void MBotPWM::run() {
Serial.println("Starting PWM comms with MBot");
ledcAttachPin(MV_PWMX_PIN, MV_PWMX_CHAN);
ledcAttachPin(MV_PWMY_PIN, MV_PWMY_CHAN);
ledcSetup(MV_PWMX_CHAN, MV_PWM_FREQ, MV_PWM_BITS);
ledcSetup(MV_PWMY_CHAN, MV_PWM_FREQ, MV_PWM_BITS);
while (true) {
DetectedObject blob = _detector.get();
if (blob.detected) {
ledcWrite(MV_PWMX_CHAN, MV_PWM_MIN + (int)((float)MV_PWM_RANGE * blob.x));
ledcWrite(MV_PWMY_CHAN, MV_PWM_MIN + (int)((float)MV_PWM_RANGE * blob.y));
//ledcWrite(MV_PWMY_CHAN, (1 << (MV_PWM_BITS - 1)) + (int)((float)hsv.h * (360.0f / 256.0f)));
}
else {
ledcWrite(MV_PWMX_CHAN, MV_PWM_OFF);
ledcWrite(MV_PWMY_CHAN, MV_PWM_OFF);
}
delay(50);
}
}
void MBotPWM::runStatic(void *p) {
MBotPWM *mbot = (MBotPWM *)p;
mbot->run();
vTaskDelete(NULL);
}