Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: bcoop on May 09, 2020, 08:39:51 PM

Title: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 09, 2020, 08:39:51 PM
Can anyone tell me how to read and write the Analog input & output values, If I make an assignment as seen in the screen shot, how do I address Analog Input #1 in a script.
there must be an addressable means such as ISIG_INPUT1 for the analogs?

Bob
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: thosj on May 10, 2020, 08:02:19 AM
Look down thread for a topic about Manual Spindle Start Switch. On the second page I outline how I've used two of those. Might give you a start on whatever you're looking to do.

Tom
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 10, 2020, 10:42:21 AM
Thanks Tom. that thread looks like a way to read the pokeys57 registers directly,  what i am attempting to do is utilize the Mach Analog inputs and outputs, assigning pokey and other devices to the Mach4 analogs and read/write within scripting. I would think there would be some tags associated with them
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: jbuehn on May 10, 2020, 11:52:54 AM
There are specific API functions for analog that you can find in ZeroBrane. I think they just use the actual analog input/output #?
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: joeaverage on May 10, 2020, 09:21:37 PM
Hi,
the analogue values are stored in registers. The individual registers are 'owned' by the PoKeys board.

If you go to the Register Diagnostics page and expand the PoKeys register tree you will see the registers you require.

To read the value first you must get the handle of the register using mc.mcRegGetHandle() and then read the value with
mc.mcRegGetValue or mc.mcRegGetValueString().

Craig
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 10, 2020, 10:17:00 PM
Craig, thanks for your response,  my question is not really related to Pokey registers, it is how to read Mach's "Analog Input #1" register.  I am just not finding anything in the API reference or any other documents. Also do not see any reference to the Mach Analog registers in the register plugin under diagnostics.

If I wanted to Read "Pokeys Analog Input 41" .........then i would use  PoKeys_50590/Analog input 41  (register)
if I wanted to Write "Output #1"  ..........................then I would use mc.OSIG_OUTPUT1   
if I wanted to Read "Input #1"  ............................then i would use  mc.ISIG_INPUT1 
if I wanted to Read "Analog Input #1"  ..................then what would i use ?
if I wanted to Write "Analog Output #1"  ...............then what would i use ?

Bob
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: joeaverage on May 10, 2020, 11:27:35 PM
Hi,

Quote
if I wanted to Read "Analog Input #1"  ..................then what would i use ?
if I wanted to Write "Analog Output #1"  ...............then what would i use ?

To my knowledge there is no such thing as 'Analog Input #1' or 'Analog Output #1'. You could certainly define registers like that
under iRegs0 for instance.

Your examples Output#1 and Input#1 are Boolean signals whereas an analogue value is a number and requires a register not a signal.

Craig
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: jbuehn on May 11, 2020, 03:39:25 AM
value, rc = mc.mcAnalogInputRead(inst: number, analogInputNum: number)
value, rc = mc.mcAnalogOutputRead(inst: number, analogOutputNum: number)
rc = mc.mcAnalogOutputWrite(inst: number, analogOutputNum: number, value: number)

They don't look like they're in the API docs yet, but if you're in ZeroBrane they should come up as suggestions.

I've never used them, though I think analogInputNum and analogOutputNum is just the number of the input or output? So to read "Analog Input 1" it'd be mc.mcAnalogInputRead(inst, 1), given you have that assigned to something in Mach4's Config.
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: joeaverage on May 11, 2020, 04:16:26 AM
Hi,
I can't find that API in API.chm, neither does it come up in ZeroBrane.

Craig
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 11, 2020, 08:09:30 AM
Thank you all for your input on this, 

I have tried    local value1 = mc.mcAnalogInputRead(inst, 1 ) which always displays 0.0

directly reading the register assigned to Mach Analog Input #1,   displays the correct value,
    hReg= mc.mcRegGetHandle(inst, "PoKeys_50590/Analog input 41")
    local value2 = mc.mcRegGetValue(hReg)

I have tried numerous variations of reading the Reg Handle of Analog Input #1 without any luck
Possibly these have not been fully implemented yet, I will submit a ticket and see if i can find out anything more, will report back to this thread

Thanks again
Bob
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: SwiftyJ on May 11, 2020, 08:30:36 AM
Try putting a 1 in the denominator and numerator column for each of the inputs
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 11, 2020, 09:15:19 AM
Ha,  that worked!    I guess I never noticed the scaling method implemented,   thank you very much!

so here is what worked to read an Analog Input
1. make an assignment to the Analog Input# under Mach's 'Control Configuration / Analog Inputs'
2. set some scaling values in the numerator and denominator - for no scaling factor set to 1 & 1 
3. in a script use      local value = mc.mcAnalogInputRead(inst, # )  where # is 0-63

Thanks to everyone

Bob
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: jbuehn on May 11, 2020, 12:14:15 PM
Craig, I was looking in build 4481 at the analog API functions.
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: smurph on May 12, 2020, 12:00:46 AM
As stated above, you can access the analogs as registers as they come in from the plugin using the register read/write functions.  This will let you access a raw ADC or DAC value, usually something along the lines of 0 to 1023 (10 bit ADC/DAC) or 0 to 4095 (12 bit ADC/DAC), etc...  Mapping these registers to an analog input give you the capability of working with voltages instead of raw ADC/DAC values, if required. 

For example, say you have a 12 bit DAC output with a voltage range of 0 to 10v represented by the scale of 0 to 4095, 0 being 0 volts and 4095 being 10 volts:

Set the numerator to the total voltage swing (0 to 10 = 10 volts total) and the denominator equal to the total capacity of the DAC bits.  12 bits == 4096.  Now you can use the mcAnalogOutputWrite() function with volts!

rc = mc.mcAnalogOutputWrite(inst, 1, 7.5) -- Set analog output 1 to 7.5 volts. 
rc = mc.mcAnalogOutputWrite(inst, 1, 2) -- Set analog output 1 to 2 volts. 

What if you have a bi-polar -10 to +10 volt 12 bit DAC that you want to set?

Set the numerator to the total voltage swing (-10 to +10 = 20 volts total) and the denominator equal to the total capacity of the DAC bits.  12 bits == 4096.  Most bi-polar DACs have mid scale as 0 volts, so set the offset to half of the denominator, 2048.

rc = mc.mcAnalogOutputWrite(inst, 1, -10) -- Set analog output 1 to -10 volts.  Raw DAC value will be 0.
rc = mc.mcAnalogOutputWrite(inst, 1, 0) -- Set analog output 1 to 0 volts.  Raw DAC value will be 2048.
rc = mc.mcAnalogOutputWrite(inst, 1, 10) -- Set analog output 1 to +10 volts.  Raw DAC value will be 4096.

Steve
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 12, 2020, 05:56:30 AM
Thank you Steve for the examples on using the numerator, denominator, & offset   - very helpful.

Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 14, 2020, 06:30:06 PM
one more analog question, how do I assign an AnalogInput  or Analogoutput to a to a dro or textbox. to display the value?
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: smurph on May 15, 2020, 04:13:31 AM
I haven't got that implemented yet.  :( 

Steve
Title: Re: How to read/write Analog Inputs and Analog Outputs
Post by: bcoop on May 15, 2020, 08:49:42 AM
Ok Steve, good to know,   I  just made the assignment via the PLC script.

all the best
Bob