Adding support for 16 channel PWM expansion to libraries

Related to the development libraries, released by Curt Binder
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Roberto has a custom 16 channel 12-bit PWM expansion that he makes for people from time to time. I see the code in for the standard PWM expansion with 6 channels. The 16 channel one uses the same chip, but is larger in size, physically, doesn't have a box for it yet (but I'm going to put mine in one), and needs custom code to address, and, by default runs on at 0x41 instead of 0x40 for the standard PWM expansion.

I have downloaded the dev branch of the libraries into the gibhub client and will make changes against that. I'm posting a thread here to keep track of what's going on with it and maybe to ask questions about things I don't understand with the RA_PWM.cpp file.

A few questions to start. I don't really get all of the "over 100 disables override" concept. What's going on with the override stuff and the if<= 100 return override value stuff? Also what's RA_STAR?

Should I just write a class wrapper for the 16 channel expansion how I'd want to use it at the moment and email it in and you guys could figure out if it's useful to scrape stuff out and incorporate since there seems to be some compatibility history in that file that I don't get?
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 »

RA_STAR is the next generation hardware being developed.

The way the override works you can set that value to 0-100 and it will override the default schedule and turn the leds to that %. If you want to disable the override you set it to 255 or really as you've seen, anything over 100.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

Is that like Easter egg hunting for devs??? :)
You found RA_STAR... lol
There is a lot under the hood that people just don't get to see.
I think the best way is to do this:
Add a feature to our Arduino IDE. This is located in the features.txt file inside the /update folder.
For the sake of progressing, lets call it like this:

Code: Select all

16CHPWMEXPANSION,ReefAngel.Add16ChPWM,16 Channel Dimming Expansion Module
There is also a git repository I just created a few days ago for this file.
When you add that to the features.txt file, the Arduino IDE will scan your code and look for that keyword (ReefAngel.Add16ChPWM) and it will enable the define 16CHPWMEXPANSION.
So, make sure that your INO code has this line:

Code: Select all

ReefAngel.Add16ChPWM();
We usually put it in the setup(), but anywhere in the code works... Even if it is commented out.
Now, you have the ground work to start changing the library. You have the define 16CHPWMEXPANSION that can be enabled or disabled.
After I replied to your PM, I realized that the idea I had will not work.
It will disable the existing dimming module. I think the idea is to make both work at the same time.
So, with the new ifdef 16CHPWMEXPANSION, you will need to add code to the libraries so that you can interface with your module.
I think you will need to add these defines to Globals.h:

Code: Select all

#define 16CH_PWM_EXPANSION_CHANNELS     		16
#define I2CPWM__16CH_PCA9685		0x41
And then add all the same methods we have for the regular dimming expansion module.
I think these are the basic ones you need:
Set16Channel
Get16Channel
Set16ChannelOverride
16ChExpansion
16ChExpansionWrite

Let me know what you think.
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 »

Got it. I'll do it that way.

BTW, I know your PM would disable the regular PWM expansion, but figured that would be a quick and dirty way to see if it worked and then I'd back it out and do the extra define stuff.

I'm looking in RA_PWM.cpp and see lots and lots of stuff in there with multiple constructors for each channel in the standard expansion. If it's OK I'm going to wrap those #ifdef PWMEXPANSION lines with an #ifdef 16CHPWMEXPANSION one and not duplicate the code for the first 6 channels since they'll be the same. What do you think?

Also, I may try adding in the ability to use all 12 bits as a constructor argument to allow it to go from 0 to 4095 if specified. The only place all 12 bits will matter to me is when the lights are on very dimly. In that case the difference between 1% and 2% is a big jump in intensity. The difference between 40% and 41%, as a for instance, is not visible, but 1 and 2 definitely is.

And while I'm at it I'm going to take the liberty to add my SmoothRamp function to all the channels. 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 »

lnevo wrote:RA_STAR is the next generation hardware being developed.

The way the override works you can set that value to 0-100 and it will override the default schedule and turn the leds to that %. If you want to disable the override you set it to 255 or really as you've seen, anything over 100.
If that's what override does, then how could you have the default schedule ever set anything between 0 and 100 and keep running the "schedule" because it would think it was between in override status if it was between 0 and 100.
User avatar
heatdissipation
Posts: 52
Joined: Wed Aug 14, 2013 5:25 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by heatdissipation »

This is awesome. I agree that 0-4095 is definitely better. I also have the 16 channel expansion, I haven't had much time lately to work with it, I just have it on a simple parabola program right now. I would like to keep the standard pwm expansion in my libraries as it runs my four dc pumps. I'll definitely be following and helping as much as I can.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

I assume that these in Globals.h have something to do with detecting when something is in override state?

Code: Select all

#define OVERRIDE_CHANNEL0		2
#define OVERRIDE_CHANNEL1		3
#define OVERRIDE_CHANNEL2		4
#define OVERRIDE_CHANNEL3		5
#define OVERRIDE_CHANNEL4		6
#define OVERRIDE_CHANNEL5		7
If so, since I don't really know what they're doing should I add my own set of OVERRIDE_16CH_CHANNEL0, etc defines or should I just add channels 6-15 at the end of that block of OVERRIDE defines?
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Also this section looks like all the bits up to 128 are used. Can I pick 256 for 16CHPWMEXPANSION?

Code: Select all

#ifdef PWMEXPANSION
	#define PWMEbit		1
#else
	#define PWMEbit		0
#endif  // PWMEXPANSIO
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 »

The override uses a different variable to store the percentage and that variable overrides the default
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

Code: Select all

#define OVERRIDE_CHANNEL0      2
This define is used by the internal webserver to identify which channel you are overriding when you call http://ipaddress:2000/pox,y
x is the channel and y is the value to override.
So, you will need to add all the 16 channels to the end of the list and make sure to leave OVERRIDE_CHANNELS as the last one.

Code: Select all

#define PWMEbit      1
This define is used by the portal and other apps to identify that you have the dimming expansion module activated in your code. I don't think you need to mess with that at this point.
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

lnevo wrote:The override uses a different variable to store the percentage and that variable overrides the default
Correct...
Check this piece of code:

Code: Select all

byte RA_PWMClass::GetChannelValue(byte Channel)
{
	if (ExpansionChannelOverride[Channel]<=100)
		return ExpansionChannelOverride[Channel];
	else
		return ExpansionChannel[Channel];
}
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 »

rimai wrote:

Code: Select all

#define OVERRIDE_CHANNEL0      2
This define is used by the internal webserver to identify which channel you are overriding when you call http://ipaddress:2000/pox,y
x is the channel and y is the value to override.
So, you will need to add all the 16 channels to the end of the list and make sure to leave OVERRIDE_CHANNELS as the last one.
OK. The way that this one is written, what would happen if, for instance, someone had two of the 6 channel expansions?
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

Also, would I break the non-plus versions for everyone if I changed PWMSlope and PWMParabola and a new one I'd add called PWMSmoothRamp to int's rather than bytes by taking up more space?
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

Only 1 6ch dimming module is supported at this moment.
You should be able to create PWMSmoothRamp. The compiler should be smart enough to not included if the function is never called.
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, 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 »

I'm learning about github a bit and talking to my officemate who is an old-time Linux guy about doing commits to github. I had pulled down the dev branch of your repo yesterday to work on it, but didn't fork or anything yet.

What I'm thinking about doing is forking your Libraries dev branch and adding changes a small bite at a time, like just adding a new PWMSmoothRamp function without touching anything else, and then doing a pull request for small parts so it doesn't end up being a big burden on checking things.

Would you be open to pulling in changes like that? I'd probably add that one in, then a few basic functions and defines mentioned above for using the 16ch PWM module, and then some changes to the DCPump (and ReefAngel) to add in the idea of thresholds where the pumps can have values of 0 speed, or threshold to max, but not values inbetween.
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Adding support for 16 channel PWM expansion to libraries

Post by binder »

just make sure you do the pull requests on the dev branch. that way it can be tested easier. but sounds like you have the right idea. you will want to fork the repository in order to create the pull requests though.
make small changes and make sure it compiles and runs before you commit and create a pull request. :-)


Sent from my iPad mini
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

The way you think is good :)
Makes it manageable and easy to understand what you are doing.
So, for every bit of things you do, make a new commit and add a comment, that way when I look at the commit history, I know what you did for that particular commit.
You can have a few commits in every pull request too.
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 »

Great. I think I get it. I'll use the dev branch. I'll fork the Libraries and FeaturesAndUpdateFiles ones because I need to add to feature.txt to get the 16CH stuff working how Roberto suggested above.

I'm a github novice. I've used svn, hg, and cvs at work, but never git or github, so I'm trying to get it right. So far I've only cloned those repos by dragging the URLs to the Windows GitHub client and done my work from there, but I'll fork it and start from there. I tried to hit commit on my cloned version thinking it might be smart enough to fork it at the point where I cloned it and commit the change to my fork, but it just gave me an error because it was apparently trying to commit to your real dev branch.
KRavEN
Posts: 104
Joined: Sun Mar 17, 2013 8:21 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by KRavEN »

I had done code for this in one of my branches a while back if you want to use some of what I did.

https://github.com/brunnels/Libraries/t ... RA_PCA9685

Also changes to Globals and InternalEEPROM

https://github.com/brunnels/Libraries/t ... rnalEEPROM
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

KRavEN, I'll try to figure it out. I was planning to add functions to RA_PWM to mimic the 6 channel ones surrounded by appropriate defines so that the 16 channel module would be familiar to people already running the 6 channel one.

The reason is mostly selfish, on my part, so that I can ask questions about my 16 channel one and get answers from people who know how the 6 channel one is supposed to work. 8) But I think it would also make the code simpler to maintain if it all looks the same.
AlanM
Posts: 263
Joined: Wed Jan 01, 2014 7:26 am

Re: Adding support for 16 channel PWM expansion to libraries

Post by AlanM »

How much would it break if I set the 16 channel one to ints instead of bytes and everyone could be told to cast the outputs of the PWMSlope and PWMParabola and ReefCrest type functions to ints if they want to send them to the 16 channel PWM expansion? Then we could get 12-bit values in there.

I also haven't gotten my head around yet how the 6 channel expansion knows that it's being sent percents when pump modes are called and not raw values out of 255 or how the 6 channel one is using 8 bit numbers at all since I think the chip supports 12bit. Where should I be looking for that stuff?
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 »

You could always overload the functions :) The only tricky thing is you'll need to cast your arguments as int.
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.

I found the 100-255-4095 mapping part in RA_PWM.cpp

Code: Select all

	
        Wire.write((int)(data*2.55));			// send the data
	Present=Wire.endTransmission();		// stop transmitting
	if (cmd<PWM_EXPANSION_CHANNELS) ExpansionChannel[cmd]=data;
	// Also send data to new module PCA9685
	int newdata=(int)(data*40.95);
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

lnevo wrote:You could always overload the functions :) The only tricky thing is you'll need to cast your arguments as int.
Overloading won't break anything :)
All you have to do is declare the same function with a different size.
For example... We already have this:
SetChannel(byte Channel, byte Value)
You can declare another one like this:
SetChannel(byte Channel, int Value)

This way, when you use the code like this:
byte a=10;
SetChannel (0,a);
It will use the first one.
And if you use like this:
int a=10;
SetChannel (0,a);
It will use the second one.

The compiler is smart enough to find out which variable size you are using and uses the correct one.
Roberto.
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 look at the RA_NokiaLCD library, you will see a bunch of overloads of the same function as a reference for you to wrap your head around the idea.
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 »

Awesome. I did that for the DC pump constructor to add in my idea of a threshold so it doesn't break uses that don't include the threshold argument.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Adding support for 16 channel PWM expansion to libraries

Post by rimai »

I went ahead and merged the request, but had to make a small change due to conflict from a pending merge from brunnels.
It was just a comment he added, so no big deal.
I also changed the default and initial threshold to 30.
Great stuff :)
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 »

I'm pretty excited to be doing this. 8) Thanks!

I commented on the threshold of 30 thing on github, but I think it should be 0 so that the default is essentially the previous default, which was actually 0. Then current code will work the same as it always did, and if someone wants to set it to 30 to use the new feature, they can. Apparently the vortechs can run all the way down to 0? At least that was lnevo's objection when I proposed adding the threshold right to the wave function outputs.
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 »

Yeah I use the library modes when setting my vortech in custom modes. The threshold should be set in the dcpump class not in the global function...roberto can you clarify?
Post Reply