MPG with Mach3 using mach3 controls

Moderators: TomKerekes, dynomotion

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: MPG with Mach3 using mach3 controls

Post by TomKerekes » Thu Sep 24, 2020 3:26 pm

Hi Jerry,

The reason the MPG movement code is placed in KFLOP is so that it can always respond instantly without requiring the PC/Windows/Mach3 to do anything. So the ServiceMPG function (subroutine) should be added to your Init program and a call to it should be placed inside the existing forever loop. You might read about functions here.

Your Init program already has a forever loop to monitor status requests from the PC and if they stop happening for a while to shut off main power. So now your forever loop will be doing that and also handling MPG Motion.

Note that if the function is added at the end (like I prefer) a "prototype" should be added at the beginning to tell the compiler what kind of function it is so it knows what to expect when it sees the call to it. So near the beginning, before main(), add:

void ServiceMPG(void);

btw does the axis 5 Position on the Axis Screen change when you rotate the MPG?

btw your code isn't indented properly to make it easier to read. See here.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

macona
Posts: 39
Joined: Tue Sep 15, 2020 4:44 am

Re: MPG with Mach3 using mach3 controls

Post by macona » Thu Sep 24, 2020 9:11 pm

OK, I added ServiceMPG like so to my init file:

Code: Select all

#include "KMotionDef.h"

// Defines axis 0, 1, 2 as simple step dir outputs
// enables them
// sets them as an xyz coordinate system for GCode

#define DISABLE_DELAY 5.0 // seconds after host stops to disable the thing

void ServiceMPG(void);

main() 
{	InitAux();
	AddKonnect(0,&VirtualBits,VirtualBitsEx);
I put the MPG routine in the main loop of the notifymach3 file.

The DRO is changing for axis 5 in the Kmotion software, 4 encoder counts per click.

Still not working.

-Jerry

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: MPG with Mach3 using mach3 controls

Post by TomKerekes » Thu Sep 24, 2020 11:10 pm

Hi Jerry,
OK, I added ServiceMPG like so to my init file:
I only see a portion of your Init Program with the function defined. I don't see where you added the function or a a call to it in the forever loop.

In general a 'for' loop in C allows you to define 3 things. What to do at the start of the loop, when to exit the loop, and what to do at the end of the loop before repeating the loop. For example a loop that starts at i=0, loops while i is less than 10, and increments i each loop would be coded:

Code: Select all

for (i=0; i<10; i++)
{
}
A forever 'for' loop that does nothing at the start, never exits, and doesn't do anything each loop would be coded:

Code: Select all

for (;;)
{
}

I put the MPG routine in the main loop of the notifymach3 file.
The MPG routine should not be added to the NotifyMach3 file. And btw the NotifyMach3 file doesn't have any loop, it just executes once when called by Mach3.

The DRO is changing for axis 5 in the Kmotion software, 4 encoder counts per click.
In this case you were correct you would need to add a divide by 4 to Factor to move one increment per click
Regards,

Tom Kerekes
Dynomotion, Inc.

macona
Posts: 39
Joined: Tue Sep 15, 2020 4:44 am

Re: MPG with Mach3 using mach3 controls

Post by macona » Thu Sep 24, 2020 11:58 pm

Ahh, for some reason I thought the MPG stuff had to go into the notifymach file. I deleted that and put the mpg routine into the init file.

But I am doing something wrong. When I launch mach I get a compiler error, 432: cannot use local functions.

-Jerry
Attachments
InitStepDir3AxisSupermax2.c
(12.03 KiB) Downloaded 88 times

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: MPG with Mach3 using mach3 controls

Post by TomKerekes » Fri Sep 25, 2020 3:22 pm

Hi Jerry,

The function definition is placed inside forever loop instead of outside the main function but with a call to the function inside the forever loop.

Do you understand the difference between a subroutine definition and a call to a subroutine?

Again the idea of indenting code properly is to show what is inside of what. Your indenting is all messed up making the code hard to read. There is an Auto Indent option to do this for you. See my previous post.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

macona
Posts: 39
Joined: Tue Sep 15, 2020 4:44 am

Re: MPG with Mach3 using mach3 controls

Post by macona » Fri Sep 25, 2020 9:26 pm

Ah, I didnt know there were new versions of the software off the main dynomotion page.

Im a hardware guy, I only use a little bit of C++ every few years when I need to do something simple with a microcontroller. All the stuff I do at work revolves around LabView.
The function definition is placed inside forever loop instead of outside the main function but with a call to the function inside the forever loop.
I just dont understand what that means and how to implement it. Im pretty much throwing stuff at the screen and seeing what sticks at this point.
InitStepDir3AxisSupermax2.c
(12.79 KiB) Downloaded 91 times

macona
Posts: 39
Joined: Tue Sep 15, 2020 4:44 am

Re: MPG with Mach3 using mach3 controls

Post by macona » Sat Sep 26, 2020 12:04 am

OK, a friend game me some hints and I fixed a bunch of stuff and now it compiles.

But still does not responds to MPG
Attachments
InitStepDir3AxisSupermax2.c
(12.79 KiB) Downloaded 82 times

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: MPG with Mach3 using mach3 controls

Post by TomKerekes » Sat Sep 26, 2020 1:06 am

Hi Jerry,

That looks correct to me. Maybe the Mach3 Macropump isn't working.

I added some diagnostic code to print to the console some values every 5 seconds so we can figure out what isn't working. Please try it and report what is printed on the console as you change axis and increments.
Attachments
InitStepDir3AxisSupermax2 tk.c
(12.98 KiB) Downloaded 85 times
Regards,

Tom Kerekes
Dynomotion, Inc.

macona
Posts: 39
Joined: Tue Sep 15, 2020 4:44 am

Re: MPG with Mach3 using mach3 controls

Post by macona » Sat Sep 26, 2020 12:07 pm

OK, that helped a lot. Turns out Mach did not like the Else If statements, it was stopping the macro pump from executing. I replaced it with just a bunch of if statements and then found the correct OEMLeds for what I wanted, the chart I used was wrong.

Completed, working macropump.m1s:

Code: Select all

'i=i+1
'message "Macro Running #"&i

Increment = GetOEMDRO(828) 'Get Increment
SetOEMDRO(1007, Increment) ' put it in User DRO 7
NotifyPlugins(19007) 'Send it to KFLOP

If (GetOEMLED(59) = True) Then Axis = 0 'X Axis
If (GetOEMLED(60) = True) Then Axis = 1 'Y Axis
If (GetOEMLED(61) = True) Then Axis = 2 'Z Axis
If (GetOEMLED(62) = True) Then Axis = 4 'A Axis
If (GetOEMLED(63) = True) Then Axis = 5 'B Axis
If (GetOEMLED(64) = True) Then Axis = 3 'C Axis (spindle)'
If (GetOEMLED(57) = False) Then Axis = 6 'Jogging mode selcted'

SetOEMDRO(1008, Axis) ' put it in User DRO 8
NotifyPlugins(19008) 'Send it to KFLOP 
So, not that I was getting data to the init file and the values were changing in the console the machine was not moving. I used your add-on to look at other variables and found it was reading the encoder but change1 was stuck at 0. I commented out the

Code: Select all

if (Axis = 6 || JOB_ACTIVE)	// if not button pressed or Job Active ignore the encoder.
		Change1 = 0;
and it works. Well, it moves at least, I can select the axis and it moves in the increments I select.

I have two problems now, a couple of the axis move in the wrong direction in reference to the wheel. And ideas how to fix that?

Second problem is much more serious, if I quit and relaunch mach the machine will take off in whatever axis is currently selected to some point.I had to be quick with the estop. I am guessing that it is trying to meet up with a stored increment or something. So I think I need something that will reset all these variables first thing?

And then I need to figure out why the disable code is not working.

I should probably sleep now. lol

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: MPG with Mach3 using mach3 controls

Post by TomKerekes » Sat Sep 26, 2020 4:13 pm

Hi Jerry,

Code: Select all

if (Axis = 6 || JOB_ACTIVE)	// if not button pressed or Job Active ignore the encoder.
		Change1 = 0;
has an '=' instead of an '=='. So the result is Axis is always set to 6 and since 6 is 'considered TRUE then Change1 is always 0.

As I said this is one of the most common C programming bugs. If you compile with the TI Compiler it has better warnings and will warn you about this with:

"C:\Temp\InitStepDir3AxisSupermax2 tk.c", line 464: warning: use of "=" where "==" may have been intended

I see also:

"C:\Temp\InitStepDir3AxisSupermax2 tk.c", line 402: warning: function declared implicitly
"C:\Temp\InitStepDir3AxisSupermax2 tk.c", line 435: error: declaration is incompatible with previous "ServiceMPG" (declared at line 402)

because the function prototype was removed so the compiler doesn't know about the function when it makes the call to the function.

a couple of the axis move in the wrong direction in reference to the wheel. And ideas how to fix that?
Not sure why that would be if Mach3 moves the axes in the correct directions. If so then the configuration should be correct. But defining the Steps per unit for the axis as a negative value should reverse the motion.

if I quit and relaunch mach the machine will take off in whatever axis is currently selected to some point.
Not sure why. You will need to debug what is happening. One issue might be when Jogging is disabled Axis is 6, AxisSteps is not set and might contain garbage (like infinity), but then moving axis 6 which doesn't exist to infinity shouldn't cause a run away. Anyway better to set AxisSteps to 0 in that case.
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply