2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file servo.cpp
|
|
|
|
*
|
|
|
|
* http://rusefi.com/wiki/index.php?title=Hardware:Servo_motor
|
|
|
|
*
|
2018-03-17 18:24:04 -07:00
|
|
|
* SG90 pinout:
|
|
|
|
* brown GND
|
|
|
|
* red VCC
|
|
|
|
* orange PWM signal
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
* @date Jan 3, 2015
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "servo.h"
|
|
|
|
#include "pin_repository.h"
|
2018-02-25 18:17:59 -08:00
|
|
|
#include "engine.h"
|
|
|
|
|
|
|
|
// todo: remove this hack once we have next incompatible configuration change
|
|
|
|
#define TEMP_FOR_COMPATIBILITY GPIOA_0
|
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
|
|
|
THD_WORKING_AREA(seThreadStack, UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
2018-03-18 09:15:51 -07:00
|
|
|
static OutputPin pins[SERVO_COUNT];
|
2018-02-25 18:17:59 -08:00
|
|
|
|
|
|
|
static int countServos() {
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < SERVO_COUNT; i++) {
|
|
|
|
if (engineConfiguration->servoOutputPins[i] != GPIO_UNASSIGNED &&
|
|
|
|
engineConfiguration->servoOutputPins[i] != TEMP_FOR_COMPATIBILITY) {
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-25 18:37:42 -08:00
|
|
|
static scheduling_s servoTurnSignalOff;
|
|
|
|
|
|
|
|
// todo: extract common 'pin off' callback?
|
2018-03-18 09:15:51 -07:00
|
|
|
static void servoTachPinLow(OutputPin *pin) {
|
|
|
|
pin->setValue(false);
|
2018-02-25 18:37:42 -08:00
|
|
|
}
|
|
|
|
|
2018-02-25 18:17:59 -08:00
|
|
|
static msg_t seThread(void *arg) {
|
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("servo");
|
|
|
|
while (true) {
|
2018-03-18 09:15:51 -07:00
|
|
|
|
|
|
|
OutputPin *pin = &pins[0];
|
|
|
|
pin->setValue(1);
|
2018-02-25 18:59:15 -08:00
|
|
|
|
|
|
|
|
2018-03-17 18:51:46 -07:00
|
|
|
percent_t position = (currentTimeMillis() / 5) % 200;
|
|
|
|
if (position > 100)
|
|
|
|
position = 200 - position;
|
|
|
|
|
|
|
|
float durationMs = 0 + position * 0.02f;
|
2018-02-25 18:37:42 -08:00
|
|
|
|
2018-03-18 09:15:51 -07:00
|
|
|
scheduleForLater(&servoTurnSignalOff, (int)MS2US(durationMs), (schfunc_t) &servoTachPinLow, pin);
|
2018-02-25 18:37:42 -08:00
|
|
|
|
2018-02-25 18:17:59 -08:00
|
|
|
|
2018-02-25 18:37:42 -08:00
|
|
|
chThdSleepMilliseconds(19);
|
2018-02-25 18:17:59 -08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initServo(void) {
|
|
|
|
|
2018-03-18 09:15:51 -07:00
|
|
|
for (int i = 0; i < SERVO_COUNT; i ++) {
|
|
|
|
brain_pin_e p = engineConfiguration->servoOutputPins[i];
|
|
|
|
if (p != TEMP_FOR_COMPATIBILITY) {
|
|
|
|
pins[i].initPin("servo", p);
|
|
|
|
}
|
2018-02-25 18:17:59 -08:00
|
|
|
}
|
|
|
|
|
2018-03-18 09:15:51 -07:00
|
|
|
|
|
|
|
|
2018-12-27 06:40:40 -08:00
|
|
|
chThdCreateStatic(seThreadStack, sizeof(seThreadStack), NORMALPRIO, (tfunc_t)(void*) seThread, NULL);
|
2018-02-25 18:17:59 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
|