Attiny25/45/85 FW Development Thread

Wauw Tom sounds great!

Amazing work Tom. Thanks. Are you charging by the hour?

Would be nice, but the code to generate the tables… isn’t very small on an attiny. It’s all floating-point and uses functions which cost a lot of space like cube roots, while the attiny only does integer math so it’d need to either load a floating-point library into ROM or manually simulate things with fixed-point.

However, it could probably be cut almost in half by adding a restriction and an inflection point. While the 7135 channel is ramping up, the FET is all zeroes. Then while the FET is ramping up, the 7135 is all 255s. These could be combined into one table with the repetitive bits removed, with a bit of extra logic added to handle it differently. It’d save space when the ramp is large, but it’d cost space when the ramp is small.

It doesn’t attempt to alter the ramp shape to exactly hit the boundary between channels. I’m thinking of perhaps making it do that, when it can, but I’m not sure I want to mess with it. It’s usually fairly simple to wiggle its parameters a little until it’s close, then tweak a few PWM values manually to make it exact.

I’m thinking it might be good to estimate the maximum lumens on the low side, since it’ll drop with voltage anyway. Maybe it does 5000 lumens on full cells, but only 2500 at 3.6V? So maybe it’d look better with a curve calculated for 2500.

There’s also debate about what the best curve shape is. Selfbuilt insists it’s a cube root, but I find that a logarithm tends to be really nice, and there are other curves between (fifth root, seventh root, etc). Some people even prefer a square root, but I find it’s not steep enough.

The only way to tell for sure is to try it, and that’s what you’re doing. :slight_smile:
(is also why I put in some commented-out presets for different curve shapes)

avr-libc looks decent but probably too large to be useful here. A simple float multiply is a function over 200 bytes. Add pow, divide, etc and you’ll quickly use up more program space than the tables.

Fixed point could be made to work within a small footprint. I agree that LUTs are better here as long they get the job done. But if there’s some interest, I can take a stab at the fixed point math. I’ve done much worse things before :slight_smile:

I dunno - I'm pretty happy with how the current table is performing across 1400 - 5000 lumens. Only thing that has to change is for a 7135 bank vs. a FET, as far as I can see. I'll do more testing tonight though.

The only point of doing it within the firmware would be to include more options than can be done with tables due to limited space. It doesn’t seem like that will be the case.

Since there are a lot of complaints regarding "short_click/medium_click" because of temperature dependency of otc measurement I suggest a temperature compensation for next driver versions.

Quick and dirty, can be improved, works in my driver.

// experimental temperature compensation for otc value
thermal_compensation = adc_init_and_read(ADC_MODE_TEMP);
if ( thermal_compensation > 76 )
thermal_compensation = (x * (thermal_compensation - 76));
else
thermal_compensation = 0;
...
if ( otc_voltage > (OTC_SHORT_PRESS - thermal_compensation) )
...

with x something between 4 and 8. Predefined OTC border values must be increased.

I haven’t had temp issues with mine, maybe because of the component choice, not sure. For me there is much more difference if the cell is fully charged or almost depleted. Charging the OTC with 2.8V or 4.2V makes more of a difference than temperature. I couldn’t be bothered to compensate for it though, having the voltage divider and OTC on the same pin presents it’s own set of challenges.

Something like Flashy Mike’s compensation routine should be enough.

Changed my mind… Going to play around a little with voltage compensation on my drivers, just to see if I can make it work nicely. If it works out maybe I can re-use the code for temperature compensation, gotta do a little more precise testing to see how much my drivers are affected by temp changes.

It’s not nearly as much of an issue on drivers with good components. However, that said, there are other complicating factors. For one, the most-affected drivers tend to have their OTC readings right at the edge of the usable range. So, there is sometimes no room to adjust; it’s already at the boundary. It would need calibration on a per-unit basis because the behavior varies with each individual driver. The MCU’s temperature readings alone can vary by a pretty large amount, so that “76” either needs to be configurable by the user or calibrated at the factory. As Mike C said, voltage is also an important factor. Getting a reliable temp+voltage measurement can take a few tries too, so that would delay boot-up until readings are taken. The scale factor “x” needs to be quantified and might not be linear, or might require floating-point math to get it even close to accurate (floating point or even an integer multiply can take a lot of ROM space).

Overall, it should be do-able on some drivers. But it might involve undesirable tradeoffs.

Anyway, the code is all there for anyone who wants to improve it. :slight_smile:

It is a bit frustrating when people have a lot of counter arguments but don’t even try. I did try on two high current triples and it works reliably. Without compensation the lights with bistro drivers (banggood) weren’t usable at all when hot. If calibration were necessary then even more without compensation. When this driver is used in BLF lights we must use whatever banggood put on the driver, and that are not always “good components”. The “undesirable tradeoffs” are already there, just read the threads about this lights.
Of course factor “x” and other parameters have to be quantified and I know the temperature readings differ between drivers, my test drivers have quite different readings in voltage and temperature. And probably there is no linearity, I said that it was just a quick and dirty hack, my first test of compensation. And nevertheless it works better than without compensation.
I also don’t understand all this fear of floating point math. With some tricks you could do a lot in fixed point which means integer with some shifting. A 16x16 bit multiplication takes less than 40 bytes program space, a 16/16 bit division less than 50 bytes. I squeezed a complete 2-channel ramping calculation for 256 steps in ATtiny with less than 200 bytes code, configurable for probably any light by only 1 parameter which can easily done by the user.

Awesome, could you put the code online?

I must admit, I was hoping to be proven wrong. Few things are more motivating than “someone is wrong on the internet”.

For people making individual lights, per-unit tweaking isn’t a big issue. A lot of cool things become possible when people can modify the code, and when space doesn’t need to be spent on runtime configuration options. Config options and whole-batch consistency only really matter when making larger amounts for more people. I think I’ve spent too much time on the large-batch approach, and should go back to the more enjoyable one-at-a-time approach where these things are feasible.

Just tried Bistro out. Made a light for a friend of mine; Red S2+ triple with copper heatsink and a Mtn-Fet+1 with Bistro on it. Really liking the reverse functionality and programability. I’m sure most people could get exactly what they want from this firmware. Awesome work ToyKeeper.

I noticed that the voltage blinkout seems to only blink the tenths. Someone in the Mtn thread asked about turbo step-up after the temperature of the light reduces. I’m curious about this as well. Seems it does not have this functionality upon testing.

By default, bistro uses a volts+tenths style battery readout. I think RMM’s drivers use a different style though, with 0 to 8 blinks representing about 12.5% of a full charge for each blink. It’s a compile-time option. You’d need to reflash it to change the indicator style.

About stepping back up, it should be able to in the right conditions, but those conditions are uncommon during normal use. Initially, it would overshoot its ideal level, wait for the temperature to drop, step back up and again overshoot, wait for the temperature to rise, then repeat in an oscillating pattern. It bounced. To avoid that, I simply made it steer slower. Not the greatest solution, but it worked okay with the X6 due to its large heat sink. On a smaller, higher-powered light, I honestly wouldn’t trust its thermal regulation much.

If you want to see the thermal behavior more clearly, you could put it on a medium mode, aim a hot blow dryer at it until it steps down, then switch the blow dryer to cool mode until the light ramps back up. Works faster if you pull the driver out so it’s floating in the air, though it still steers pretty slowly to avoid bouncing.

Here’s a visual representation. On the left is what I started with, reacting too fast and too far, bouncing around. In the middle is what bistro uses now, reacting slowly in a manner which overheats then settles (usually), but might overheat too far on a small triple. On the right is what it should ideally be doing, hitting the right level at the right time without exceeding any limits.

Ah. Thanks for the great explanation. Well, it definitely works as a safety feature as is, which is necessary, and I appreciate. I’d wager it’s better than an adjustable turbo timer.

The first step down stays pretty bright, and it does keep getting hotter. A few more step downs starts to stabilize the temperature. It continues to step down a couple more times to a pretty low level (like mode 2) and stays there. On this light, maybe a large initial step down might work well, then ideally it would wait and adjust the output in small increments based on the temperature, to maintain that temperature. A very slow, almost imperceptible ramping adjust after the initial drop, or just in general, would be super cool but that might be impractical due to program space or whatnot.

Good safety feature as is though. I think it may work better on this light if I set the initial trip point lower.

Regarding the Bistro thermal management: I just got a FET+1 driver from MTN with bistro flashed by Richard. I’m using it in a S2+ with single XPL emitter. I was trying to test the thermal management by monitoring the light output. With a cooling fan and light in medium mode (about 2.2A, one mode down from 100% in the 5 mode group) the outside temp stabilized around 111F. I then turned the fan off and the temp quickly went up and the driver stepped down when the outside got to around 123F. The driver stepped down to roughly 60% every 12 or so seconds, so it got down pretty quickly. It stepped down 3 or 4 times then stayed at that level. I then turned the fan back on and the flashlight cooled pretty quickly to 91F. I waited for several minutes, but it never stepped the output back up.

Does this seem normal? It’s hard to know exactly what the temp of the MCU is, but it seems like it would have cooled enough to get below the temp setpoint.

If it didn’t step back up, it’s probably still in the green zone on that middle graph. To avoid constant bouncing, it has a window of a few degrees where it won’t step up or down. If it gets above the window it steps down, and if it gets below the window it steps up, but if it stays in that zone, it won’t make any changes.

The MCU’s temperature units seem a bit odd. It gives me a reading of 74 for room temperature (70 F?), 87 if I hold a hair dryer on it at its hottest setting for a few minutes (135 F?), and 62 after resting in the freezer (10 F?). That seems to be something like 4 to 6 degrees F per thermal unit, and the no-change window is 2 of those units.

By default, it’s:

  • 79 or more: step down (95 F?)
  • 78: do nothing (90 F?)
  • 77: do nothing (85 F?)
  • 76 or less: step up (80 F?)

That could mean it needs to cool by 15 degrees F before it’ll step back up… which is a bit much. This is probably made worse by how slowly the heat transfers to the driver. Basically, the ceiling gets set a bit low in order to start the step-down sooner, but then when it later stabilizes it might end up too low.

Oh, and the readings differ by at least 5 thermal units between different MCUs of the same type. So, the hardcoded default is likely to be off by 25 F (or maybe more) due to manufacturing variances. I set the initial value to work just right on one light, then tried another identical light with identical code and it thought room temperature was “overheating”.

Anyway, I’d suggest reflashing it if you can, with the window set to maybe 1 unit instead of 2. That’s the minimum to avoid constant adjustments but small enough to make it step up easier. IIRC, I tried this and found it still adjusted too much, but your mileage may vary. Look for this part:

    if ((temp < maxtemp - 2) && (actual_level < output)) {
        actual_level ++;
    }

Soon.

That’s cool!

I’m rather targeting the less experienced modders who are not able to fiddle with code and like to use this driver for different lights - and the experienced modders who don’t want to flash the driver multiple times in order to find the best ramping curve.

Can’t believe I never seen this thread. Maybe because the thread title didn’t look interesting or just because it has to do with boards, drivers, programming and software stuff And all things I’m not able to do.

But some very interesting reads here. I’m not close to finish reading this thread yet though.