Shared Functions()

Post a reply

Confirmation code
Enter the code exactly as it appears. All letters are case insensitive.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: Shared Functions()

Re: Shared Functions()

by Amazon [Bot] » Wed Feb 10, 2016 4:16 pm

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

Re: Shared Functions()

by Amazon [Bot] » Sat Jan 30, 2016 12:12 am

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

https://youtu.be/qaumU9BthKs

Re: Shared Functions()

by GlennD » Thu Jan 28, 2016 12:15 am

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.  
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) 
{
   result = (Val - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
  print ("Map Result " + result);
  return result;
};
Thanks
Glenn
You would have to know what PWM value output the spindle will start at.
Edit:
Another note the Val  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));};

Re: Shared Functions()

by Amazon [Bot] » Wed Jan 27, 2016 10:40 pm

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,  :-[ (I'm old)

Re: Shared Functions()

by Amazon [Bot] » Mon Jan 11, 2016 4:44 pm

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,  m = m+12;  cy = cy-1;  end;
 c = fix(cy/100);  
 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");
        m = m-2;  
        if (m <= 0)
          {
            m = m+12;
            cy = cy-1;
          }
         c = math.floor(cy/100);  
         y = (cy % 100);
         w = ((d+math.floor(m*2.59)+math.floor(y*1.25)+math.floor(c*5.25)) % 7);
         w = ToInt(w);
         return day[w];
}; 
weekday=getday(1,11,2016); // todays date returned as #1 Monday
print("The day of the week was: "+weekday);

Re: Shared Functions()

by Amazon [Bot] » Mon Dec 28, 2015 2:43 pm

Here is an example CheckHomed(); of how to change the axis DRO's font color based on the homed condition.
I used a button to call the function to test.

Code: Select all

global x=DRO("Main_DRO_0");
global y=DRO("Main_DRO_1");
global z=DRO("Main_DRO_2");
global a=DRO("Main_DRO_3");
global axis=b; 
  x.DrawColor(0x00ff00, 0x000000);
  y.DrawColor(0x00ff00, 0x000000);
  z.DrawColor(0x00ff00, 0x000000);
  a.DrawColor(0x00ff00, 0x000000);

global Homed = function(axis,state) 
{ 
if(state==1){
  if (axis == 0){x.DrawColor(0xff0000, 0x000000);}
  if (axis == 1){y.DrawColor(0xff0000, 0x000000);}
  if (axis == 2){z.DrawColor(0xff0000, 0x000000);}
  if (axis == 3){a.DrawColor(0xff0000, 0x000000);}
  }
else{
  if (axis == 0){x.DrawColor(0x0000ff, 0x000000);}
  if (axis == 1){y.DrawColor(0x0000ff, 0x000000);}
  if (axis == 2){z.DrawColor(0x0000ff, 0x000000);}
  if (axis == 3){a.DrawColor(0x0000ff, 0x000000);}
  }
};

global CheckHomed = function() 
{ 
  state=GlobalGet("Axis1Homed");
  Homed(0,state);
yield();
  state=GlobalGet("Axis2Homed");
  Homed(1,state);
yield();
  state=GlobalGet("Axis3Homed");
  Homed(2,state);
yield();
  state=GlobalGet("Axis4Homed");
  Homed(3,state);
yield();
};

CheckHomed(); // i use this here to test in the mainscreen script to call the function

Re: Shared Functions()

by Amazon [Bot] » Sat Dec 26, 2015 11:15 pm

You might like Dialog popup boxes too, in this example called with a screen button named "Main_BUT_45"

Code: Select all

global  TestDlgs = function(current) 
{
  print(current);
  dlg=Button("Main_BUT_45");
  if(current == 1)
    {
      a = Dlgs();
      b = a.GetCoord3("Enter 3 coords:", "x", "y", "z", Vec3(4,5,6));
      val0=(b[0]);
      val1=(b[1]);
      val2=(b[2]);
      print(b);
      print(val0);
      print(val1);
      print(val2);
    }
return;
};

Re: Shared Functions()

by GlennD » Sat Dec 26, 2015 6:25 pm

Thanks
You forgot the you numbskull part.
I tried everything except the parentheses.  :D

Re: Shared Functions()

by ArtF » Sat Dec 26, 2015 5:30 pm

Hi Glen:

  There are a couple ways to do that really. The way I recommend is to use

mybut = Button("Main_BUT_88");
mybut.SetText("New");

  to get the text you can do a

text = mybut.GetText();

  FYI:
    When your typing these things, type the words  "mybut = Button"", then left shift and click the button,
the rest will fill in for you.. If a button text doesnt set.. check its ID is unique..

Art



Re: Shared Functions()

by GlennD » Sat Dec 26, 2015 4:56 pm

Art
Thank you for the explanation.
A quick question on the SetText and GetText if you have time.

I have a button I want to change its text based on the state.
I started my button id as just 0 or 1 initially then thought I would add a 700 in front maybe to differentiate them.

Here is the different examples to see if I could get the text from a button.
Figured if I went that route I could figure out how to set it from there.
Since I tried a bunch of different iterations last night and today on the SetText with no luck.

Code: Select all

mTxt = Button(Main_BUT_7002).GetText;
mTxt1 = Button("Main_BUT_7002").GetText;
mTxt2 = Button(Main_BUT_7002).Text;
mTxt3 = Button(Main_BUT_7002).text;
mTxt4 = Button("Main_BUT_7002").Text;
mTxt5 = Button("Main_BUT_7002").text;

but= Button("Main_BUT_7002");
 mTxt6 = but.GetText;
"but" does show the hex val of the Main_BUT_7002

As you can see the .GetText gives a function:_native
The .text and .Text stay as null.

I even tried getting the text of a label same results.
A perfectly good answer is it is not hooked up yet and I can wait.
Or numb skull do it this way or that ain't happening.  :)

Thanks
Glenn

Top