Big Stepper Motor - GH Firefly

Hello Everyone,

I am using Arduino Uno to control a big stepper motor for material extrusion. When using the following code directly from the serial monitor of the Arduino everything works great:

int PUL = 8; //define Pulse pin
int DIR = 6; //define Direction pin
int ENA = 5; //define Enable Pin
int spd = A0;       // Potentiometer
int extrude_speed = 50;
int cmnd;
int data;
char rx_byte = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("IHSS57-XX INTEGRATED STEPPER SERVO MOTOR BEGIN");
  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);
}

void forward(int extrude_speed) {
  extrude_speed = map((analogRead(spd)),0,1023,200,1);

  for (int i = 0; i < 6400; i++) 
  {
    digitalWrite(DIR, LOW);
    digitalWrite(ENA, HIGH);
    digitalWrite(PUL, HIGH);
    delayMicroseconds(extrude_speed);
    digitalWrite(PUL, LOW);
    delayMicroseconds(extrude_speed);
  }
}

(The rest of the code loops through forwarding/backward/idle functions by pressing “1”, “2” or “0” so I thought it is not relevant for my question.)

However, when I try to control the motor from grasshopper via firefly firmata it is simply not working, and I don’t understand why.

This is the code in GH:

Any advice or help would be much appreciated!

Thanks,
Ofer

do you use a stepper driver?

Yes, that is the reason I tried the timer and the python code in GH.

There’s not enough info to diagnose your problem.

With the presumed direct connection of your stepper driver’s step (pulse) and direction lines to the step and direction outputs of your Arduino it works - great - you know your integrated stepper+driver is alive!

But we can’t see what your Python code is sending to the Uno Write component’s “pin 8” input. All we are seeing from “Out” is a single “packet” of the state of the nine “DPpin” Uno Write component inputs.

Guessing your Python script + timer input is not sending out the stream of pin 8 toggles you think it is. You may want to simply try repeatedly turning on and off a single LED connected to pin 8 using Firefly. If that’s working, but your stepper isn’t stepping, then double check that the enable pin is high.

Be glad to check it if you upload your GH definition.

Also - in looking at the Uno Write component’s help, it says:

Note: This component is meant to be used in tandem with the Firefly Uno Arduino Sketch which can be downloaded from the Firefly website at: http://www.fireflyexperiments.com.

And:

Arduino Output (Text)

Returns the string that will be sent to the Arduino board. The Arduino sketch that accompanies this component will split this string up and send the values to the appropriate pins.

Are you using this Arduino sketch to decipher the serial packets sent out by the Uno Write component?

Thank you a lot, Bruce!

Attached is the GH definition, I will take on your wise advice and check that it is working using a LED.
I am not completely sure I fully understand your second question, please excuse my gaps of knowledge, I am learning as I go.
Thank you for your time :slight_smile:

3D_Potter.gh (32.5 KB)

I remember to have had problems with the stepperfile to load on the arduino using the uno.
If I remember correctly, I had to remove the version extendsions.
this one:

#if defined(__SAM3X8E__) 
  void WriteToDAC(int _pin, int _value){
    if(_pin == 0) analogWrite(DAC0, _value);
    else if (_pin == 1) analogWrite(DAC1, _value);
  }
#endif

from the firmata firmware. not sure this solves your problem though…

here you got some examples using libraries or no libraries alternatively:

…ok, I cna’t attcach .ino

noLib:

int delaylength = 500;
int delayPauseLength = 5000;
int steps = 100;
int n;
int m;
void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
}

void loop(){
  switch(n){
    case 0:
      digitalWrite(9, LOW);  //ENABLE CH A
      digitalWrite(8, HIGH); //DISABLE CH B
      digitalWrite(12, HIGH);   //Sets direction of CH A
      analogWrite(3, 255);   //Moves CH A
      n++;
      m++;
      if(m>steps){
        m=0;
        delay(delayPauseLength);
      }
      delay(delaylength);
    case 1:
      digitalWrite(9, HIGH);  //DISABLE CH A
      digitalWrite(8, LOW); //ENABLE CH B
      digitalWrite(13, LOW);   //Sets direction of CH B
      analogWrite(11, 255);   //Moves CH B
      n++;
            m++;
      if(m>steps){
        m=0;
        delay(delayPauseLength);
      }
      delay(delaylength);
    case 2:  
      digitalWrite(9, LOW);  //ENABLE CH A
      digitalWrite(8, HIGH); //DISABLE CH B
      digitalWrite(12, LOW);   //Sets direction of CH A
      analogWrite(3, 255);   //Moves CH A
      n++; 
      m++;
      if(m>steps){
        m=0;
        delay(delayPauseLength);
      } 
      delay(delaylength);
    case 3:    
      digitalWrite(9, HIGH);  //DISABLE CH A
      digitalWrite(8, LOW); //ENABLE CH B
      digitalWrite(13, HIGH);   //Sets direction of CH B
      analogWrite(11, 255);   //Moves CH B
      n=0;
      m++;
      if(m>steps){
        m=0;
        delay(delayPauseLength);
      }
      delay(delaylength);
   }
}

here using stepper.h as library:

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution

// for your motor

// initialize the stepper library on the motor shield

Stepper myStepper(stepsPerRevolution, 12,13);

// give the motor control pins names:

const int pwmA = 3;

const int pwmB = 11;

const int brakeA = 9;

const int brakeB = 8;

const int dirA = 12;

const int dirB = 13;

int x = 0;

void setup () {

Serial.begin(9600);

// set the PWM and brake pins so that the direction pins // can be used to control the motor:

pinMode(pwmA, OUTPUT);

pinMode(pwmB, OUTPUT);

pinMode(brakeA, OUTPUT);

pinMode(brakeB, OUTPUT);

digitalWrite(pwmA, HIGH);

digitalWrite(pwmB, HIGH);

digitalWrite(brakeA, LOW);

digitalWrite(brakeB, LOW);

// initialize the serial port:

Serial.begin(9600);

// set the motor speed (for multiple steps only):

myStepper.setSpeed(200);

} void loop () {

myStepper.step(200);

myStepper.step(-200);

delay(2000);

}

further it’s important to see that there is a quad stepper motor stream and a write component, as well as two different firmatas respectively. I guess that’s what Bruce is asking in his second question.

also there are several types of stepper drivers or modes respectively. you can have direction and steps (e.g. sparkfun) or some h-bridge where you’d control each coil “yourself”. If you want it to simply work, I’d strongly recommend the sparkfun stepper driver from the image coming with firefly. It worked pretty good for me.
you seem to be trying to combine them together, which demands for another kind of firmata than the provided ones, like Bruce pointed out. the text string providing the arduino with information from firefly needs to be deciphered to be converted into something that makes sense for arduino and it’s firmata logic.

OK - looks like your Python+timer works:


(though when your sketch loaded, the Python script reported an error (“counter” undefined), but toggling the button attached to “x” a couple times got it going). As expected, the sixth item in the text string is toggling. This would turn pin 8 on and off IF you had the appropriate Arduino sketch ready to receive the text stream sent by serial connection using the Firefly component.

I’m sure with some digging on the Firefly site and its forum, you can find this missing piece. However, after thinking about it a bit, I’m not sure I’d recommend that strategy. I don’t know exactly what you want to do, but given the clues - “3D_potter.gh” and “IHSS57 Integrated Stepper…” and “extruder speed” I suspect you want to do 3D printing (or some other multi-axis project). In my brief experimentation with Firefly (about a year or two ago), I did not use the Uno Write component - just the Serial Write. My stepper control platform uses a non-Arduino microcontroller to handle the timing of the step / dir stream fed to the stepper drivers. I tried Firefly to see if I could sync a Kangaroo simulation I was seeing on my monitor to a real 2-axis kinetic sculpture. The answer was yes, but with some severe limitations. In my case, though it was cool and I felt some sense of achievement, there really was no practical value for me. But strategically - I never tried to use GH/Firefly to do the low level job of flipping step/dir bits because from prior experience I know that “timers” in GH cannot be relied on for accuracy (perhaps I’m wrong, but they appear very dependent on CPU speed and whatever else is happening on your GH computer). However - you already have a micro that has reasonable micro-second timing - your Arduino. I suggest if you really need/want to do the high level stuff using GH, then just rely on Firefly to relay high level info over the serial port to your Arduino, and write an Arduino sketch that parses incoming hi-level text commands (e.g. “D6400, S50”) and converts each to:

steps = 6400; //Distance
extrude_speed = 50; //Speed

You do have to spend the effort to write the Arduino code to do this, but you already know your Arduino code can move your stepper reliably. Note, you don’t need a high level command for direction - just flip the direction pin if the distance is negative.

Thank you very much for your time and thoughts!
I will try to implement the code in the Serial Write component, will let you know how it works out.
Maybe it is a bit pointless in terms of accuracy, to try and control the extrusion rate, but I do want to develop this degree of freedom for the manufacturing process and my experimentations :slight_smile: and see how it works out.

Maybe it is a bit pointless in terms of accuracy, to try and control the extrusion rate, …

You should be able to control the speed of your stepper with very high accuracy, precision, and repeatability. If your extruder is mechanically sound, I’m guessing you will be pleasantly surprised.

great to have people like you on this forum!
I am sure you’ll get there! let us know if we can do something for you