🛸 Adds servo config

This commit is contained in:
Rune Harlyk
2024-06-06 21:53:41 +02:00
committed by Rune Harlyk
parent f5d9cea236
commit e81beeb36b
4 changed files with 70 additions and 17 deletions
+5 -5
View File
@@ -173,11 +173,11 @@ export type CameraSettings = {
export type servo = {
channel: number;
name: string;
direction: number;
inverted: boolean;
angle: number;
min_pwm: number;
max_pwm: number;
min_angle: number;
max_angle: number;
center_angle: number;
// min_pwm: number;
// max_pwm: number;
// min_angle: number;
// max_angle: number;
};
@@ -8,13 +8,16 @@
<div class="flex gap-2 items-center">
Is inverted <input type="checkbox" checked={servo.inverted} class="checkbox"/>
</div>
<div>
Middle position <input type="number" bind:value={servo.center_angle} class="input input-bordered input-sm max-w-xs"/>
</div>
<div class="relative mb-6">
<label for="labels-range-input" class="sr-only">Labels range</label>
<input id="labels-range-input" type="range" value="1000" min="100" max="1500" class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700">
<span class="text-sm text-gray-500 dark:text-gray-400 absolute start-0 -bottom-6">Min ($100)</span>
<span class="text-sm text-gray-500 dark:text-gray-400 absolute start-1/3 -translate-x-1/2 rtl:translate-x-1/2 -bottom-6">$500</span>
<span class="text-sm text-gray-500 dark:text-gray-400 absolute start-2/3 -translate-x-1/2 rtl:translate-x-1/2 -bottom-6">$1000</span>
<span class="text-sm text-gray-500 dark:text-gray-400 absolute end-0 -bottom-6">Max ($1500)</span>
<input id="labels-range-input" type="range" bind:value={servo.angle} min="0" max="180" class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700">
<span class="text-sm text-gray-500 dark:text-gray-400 absolute start-0 -bottom-6">0</span>
<span class="text-sm text-gray-500 dark:text-gray-400 absolute start-1/2 -translate-x-1/2 rtl:translate-x-1/2 -bottom-6">90</span>
<span class="text-sm text-gray-500 dark:text-gray-400 absolute end-0 -bottom-6">180</span>
</div>
<button class="btn btn-ghost rounded-btn">Sweep range</button>
<button class="btn btn-neutral btn-sm">Sweep range</button>
</div>
@@ -16,7 +16,7 @@
{
channel: 0,
name: "Front right hip",
direction: direction,
inverted: false,
angle: angle,
min_pwm: min_pwm,
max_pwm: max_pwm,
+55 -5
View File
@@ -11,14 +11,66 @@
#define SERVO_CONFIG_FILE "/config/deviceConfig.json"
#define SERVO_CONFIGURATION_SETTINGS_PATH "/api/servo/configuration"
struct servo_t
{
String name;
int8_t channel;
bool inverted;
int16_t angle;
int16_t center_angle;
};
class ServoConfiguration {
public:
int32_t servo_oscillator_frequency {SERVO_OSCILLATOR_FREQUENCY};
int32_t servo_pwm_frequency {SERVO_FREQ};
static void read(ServoConfiguration &settings, JsonObject &root) {}
std::vector<servo_t> servos_config;
bool is_active {false};
static void read(ServoConfiguration &settings, JsonObject &root) {
root["is_active"] = settings.is_active;
root["servo_pwm_frequency"] = settings.servo_oscillator_frequency;
root["servo_oscillator_frequency"] = settings.servo_oscillator_frequency;
JsonArray servos = root["servos"].to<JsonArray>();
for (auto &servo : settings.servos_config)
{
JsonObject servo_config = servos.add<JsonObject>();
servo_config["name"] = servo.name;
servo_config["channel"] = servo.channel;
servo_config["inverted"] = servo.inverted;
servo_config["angle"] = servo.angle;
servo_config["center_angle"] = servo.center_angle;
}
}
static StateUpdateResult update(JsonObject &root, ServoConfiguration &settings) {
settings.is_active = root["is_active"];
settings.servo_pwm_frequency = root["servo_pwm_frequency"];
settings.servo_oscillator_frequency = root["servo_oscillator_frequency"];
settings.servos_config.clear();
JsonArray servos = root["servos"];
if (root["servos"].is<JsonArray>())
{
int i = 0;
for (auto servo : servos)
{
JsonObject servo_config = servo.as<JsonObject>();
servo_t new_servo;
new_servo.name = servo_config["name"].as<String>();
new_servo.channel = servo_config["channel"];
new_servo.inverted = servo_config["inverted"];
new_servo.angle = servo_config["angle"];
new_servo.center_angle = servo_config["center_angle"];
settings.servos_config.push_back(new_servo);
i++;
}
}
return StateUpdateResult::CHANGED;
};
};
@@ -43,17 +95,15 @@ class ServoController : public Adafruit_PWMServoDriver, public StatefulService<S
}
void deactivate() {
isActive = false;
_state.is_active = false;
sleep();
}
void activate() {
isActive = true;
_state.is_active = true;
sleep();
}
bool isActive{false};
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;