Shared Functions()

Discussions and file drops for Auggie
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

Found this function today written for MatLab (Created by Feng Cheng Chang); liked it and thought i'd convert it to Monkey script.
Returns the day of the week for a calendar date.

good example of math & table use.
Note: needed to convert "w" to integer to get the table string associated day.

source matlab code:

Code: Select all

function w = Wd(m,d,cy)
 m = m-2;  
 if m <= 0,&nbsp; m = m+12;&nbsp; cy = cy-1;&nbsp; end;
 c = fix(cy/100);&nbsp; 
 y = mod(cy,100);
 w = mod(d+fix(m*2.59)+fix(y*1.25)+fix(c*5.25),7);

%example
w = Wd(1,6,2016)
%returns
w=3
Converted to Monkey Script :

Code: Select all

global getday = function(m,d,cy)
{
day = table("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
&nbsp; &nbsp; &nbsp; &nbsp; m = m-2;&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; if (m <= 0)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = m+12;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cy = cy-1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;  c = math.floor(cy/100);&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp;  y = (cy % 100);
&nbsp; &nbsp; &nbsp; &nbsp;  w = ((d+math.floor(m*2.59)+math.floor(y*1.25)+math.floor(c*5.25)) % 7);
&nbsp; &nbsp; &nbsp; &nbsp;  w = ToInt(w);
&nbsp; &nbsp; &nbsp; &nbsp;  return day[w];
}; 
weekday=getday(1,11,2016); // todays date returned as #1 Monday
print("The day of the week was: "+weekday);
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

Been working on an analog clock function that takes the digital values and converts them to polar rotation, then I overlay a dxf file clock face. All cool stuff that can be done with scripting. :) (and then hopefully used to output to the machine controller)

Sorry I'm not very good at capturing the screen display and the correct seconds value,&nbsp; :-[ (I'm old)
Attachments
analogClock.JPG
GlennD
Old Timer
Posts: 80
Joined: Tue Oct 18, 2011 4:19 pm

Re: Shared Functions()

Post by GlennD »

Stole this shamelessly from the arduino web site and added it to MyGlobals.
You can use it to map a value from one set of numbers to another set.
Say you get a value from a potentiometer and want a duty cycle for PWM out.

Or use it to map spindle speed to the PWM out.

for instance:
SetPWMDuty_1(2,map(GlobalGet("SpindleSpeed"),8000,18000,0.1,100));
Assuming you have a range of 8000 to 18000 rpm on your spindle.

The above would set PWM(2) to the correct range for your spindle if the PWM value was linear to the spindle speed.
The output is an integer value if you use all integer numbers so you won't have finite control over the spindle in that case.
Make sure your Val coming in is a float or one of the other number is at least. &nbsp;
I checked and Pokey can handle a value like 12.3333 in the PWM output.

Code: Select all

global map = function(Val,inMin,inMax,outMin,outMax) 
{
 &nbsp; result = (Val - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
 &nbsp;print ("Map Result " + result);
 &nbsp;return result;
};
Thanks
Glenn
You would have to know what PWM value output the spindle will start at.
Edit:
Another note the Val&nbsp; needs to be a larger number than the inMin so you don't get a negative number.
Will need to test for that before calling the map function.
for instance

Code: Select all

SSpeed=GlobalGet("SpindleSpeed");
if (SSpeed>= 8000){SetPWMDuty_1(2,map(SSpeed,8000,18000,0.1,100));};
Last edited by Anonymous on Wed Jan 27, 2016 5:55 pm, edited 1 time in total.
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

Running a Gcode program while an analog clock script is updating on the screen, and a very short video

https://youtu.be/qaumU9BthKs
Attachments
clock_Gcode.JPG
Last edited by Ya-Nvr-No on Fri Jan 29, 2016 1:20 pm, edited 1 time in total.
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

Quick video of a function call that uses 3 points to determine the circle radius and center.

I added the code to the beginning to clear the screen and also could show all three calls together. Now it will be part of the Library and as long as the check box is on, it is available to use. I am also using the pattern call from the library to display the points and the center of the circle created from the points.

https://youtu.be/p4Jv4hvptvM

added one to show some graphic patterns and spinning using a function call

https://youtu.be/ndLadbfkC1s
Attachments
threepointcircle2.JPG
threepointcircle.JPG
Last edited by Ya-Nvr-No on Wed Feb 10, 2016 7:02 am, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 76 guests