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
|
|
|
*/
|
|
|
|
|
2019-01-03 20:51:29 -08:00
|
|
|
#include "engine.h"
|
|
|
|
|
2019-03-29 07:29:01 -07:00
|
|
|
#if EFI_SERVO || EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "servo.h"
|
|
|
|
#include "pin_repository.h"
|
2018-02-25 18:17:59 -08:00
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
2019-12-14 06:31:13 -08:00
|
|
|
// This thread calls scheduleForLater which eventually could trip the main trigger callback
|
|
|
|
// if self stimulation (heh) is enabled, which uses a TON of stack space.
|
|
|
|
// So this stack has to be pretty big, unfortunately.
|
|
|
|
THD_WORKING_AREA(servoThreadStack, UTILITY_THREAD_STACK_SIZE * 3);
|
2018-02-25 18:17:59 -08:00
|
|
|
|
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++) {
|
2019-05-05 13:43:07 -07:00
|
|
|
if (engineConfiguration->servoOutputPins[i] != GPIO_UNASSIGNED) {
|
2018-02-25 18:17:59 -08:00
|
|
|
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
|
|
|
|
2020-01-07 15:10:31 -08:00
|
|
|
engine->executor.scheduleForLater(&servoTurnSignalOff, (int)MS2US(durationMs), { &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];
|
2019-05-05 13:43:07 -07:00
|
|
|
pins[i].initPin("servo", p);
|
2018-02-25 18:17:59 -08:00
|
|
|
}
|
|
|
|
|
2019-07-13 07:32:36 -07:00
|
|
|
chThdCreateStatic(servoThreadStack, sizeof(servoThreadStack), NORMALPRIO, (tfunc_t)(void*) seThread, NULL);
|
2018-02-25 18:17:59 -08:00
|
|
|
}
|
2019-03-29 07:29:01 -07:00
|
|
|
#endif /* EFI_SERVO */
|
2015-07-10 06:01:56 -07:00
|
|
|
|