Adding support for 16 channel PWM expansion to libraries

Related to the development libraries, released by Curt Binder
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by lnevo »

Those functions all take a high and low argument so why should it change anything if you have to specify the arguments? You confused me?

If i say 0,100 i would expect it to still return 0-100...if i said 0,4096 i would not expect a 0-100 return...
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Lee, here's a for instance.

The current version of PWMSlope expect a byte for start, a byte for end. Bytes can only go up to 255. It returns a byte between start and end, just like you said.

However, the function that actually writes the output of the PWMSlope function to hardware is called "Expansion" in the RA_PWM.cpp file. That function makes it obvious that what it actually expects to see coming from the PWMSlope function is not a byte from 0 to 255, but one from 0 to 100. It multiplies the number that you try to set the channel to by 2.55 in the case of the old PWM expansion or by 40.95 in the case of the new one because the new one uses a chip with more bits.

This means that, in the case of the new chip, you're reducing the resolution of that chip by a factor of 41 by using the stock PWMSlope function with bytes in and bytes out from 0 to 100 and getting only 100 levels of dimming rather than 4096 levels. A huge diminishment of capability.

So in order to get that back, I'm going to make the PWMSlope function still accept 0 to 100 as an input for the start and end parameters, but as soon as the function is called with that, I'm going to scale those percentages to 12-bit values between 0 and 4095 and then do all the math with the current time of where I am along the slope. Then I'm going to output a number between 0 and 4095 which can be written directly to the chip with an overloaded version of the Expansion function which accepts ints instead of bytes as arguments without any loss of precision. That version won't multiply the percent by 2.55 or 40.95. It will scale back the 4095 to 255 for the older chipset and write it to the chip.

I'll keep the old byte version of Expansion around to be used by DCPump classes which seem fine to do in percents. I doubt anyone can claim to need 4096 levels of pump speed control, but at low light levels the difference between 1 and 2 out of 100 is really visible while the difference between 1 and 2 out of 4096 is invisible.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

By the way, since I'm keeping it totally separate from the code for the 6 channel expansion, I don't see why someone couldn't have both at the same time.

I thought about trying to make the addresses for the 16 channel one an array so that in the future a whole bunch of them could be added and someone could do something really silly like running 64 channels of LED lights on 4 16ch PWM expansions to make clouds move from side to side or make the sun rise in the east and set in the west. 8)
Meldrath
Posts: 20
Joined: Mon Mar 17, 2014 1:07 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by Meldrath »

Why not overload the Expansion function to accept an int from an overloaded PWMSlope function? Or...
You could overload PWMSlope and call the expansion function directly from the PWMSlope and not have to deal with conversions per se?

Or will that not work in this case?

Further, I have Kraven's library fully working on my own chips, so if you need any help let me know.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by lnevo »

I agree with overloading Expansion if possible but I haven't looked at the code at all... so I'll leave it to your implementation...
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Overloading Expansion would be fine if all I wanted to do was let people use their own code in their .ino file to write whatever integer they wanted to the 6-channel expansion. It would probably work ok, and I'd have the 16 channel expansion doing 12 bits like I wanted, but that wouldn't help anyone else.

If I overloaded PWMSlope, which I tried to do, to accept either ints or bytes (it currently accepts bytes) the only one that would get called all the time would be the int version unless users explicitly cast the argument to PWMSlope as a byte. When the only version that exists is one that accepts bytes, then the runtime doesn't care if you're giving it an argument of 50, for instance, which is technically an int. It casts it to a byte automatically and does the right thing. However, if now you made two versions, an int version and a byte version, the int one will get called and not the byte. You'd have to do the argument as "(byte)50" in order to get the current byte version, which would violate what I'm trying to do, which is make it work better for everyone with existing .ino files without requiring them to change it all around or learn a new way to do it.

By just overloading Expansion you'd also be stuck with 100 levels of dimming on the relay box and the lowATOport. And you'd just get 100 levels on the convenience functions like PWM.ChannelPWMSlope().

Anyway, TL;DR version, yes it would have been pretty easy to just make a 16 channel version of the Expansion function, make it 12-bit, and call that from overloaded PWMParabola functions, or whatever, it wouldn't help anyone else, so I decided to make the attempt to integrate the 16 channel expansion as fully as possible into the existing codebase. To write in the support for everything that I can figure out that the 6 channel expansion does. And in the process, to change around the internals of how the various PWM chips are called so that everyone's would work a little better.
Meldrath
Posts: 20
Joined: Mon Mar 17, 2014 1:07 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by Meldrath »

As an fyi, to do that, you're going to have to either create a new call/write methods or you're going to have to change the existing ones. (I went with adding a new expansion/pwm slope/pwm parabola methods)
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Yep. Already done and checked in to my device branch of the reefangel fork on github.
89delta
Posts: 163
Joined: Mon Oct 15, 2012 7:21 pm
Location: Leesburg, GA

Re: Adding support for 16 channel PWM expansion to libraries

Post by 89delta »

So since the dimming expansion uses the PCA9685 chip that means I could use the Adafruit Servo Breakout Board that I already have correct. Don't care for all 16-channels though as 6 would be plenty. What would I need to do to get it up and running? I already made the usb cable for it with the correct pinouts.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

89delta wrote:So since the dimming expansion uses the PCA9685 chip that means I could use the Adafruit Servo Breakout Board that I already have correct. Don't care for all 16-channels though as 6 would be plenty. What would I need to do to get it up and running? I already made the usb cable for it with the correct pinouts.
Seems like you could do that. You'd just have to know what address the Adafruit board is on and tweak the library files appropriately. It should be identical calls to talk to it as it is to talk to the one Roberto makes. It would be the defines in Globals.h for I2CPWM_PCA9685 which by default uses the address of 0x40 for his 6 channel PWM expansion. I don't know which board you have, but I bet it has jumpers to set the address.

That being said, I'm not sure what other customization Roberto does to get the outputs right for the range of things that people want to do with them. For instance, I know he has a way of doing 10V pwm or 5V pwm outputs and also has some filters that can convert those PWM signals into 10V or 5V analog DC voltage signals and he supplies pins to let you just pick which of those you want to have coming out. That's a pretty convenient set of additions.
89delta
Posts: 163
Joined: Mon Oct 15, 2012 7:21 pm
Location: Leesburg, GA

Re: Adding support for 16 channel PWM expansion to libraries

Post by 89delta »

The default address is 0x40 for the Adafruit Servo Breakout board and 0x41 with A0 jumper soldered. So the board should work without any kind of modifications to it or the library then correct. Thanks for the input/help.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Quick update on this. The code all compiles and is in my github repo. I've pulled down the upstream stuff from Roberto and merged it all in. I'm going to load it on my RA+ tonight and see what it does over the next couple of days. One thing I discovered is that C++ doesn't like defined names to start with numbers, so I had to replace "16" with "SIXTEEN" everywhere, heh.

Also, Roberto, if you read this, if I changed the RA_Net size from 26 to 42 to accommodate 16 channels of dimming expansion in Globals.h will that break stuff?
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

If you are going to provide complete compatibility for all 16 channels, there is a lot more to it than just the RANet size.
Anything related to PWM will need to be updated, including a few that pops my mind: PWM channels, PWM override, wifi xml, wifi json, RANet array, LCD screens and internal memory locations.
Roberto.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

OK. Got PWM channels and PWM override already in. I added it in a few small spots of LCD screens variables and internal memory locations, but don't know exactly what I'm doing with those. Didn't do anything to RANet array or the wifi stuff. Will at least look at that.

Thanks.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

What do you think would need to be stuck in the internal memory locations, by the way? I don't see much in there for the regular 6 channel PWM expansion.

Basically I'm finding places where the 6 channel one is referenced and defines for PWMEXPANSION and doing the same thing for the 16 channel one everywhere I can.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

Yes, the internal memory has 3 locations for each channel.

Code: Select all

#define Mem_B_PWMSlopeStart0      VarsStart+58
#define Mem_B_PWMSlopeEnd0	      VarsStart+59
#define Mem_B_PWMSlopeDuration0   VarsStart+60
#define Mem_B_PWMSlopeStart1      VarsStart+61
#define Mem_B_PWMSlopeEnd1	      VarsStart+62
#define Mem_B_PWMSlopeDuration1   VarsStart+63
#define Mem_B_PWMSlopeStart2      VarsStart+64
#define Mem_B_PWMSlopeEnd2	      VarsStart+65
#define Mem_B_PWMSlopeDuration2   VarsStart+66
#define Mem_B_PWMSlopeStart3      VarsStart+67
#define Mem_B_PWMSlopeEnd3	      VarsStart+68
#define Mem_B_PWMSlopeDuration3   VarsStart+69
#define Mem_B_PWMSlopeStart4      VarsStart+70
#define Mem_B_PWMSlopeEnd4	      VarsStart+71
#define Mem_B_PWMSlopeDuration4   VarsStart+72
#define Mem_B_PWMSlopeStart5      VarsStart+73
#define Mem_B_PWMSlopeEnd5	      VarsStart+74
#define Mem_B_PWMSlopeDuration5   VarsStart+75
Roberto.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Ah, right. I didn't make the connection that there were three for each channel.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

These are in and seem to be working well for my custom dimming module. I submitted a pull request against /dev tonight after a few months of work off and on on various things that I considered to be prerequisites to making these work how I wanted them to work. They work in DCPump, PWM, with all of the overrides, and also report over Wifi.

They are currently in https://github.com/amunter/Libraries/tree/issue161 if someone wanted to download the .zip and play, but it should be in the dev branch soon, I imagine.

I recommend that if someone is really interested in seeing what this little controller can do they should take a crack at adding something to the code. I "get it" way more than I would have if I hadn't set out to add a feature and feel pretty confident now that I'll like what I get it to do. 8)
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Oh, and the cool thing is that binder already has it pretty much working in the Android app beta! Talk about service!
dlplunkett44
Posts: 74
Joined: Mon Aug 05, 2013 3:16 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by dlplunkett44 »

I would really love to have this. Is it completely up and running now? Can all 16 channels be addressed and be dimmed at 12 bit resolution? How can I purchase one and for how much?
THANKS!
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by lnevo »

12 bit resolution is in the dev libraries and working great. You now get full 8 bit resolution on the built-in ports and 12 bit on the current dimming modules. Some older ones may still he 8 bit. PM Roberto (rimai) to purchase a 16channel module.
User avatar
cosmith71
Posts: 1437
Joined: Fri Mar 29, 2013 3:51 pm
Location: Oklahoma City

Re: Adding support for 16 channel PWM expansion to libraries

Post by cosmith71 »

Could someone post a guide on how to work the 12 bit dimming?

Thanks,

--Colin
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by lnevo »

If your using InternalMemory you don't have to do anything. If your setting them manually with the parabola or slope functuons you can just add HighRes to the function name.

For set and get functions, just add Raw at the end.
User avatar
cosmith71
Posts: 1437
Joined: Fri Mar 29, 2013 3:51 pm
Location: Oklahoma City

Re: Adding support for 16 channel PWM expansion to libraries

Post by cosmith71 »

How does that work with the standard PWM ports? Still use PWMSlopeHighRes? How will it know the difference between, say, 50% and 50?

I guess the Portal and the RA Status app still use bytes to store the values for the PWM Expansion module? They're rolling over every 255.

Thanks,

--Colin
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by lnevo »

Yes GetValue() and the portal will get the byte value. But if you want to write a high res number then you'd use SetDaylightRaw(). Then when it writes it will scale it down to 0-255. The HighRes functions all output 0-4095 (12 bit range)
User avatar
cosmith71
Posts: 1437
Joined: Fri Mar 29, 2013 3:51 pm
Location: Oklahoma City

Re: Adding support for 16 channel PWM expansion to libraries

Post by cosmith71 »

Disregard the second half of my previous statement. I was doing it wrong. :oops:

Got it running, but I've noticed I can't dim below a raw value of 21 with my LDD drivers. At 20 they shut off. Hardware limitation or software issue?

--Colin
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by lnevo »

Thats pretty damn good. All drivers need some amount of voltage in order to get going. The LDDs are low for sure. 21 is like .005v
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

Yeah, I think that is the minimum that your driver will take, which is pretty good in my opinion.
How does the LED look at 21?
Roberto.
User avatar
cosmith71
Posts: 1437
Joined: Fri Mar 29, 2013 3:51 pm
Location: Oklahoma City

Re: Adding support for 16 channel PWM expansion to libraries

Post by cosmith71 »

A little too bright for a moonlight, but I have 40 LED's on that channel. :D

--Colin
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Cool. I've never driven mine down that low on purpose, but I should try with some Raw commands. I know I used to do a slope from 0% to 3% over 3 hours using HiRes functions and it was really really dim at first. I don't know what the raw value was, though.

I owe Roberto a users guide to how the new dimming stuff works and how you'd access it if you wanted to get to it from the .ino file.
Post Reply