Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: digital output for controlling lighting with HAL #2675

Merged

Conversation

shellixyz
Copy link
Collaborator

@shellixyz shellixyz commented Jan 22, 2018

This PR adds the possibility to control lighting with a digital output.

  • To use this functionality you need to build a custom firmware after defining the pin to use as output in the USE_LIGHTS macro (example on PIKOBLX to use the PWM5 output: #define LIGHTS_PIN PA1)
  • No overhead if the USE_LIGHTS macro is not defined
  • Lights flashing can be enabled on failsafe with the failsafe_lights setting and flashing on time and period can be configured with the failsafe_lights_flash_period and failsafe_lights_flash_on_time settings (unit: milliseconds).
  • The output can be switched on with the new LIGHTS flight mode.

Example application (don't forget the current limiting resistor(s) if you are using LEDs which don't have built-in current limiting):
schema_inav_lighting_power_schema

@DzikuVx
Copy link
Member

DzikuVx commented Jan 22, 2018

it's connected with #296

@DzikuVx
Copy link
Member

DzikuVx commented Jan 22, 2018

Would you belive I wanted to do it more than a year ago?
I think it would be nice to add additioanl level of abstraction from the start that will allow to use external driver over I2C for example like PCF8574 so it can be configurable if to use FC PWM outputs or extarnal device

@@ -56,6 +56,7 @@ typedef enum {
BOXCAMERA1 = 29,
BOXCAMERA2 = 30,
BOXCAMERA3 = 31,
BOXLIGHTS = 32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is BOXLLIGHTS = 13,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a mode in case somebody wants to use RGB and standard lighting at the same time but I can modify the code to use the LLIGHTS mode without a problem.

Copy link
Member

@DzikuVx DzikuVx Jan 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLIGHTS are not used, it's not connected with RGB LEDs at all. You mistake it with BOXLEDLOW

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BOXLLIGHTS stands for Landing Lights

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Now using old unused BOXLLIGHTS IDs.

@@ -70,6 +70,7 @@ static const box_t boxes[CHECKBOX_ITEM_COUNT + 1] = {
{ BOXCAMERA1, "CAMERA CONTROL 1;", 39 },
{ BOXCAMERA2, "CAMERA CONTROL 2;", 40 },
{ BOXCAMERA3, "CAMERA CONTROL 3;", 41 },
{ BOXLIGHTS, "LIGHTS;", 42 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ BOXLLIGHTS, "LLIGHTS;", 16 },

@shellixyz
Copy link
Collaborator Author

I believe it :)
About the abstraction I'm all for it but I'm not sure how to proceed.

@DzikuVx
Copy link
Member

DzikuVx commented Jan 22, 2018

I suppose we can skip abstraction for now.
By abstraction I ment additional 2 files. light.c write to abstraction and then abstraction uses either FC pwms or any new driver. So when you add new hardware, you do not change lights.c but only a "driver". We have this for rangefinder for example

@Linjieqiang
Copy link
Contributor

Why not use WS2812 LEDStrip as the lighting for fixedwing?

@DzikuVx
Copy link
Member

DzikuVx commented Jan 23, 2018

@Linjieqiang because with digital output you can connect and drive everything. Like 10W LED or IR emitter or anything else

return(true);
} else
return(false);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use standard 4 space indentation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I need to change my auto-indentation settings for iNav it is not the same I use for other projects.

@@ -314,6 +315,9 @@ void fcTasksInit(void)
setTaskEnabled(TASK_SERIAL, true);
#ifdef BEEPER
setTaskEnabled(TASK_BEEPER, true);
#endif
#ifdef LIGHTS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be #ifdef USE_LIGHTS - the USE_ prefix is used for this type of build flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I didn't because it is not only a flag it is used to define the pin to use. Maybe best to use two flags: USE_LIGHTS and LIGHTS_PIN ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Macros to use are now USE_LIGHTS, USE_FAILSAFE_LIGHTS and LIGHTS_PIN.

.taskName = "LIGHTS",
.taskFunc = lightsUpdate,
.desiredPeriod = TASK_PERIOD_HZ(100), // 100 Hz
.staticPriority = TASK_PRIORITY_MEDIUM,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

priority should be low.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I used the same priority as the buzzer/beeper because it is also used to flash lights on failsafe. Maybe the buzzer/beeper priority can be set to LOW too ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Task priority is now LOW.

@martinbudden
Copy link
Contributor

Good stuff. However I'm not sure if this should be called LIGHTS, since, as @DzikuVx , points out, you can connect anything to the output.

@kabturek
Copy link

Arduplane calls it relay switches

@shellixyz
Copy link
Collaborator Author

@martinbudden I called it LIGHTS because it is used to flash lights on failsafe. Not sure it would be relevant for something else in this context.

@shellixyz
Copy link
Collaborator Author

shellixyz commented Jan 23, 2018

@DzikuVx I had a go at the HAL please check out #2681.

Update: The HAL has been merged into this PR

@shellixyz shellixyz changed the title New feature: digital output for controlling lighting New feature: digital output for controlling lighting with HAL Jan 24, 2018
Copy link
Member

@DzikuVx DzikuVx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this

Copy link
Member

@digitalentity digitalentity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed a few things to think about.

[TASK_LIGHTS] = {
.taskName = "LIGHTS",
.taskFunc = lightsUpdate,
.desiredPeriod = TASK_PERIOD_HZ(100), // 100 Hz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't 100Hz an overkill? Do we want to update light status 100 times per second? I think 10Hz whould be enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed for the failsafe lights flashing code. As the default I set pulses the lights 100ms every second. It is what I use to keep battery usage low in case the model is lost and a long time is needed to find it. If we set it to 10Hz it won't be possible as it will make the lights either not light up or light up 100ms or 200ms each cycle depending on the time variability between task runs. The task priority is set to LOW it shouldn't be a problem.

@@ -0,0 +1,30 @@
#include "drivers/lights_hal.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the name lights_hal. We use _hal suffix in drivers that depend on ST's HAL library. This is clearly not the case. Maybe use lights_io name here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you like. I thought it would be appropriate as it is already used for WS2811 lights: light_ws2811strip_hal.c.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly my case - light_ws2811strip_hal.c is driver designed to work with ST's HAL library. That's why we have light_ws2811strip_stdperiph.c next to is (using ST's StdPeriph library). We don't depend on ST's CPU-support libraries, so let's rename.

void lightsUpdate(timeUs_t currentTimeUs)
{
UNUSED(currentTimeUs);
#ifdef USE_FAILSAFE_LIGHTS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we have USE_FAILSAFE_LIGHTS? Why not make it a config option rather than compile-time option?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would not be a setting changed often but you are right it would be better as a run-time setting. I will change that.

Copy link
Collaborator Author

@shellixyz shellixyz Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. This PR has been tested and should be ready to merge.

@digitalentity digitalentity added this to the 1.9 milestone Jan 26, 2018
@digitalentity digitalentity merged commit 873946d into iNavFlight:development Jan 26, 2018
@shellixyz shellixyz deleted the lighting_digital_output branch February 3, 2018 17:57
@krzysztofmatula
Copy link
Contributor

Heh, I was proposing something similar back in 2015 :)
cleanflight/cleanflight#1603

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants