Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: Kawgomoo on September 16, 2014, 10:52:52 AM

Title: Arduino Charge Pump Code/Sketch?
Post by: Kawgomoo on September 16, 2014, 10:52:52 AM
Hello all. I am new to all of this, CNC, electronics, programming. You name it, I'm a newb!

What i am looking for is an example or snippet of code that allows the Arduino to "read" the mach3 charge pump signal.

I just want to arm or disarm 2 outputs based off the presence or lack their of of the mach3 charge pump signal.

I will post the code i was given, however i have not had any luck with it. Trying to troubleshoot the code myself at this time is proving daunting.

Also any external circuitry required to make it work would be appreciated!

I have of course searched this topic, quite a bit. And have gotten nowhere. Any pointing in the right direction would be much appreciated.

Quote
typedef enum
{
  sInitialize,
  sMonitor,
  sDead
}
state_t;

static uint8_t state;

static unsigned long PreviousMicros;

static bool PreviousClock;
static unsigned short Clocks;

const float WAVELENGTH = (1.0 / 10000.0) * 1000000.0;
const unsigned long WINDOW = 2.5 * WAVELENGTH;

void setup( void )
{
  state = sInitialize;
 
  // Make A1 and A0 outputs
  bitSet( DDRC, DDC1 );
  bitSet( DDRC, DDC0 );
 
  // Ensure they start LOW (not necessary but documents our intent)
  bitClear( PORTC, PORTC1 );
  bitClear( PORTC, PORTC0 );
 
  // Make digital pin 8 (PB0) and input (not necessary but documents our intent)
  bitClear( DDRB, DDB0 );
}

void loop( void )
{
  unsigned long CurrentMicros;
 
  CurrentMicros = micros();

  // Count toggles of digital pin 8 (PB0)

  if ( bitRead( DDRB, DDB0 ) )
  {
    if ( ! PreviousClock )
    {
      ++Clocks;
      PreviousClock = true;
    }
  }
  else
  {
    if ( PreviousClock )
    {
      ++Clocks;
      PreviousClock = false;
    }
  }

  switch ( state )
  {
    // Wait for the first toggle
    case sInitialize:
      if ( Clocks > 0 )
      {
        // Go live
        bitSet( PORTC, PORTC1 );
        bitSet( PORTC, PORTC0 );
        // Monitor the clock signal
        state = sMonitor;
        Clocks = 0;
        PreviousMicros = CurrentMicros;
      }
      else
      {
        // Light an LED to indicate waiting?
      }
      break;
     
    case sMonitor:
      // Check each window for...
      if ( CurrentMicros - PreviousMicros >= WINDOW )
      {
        // No clock signal?
        if ( Clocks == 0 )
        {
          // Go dead
          bitClear( PORTC, PORTC1 );
          bitClear( PORTC, PORTC0 );
          state = sDead;
        }
        else
        {
          // Reset for another window
          Clocks = 0;
          PreviousMicros = CurrentMicros;
        }
      }
      break;
     
    case sDead:
      // Light an LED?  Sound an alarm?  Cry in your beer?
      break;
  }
}
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: HimyKabibble on September 16, 2014, 11:31:20 AM
You'd be much better off using attachInterrupt to get an interrupt whenever the change pump input has a rising-edge transition.  On each interrupt, get the current time, using millis(), and calculate the time since the last interrupt, which will tell you the frequency.  Compare that measured frequency to to some reasonable range.  Nominal charge pump frequency is 12.5 kHz., so if you see transitions at a rate from 10-15 kHz, you can assume the charge pump is running.

There is also an Arduino library called FreqMeasure, which will directly measure the frequency of a digital input, and give you a frequency value, in Hz.  The sample sketch for that library will give you almost everything you need, except for testing the measured frequency is in a valid range.

Regards,
Ray L.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: Kawgomoo on September 16, 2014, 02:17:46 PM
Thank you very much for your reply.

I did not author this code, it was given to me on the arduino forum.

I have also never used an arduino before. I purchased the inventors kit and am currently working my way through spark fun's 8hour introduction video {super noob}

My poor results in searching stem from the fact that i really don't know what I'm looking for, or looking at!

I will experiment with the freq measure sketch and do some more research on attachInterrupt, and how i can implement this into code.

I agree the charge pump monitor should be fairly simple, and have a fairly wide range for "on".

The last charge pump solution i built used the hardware approach posted on the mach3 website. It works, but its a little noisy.

my long term goal is to have this on an attiny45 for imbedding easily into various machines.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: Kawgomoo on September 17, 2014, 01:05:11 AM
So far i have made a little progress.

I looked up the FreqMeasure library, and as per their suggestion, tried the FreqCount sketch.

Using analog pin 5 as my input, i am able to watch the frequency monitor output its sample about once a second to the serial monitor window.

With Mach3 running i am seeing 12680 steadily and 0 when i click the reset button {flashing}.

Now i need to figure how to enable or kill my desired outputs using this data.

I thank you for the great suggestion, it seems to have me pointed in the right direction.

Code: [Select]
#include <FreqCount.h>

void setup() {
  Serial.begin(57600);
  FreqCount.begin(1000);
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
  }
}
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: mrprecise44 on September 17, 2014, 09:51:08 PM
If you get a "Charge Pump board" from CNC4PC, and connect the CP input terminal to the Charge Pump output in "Ports and  Pins", the Charge pump relay will open and close, as well as light up an LED showing the Charge Pump output 12.5 khz signal is on or off. The charge pump is a little circuit board about 2" square, with screw terminals for all I/O, and is 5v. The relay has N/C, N/O, and Com terminals.

However, no programming is required, so it may not suit your requirements.

John
 
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: Kawgomoo on September 17, 2014, 10:41:19 PM
The cnc4pc board doesn't really fit the bill. {though on paper it more or less does what i need}

Ahh but how simple life would be if it did.



Title: Re: Arduino Charge Pump Code/Sketch?
Post by: mrprecise44 on September 18, 2014, 12:02:33 AM
I am curious as to why it doesn't "fit the bill" as you say.

For what it is worth, I am also a big fan of the Arduino, and have made lots of things with them, including a Modbus/Mach3 bench setup with an Arduino, and a PP board C10 by CNC4PC,  for Feed rate and Rapid rate and some pushbutton control of basic functions.

John
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: Kawgomoo on September 18, 2014, 04:14:45 AM
because its too big too fit in my package, and teaches me nothing.

simple enough?

good.

arduino DOES NOT control anything in this machine, except the charge pump.

{stop trying to figure out what I'm building, you'll never guess} lol
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: dude1 on September 18, 2014, 06:03:33 AM
being rude wont get you any help
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: ZASto on September 18, 2014, 02:32:40 PM
Search for "Charge Pump Safety" and you will find a schematic drawn by Mariss Freimanis, aka GeckoMan :). It will relieve your headache for measuring the freq with any microcontroller. Pure "1" or "0" :D

Next time, if you keep tyour ChewingGum attitude, do  not expect any relevant answer.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: magicniner on September 19, 2014, 05:05:12 AM
because its too big too fit in my package, and teaches me nothing.

simple enough?

good.

You'd have been better off using a PIC chip, smaller and faster and available with a wide range of onboard I/O and memory options, if you only need the basics you're not wasting space or power, programmed in assembler too, so total control of the hardware with no wussy high level programming, then you'd really have to learn something ;-)
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: HimyKabibble on September 19, 2014, 11:26:08 AM
You'd have been better off using a PIC chip, smaller and faster and available with a wide range of onboard I/O and memory options, if you only need the basics you're not wasting space or power, programmed in assembler too, so total control of the hardware with no wussy high level programming, then you'd really have to learn something ;-)

I hope that's intended to be humorous, because if not you're rather ill-informed about Arduino vs PICs.  Arduinos will do absolutely anything any PIC can do, and for a lot LESS money.  At the low end, a $3 Arduino ProMini board has a 16 MHz Atmel AtMega 328 MCU, with, IIRC, 64KByte FLASH, EEROM, and about 20 I/Os.  It has at least as many peripherals as any PIC.  Find me ANY PIC-based board you can buy for $3, much less one as powerful as the ProMini!  At the high end, a $15 Arduino Due has an 84MHz Atmel chip based on an Arm Cortex 32-bit processor, with 256Mbytes of FLASH, and over 50 I/Os.  They can all be programmed in C, C++, or assembler, and all use the same development environment and programming tools.  There are countless public-domain libraries for doing almost anything you can think of, making Arduino one of the easiest, fastest, and most powerful embedded development platforms in history.  I've been doing embedded development since the mid-'70s, and I now use Arduino exclusively, as there is nothing out there that even comes close in terms of cost, performance, or speed of development.

Regards,
Ray L.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: magicniner on September 20, 2014, 05:32:48 AM
There are countless public-domain libraries for doing almost anything you can think of

Ah yes, that old chestnut, and if you can't download it you can get some sap to code it for you - the "Modder & Maker Generation" at work :-(
There's a difference between getting something going by cobbling together the work of others and optimising your own code, most of the bloated, resource hungry software available is the result of "Libraries You Can Download"

Each to his own, or even "Someone Else's" ;-)
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: HimyKabibble on September 20, 2014, 10:58:42 AM
OK, so you can't back-up your original statement about PICs vs Arduinos....
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: magicniner on September 20, 2014, 07:32:05 PM
And you advocate downloading, not writing your own code ;-)
Well written code on a PIC or any other platform will out perform lazy, cobbled together, downloaded libraries on any other platform.
But as an experienced embedded programmer you must know this and not be bothered by it,
Hey, Ho,
That's the industry as it stands,
Slackers balancing on the shoulders of those they percieve as giants ;-)
Enjoy!
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: HimyKabibble on September 20, 2014, 09:13:54 PM
And you advocate downloading, not writing your own code ;-)
Well written code on a PIC or any other platform will out perform lazy, cobbled together, downloaded libraries on any other platform.
But as an experienced embedded programmer you must know this and not be bothered by it,
Hey, Ho,
That's the industry as it stands,
Slackers balancing on the shoulders of those they percieve as giants ;-)
Enjoy!

Don't put your words in my mouth.  

You truly believe there is NO high-quality off-the-shelf code out there?  Must be very dark in your world.  If you knew what you were talking about, you would know there is a great deal of very good code available and that can save even an experienced programmer a great deal of time.  And, for people, like the OP, who are NOT experienced programmers, and for many of us who are, efficiency often really does not matter, as it buys nothing in terms of functionality.  The OP proved that by being able to take the FreqMeasure library and put it to use in very short order to do what he needed to do.  How long do you think it would have taken him to duplicate that functionality on his own?  That's the difference between someone who's more interested in accomplishing the task at hand than showing off.

FYI - Many, if not most, of the public libraries for Arduino WERE developed by very competent, professional programmers, and requires no optimization.  And even if it does, it can give a good starting point for those of us who have better things to do with our time than re-inventing the wheel just to prove how studly we are.  The fact that you believe otherwise is a clear indication of your level of knowledge about Arduino.  

Given that you seem to only want to play the troll and argue, rather than have a useful exchange of information, leaves me no longer giving a rats a$$ what you think.  So rant away - I'm not going to waste my time listening to your uninformed drivel any more.

Regards,
Ray L.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: dude1 on September 20, 2014, 09:24:15 PM
children put your toys back in the cot
get back on task what the poster wants to do i.e. a switch
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: ZASto on September 21, 2014, 06:33:06 AM
Hey, guys, please do not try to make this topic a pissing contest.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: Kawgomoo on September 22, 2014, 05:33:08 PM
In the 48 hours that mach support felt needed to "teach me a lesson" i already authored my own code and got it working fine.

Marris Freimans charge pump design is bulky and noisy. Not to take anything away from him, but he even points this out in his instructions.

I ask a question about coding arduino, and get the reply buy different hardware.

Yet i am the one who is rude for not accepting such off base off topic answers?

I have lost all interest in using Mach3. There are other solutions out there without a fanboy attitude when it comes to questions being asked. Every question i have asked regarding Mach3, on any forum, i have gotten a reply with someone trying to sell me something. I don't want anyones junk, i am in this to learn this, for myself.

The point of asking a question is for others with experience to weigh in. If the correct way to do everything was to learn every aspect of everything that an assembly needs, no one would ever achieve anything.

Its called working together, take what you know, add it to what i know, and in the end have something new to enjoy.

But since i asked a question on writing code, apparently that makes me some idiot hack.

Im sure you can step into my shoes and do my job just as well as I. What a poor poor stupid hack i am.

fyi i didnt download any code. i was given code that was authored for me, and it didnt work. so then i took it upon myself to learn the coding so i could do it entirely myself, and not have to ask questions where i get such rude replies…an on top of that a 48 hour ban for putting up with your crap.

And mach thinks this is how you run a successful business? LOL

You can count me out.
Title: Re: Arduino Charge Pump Code/Sketch?
Post by: dude1 on September 22, 2014, 06:09:26 PM
the people who had an disagreement where also told off.
this is a forum is for people to work together arguments included if someone does not like what someone says or how they say it if they say something about it that's good to, as 10 ideas may not work and get argued about but the 11 may work.
you were told of because of the way you stated your reply if you don't wont someone to now what you are doing say I wont to keep that private. (simple) that's rude.

as an example some people don't like being called disabled I don't give a rats if I am called disabled I am to some that's being rude.

also I could if jumped in a stated that TI boards are better than arduino or picks they are all the correct things to use if that's what you wont to use