Welcome! Log In Create A New Profile

Advanced

Arduino with Gen 6 board

Posted by Javaid Butt 
Re: Arduino with Gen 6 board
August 22, 2011 03:32AM
All right, now this is the code in Arduino. I connected the LED to pin 2 in order to avoid confusion with reprap.
#include "Wire.h"

//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
int ledPin = 2;                 // LED connected to digital pin 2

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  Wire.begin(CP_ADDR);
  Wire.onReceive(receiveEvent); // register event so that anything received on i2c is sent to the serial
//Test gcode, this should send the machine's X, Y and Z to home
  sendGCode("G28 X0 Y0 Z0\n");
}

void receiveEvent(int howMany)
{
  while(0 < Wire.available()) // loop through all
  {
    char c = Wire.receive(); // receive byte as a character
    if (c = '1') 
    {
      //turn dispenser on
      digitalWrite(2, HIGH); //replace the number 2 with the dispenser pin number
    }
    if (c = '0') 
    {
      //turn dispenser off
      digitalWrite(2, LOW);
    }
  }
}

void sendGCode(char* GCode)
{
  Wire.beginTransmission(REP_RAP_ADDR);
  Wire.send(GCode);
  Wire.endTransmission();   
}

void loop()
{
  
}

I also made changes to the main code in reprap but the thing is that the LED is continuously OFF confused smiley

Edited 3 time(s). Last edit at 08/22/2011 04:45AM by Javaid Butt.
Re: Arduino with Gen 6 board
August 22, 2011 07:39AM
Wouldn't it be simpler if we can send a signal from reprap to arduino whenever reprap starts printing.

In the Gen 6 circuit diagram, the EXTR EN pin on the atmega is 43 and then the motor driver enable pin is 26 (IC8 just on the bottom right of atmega).
Correct me if I am wrong about the pins. Can we make use of the signal coming here. I checked with multimeter whenever "E" has a value, there is some voltage on the pins of the Gen 6 where extruder is connected. What do you think ?

GEN6_Mendel_Circuit.pdf
Re: Arduino with Gen 6 board
August 22, 2011 11:18AM
As far as I know, the Enable pin is often on even when the extruder isn't moving and when the the printer isn't printing. This is not to mention that it doesn't have a proper pin to connect a wire, it would be difficult soldering a wire on to it.

It would make more sense to find the code that sets this pin high and add code to it to tell the arduino than read it with an arduino input.

What would be an even better idea is find the code that steps the motor and use that to tell the arduino to dispense.

I am not the person to ask about where the code to make a movement starts and stops and therefore I don't know where to put the code to issue the start dispensing and stop dispensing commands, try asking in a forum board for firmware, try here: http://forums.reprap.org/list.php?146
Re: Arduino with Gen 6 board
August 22, 2011 05:16PM
I will work on that part but in the meantime, let's work out the second method. May be it will prove to be viable.
Quote

Another option (although probably not the best for this application it would still be an effective and viable solution) is to develop a small protocol to give the gen 6 direct access to the arduino's pins (it's simpler than it sounds), each byte sent via i2c could contain a pin number in the lowest 4 bits, its state in the fifth and its mode in the sixth and then perhaps for efficiency the seventh bit could be to determine if the pin mode needs changing, that leaves one bit spare if you need it and therefore could be used to indicate that the next byte is to set an analog value (or the next two bytes if it is a high res DAC like yours), this would simplify the job that the arduino does and give full control to the gen 6, this would make switching on digital output 4 on the arduino from the gen 6 as simple as sending B00010100 down the i2c line and make setting an analog value as simple as sending these three: B10000000 B00111111 B11111111 (this example would set the 14 bit analog output to its maximum output) this would just turn the arduino into a simple i2c IO expander
How to implement this and please tell me there is no need to change anything in the Gen 6 code for this method......
Re: Arduino with Gen 6 board
August 23, 2011 03:44AM
Javaid Butt Wrote:
-------------------------------------------------------
> I will work on that part but in the meantime,
> let's work out the second method. May be it will
> prove to be viable.
>
> Another option (although probably not the best for
> this application it would still be an effective
> and viable solution) is to develop a small
> protocol to give the gen 6 direct access to the
> arduino's pins (it's simpler than it sounds), each
> byte sent via i2c could contain a pin number in
> the lowest 4 bits, its state in the fifth and its
> mode in the sixth and then perhaps for efficiency
> the seventh bit could be to determine if the pin
> mode needs changing, that leaves one bit spare if
> you need it and therefore could be used to
> indicate that the next byte is to set an analog
> value (or the next two bytes if it is a high res
> DAC like yours), this would simplify the job that
> the arduino does and give full control to the gen
> 6, this would make switching on digital output 4
> on the arduino from the gen 6 as simple as sending
> B00010100 down the i2c line and make setting an
> analog value as simple as sending these three:
> B10000000 B00111111 B11111111 (this example would
> set the 14 bit analog output to its maximum
> output) this would just turn the arduino into a
> simple i2c IO expander
>
> How to implement this and please tell me there is
> no need to change anything in the Gen 6 code for
> this method......

This is the method to use if you want to give the gen 6 the ability to control the speed of the dispenser and it will need modification of the gen 6 code

To show you how to code this I would need to know the resolution of your DAC. (Was it fourteen bits?)
Re: Arduino with Gen 6 board
August 23, 2011 09:31AM
Quote

This is the method to use if you want to give the gen 6 the ability to control the speed of the dispenser and it will need modification of the gen 6 code
Will it be easy to doconfused smiley

The DAC is 12 bit (MCP 4921).
Here is the link.
[ww1.microchip.com]

By using this method, will I be able to print out anything with Repsnapper in the conventional way or not ?

Edited 1 time(s). Last edit at 08/23/2011 01:29PM by Javaid Butt.
Re: Arduino with Gen 6 board
August 23, 2011 05:57PM
I am just asking to confirm as you didn't reply to this post. I have this code in the Arduino now. I am a bit curious about the void setup (don't know whyconfused smiley)
#include "Wire.h"

//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
int ledPin = 2;                 // LED connected to digital pin 2

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  Wire.begin(CP_ADDR);
  Wire.onReceive(receiveEvent); // register event so that anything received on i2c is sent to the serial
//Test gcode, this should send the machine's X, Y and Z to home
  sendGCode("G28 X0 Y0 Z0\n");
}

void receiveEvent(int howMany)
{
  while(0 < Wire.available()) // loop through all
  {
    char c = Wire.receive(); // receive byte as a character
    if (c = '1') 
    {
      //turn dispenser on
      digitalWrite(2, HIGH); //replace the number 2 with the dispenser pin number
    }
    if (c = '0') 
    {
      //turn dispenser off
      digitalWrite(2, LOW);
    }
  }
}

void sendGCode(char* GCode)
{
  Wire.beginTransmission(REP_RAP_ADDR);
  Wire.send(GCode);
  Wire.endTransmission();   
}

void loop()
{
  
}

Is this fine?

I also made changes to the main code as suggested and someone else has also mentioned the same place to make changes. Although the comment was find where e is 0 and where e>0. This is where e > 0. From line 275-291.
if (e_can_step)
		{
			dda_counter.e += delta_steps.e;
			
			if (dda_counter.e > 0)
			{
                                
				do_e_step();
                                real_move = true;
				dda_counter.e -= total_steps;
				
				if (e_direction)
					current_steps.e++;
				else
					current_steps.e--;
			}
		}

This is when e is 0. Line 33 in cartesian_dda.pde file.
// Default to the origin and not going anywhere
  
	target_position.x = 0.0;
	target_position.y = 0.0;
	target_position.z = 0.0;
	target_position.e = 0.0;
        target_position.f = SLOW_XY_FEEDRATE;
So you were absolutely correct.
But by following what you suggested before, the LED remains OFF. It doesn't turn ON even for a second.
I am sending custom g codes to Repsnapper with E1 and E0 to check whether it turns ON and OFF but to no avail.
Just asking to confirm, is the setup not correct?

And is this new information any useful about when e is at 0? How can it be used to achieve the objective?

Edited 1 time(s). Last edit at 08/23/2011 07:15PM by Javaid Butt.
Re: Arduino with Gen 6 board
August 24, 2011 05:14PM
I have come up with a different idea. I'll just walk you through that.

I have introduced some new G Codes in process_g_code file after line 605 (case 92)
                       case 100: 
                               Wire.beginTransmission (5);
                               Wire.send ("G100"); 
                               Wire.endTransmission();
				break;

                        case 101: 
                               Wire.beginTransmission (5);
                               Wire.send ("G101"); 
                               Wire.endTransmission();
				break;

After this, I changed the code in the Gen 6 board in order to send G Code from it to Arduino.
This was initially in Gen 6
void receiveEvent(int howMany)
{

}

void requestEvent()
{
  Wire.send("start");
}
I changed it to
//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
void setup()
{  
  Wire.begin(4);
}
void loop()
{
 
}

So that I can send G Codes from Gen 6 to Arduino. And now Arduino has this code.
#include "Wire.h"

//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
int ledPin = 2;

void setup()
{
  Wire.begin(CP_ADDR);
  Wire.onReceive(receiveEvent); // register event so that anything received on i2c is sent to the serial 
  pinMode(ledPin, OUTPUT);  
}

void receiveEvent(int howMany)
{
  char incoming[128]; // Set this to the maximum expected string length +1 for a null char 
  char length;
  while(0 < Wire.available()) // loop through all
   {
     delay(100); 
    length = Wire.available();
    for (int i = 0; i < length; i++) {
        if(i > 128){                       //prevent data corruption in case you accidentally receive more than the array can hold
            incoming[128] = '\0';  //put a null char at the end of the array
            break;
        } 
        incoming = Wire.receive();
        }
    }
    char str1[] = "G100";
 char str2[] = "G101"; 
  
  if (strcmp(str1, incoming) == 0){
      //turn dispenser on
      digitalWrite(2, HIGH); }
    
    if (strcmp(str2, incoming) == 0) {    
      //turn dispenser off
      digitalWrite(2, LOW);}
}

In the Arduino, first I am storing anything that comes via I2C in a string and then I am comparing it to G100 and G101.

This is just something that I come up with but right now it seems that the LED is not receiving anything because it remains OFF no matter what I send. I think the problem is in the code of Gen 6. May be Arduino as well, I really don't know.

Kindly have a look at this and let me know.

Edited 1 time(s). Last edit at 08/28/2011 02:19PM by Javaid Butt.
Re: Arduino with Gen 6 board
August 28, 2011 06:06PM
Sorry I haven't posted in a while, I somehow didn't realise you had made more posts. (the email system in this forum seems unreliable) I will go through what I've "missed" and write another post tomorrow.
Re: Arduino with Gen 6 board
August 28, 2011 07:16PM
.

Edited 1 time(s). Last edit at 08/28/2011 07:27PM by Javaid Butt.
Re: Arduino with Gen 6 board
August 28, 2011 07:24PM
Yes, the e-mail system is not so great here at this forum.

Anyways, I was just working through the code when I thought of introducing some new g codes, G100 & G101. So that I write g code in repsnapper in this format:
G100
G1 X10 Y10 (for this command, I will get 1 at output, LED on)
G101
G1 X15 Y15 (for this command, I will get 0 at output, LED off)

I thought it would be a good idea, without disturbing any of the Reprap code but I can't seem to send the signal from Reprap to Arduino. When I upload what I have posted, the LED remains OFF, like it is not receiving anything. Have a look at the code and I am pretty sure that you will find a lot of mistakes (because it is not working and also because I did the coding tongue sticking out smiley)

And thanks a lot for helping me out smiling smiley
Re: Arduino with Gen 6 board
September 01, 2011 03:16PM
I thought, first I should turn the led ON and OFF with the existing codes; G1 (ON) and G28 (OFF).

I put this is Reprap.
#include 

#define reprap 6
#define slave 5

void setup ()
 {
 Wire.begin (reprap);
 }
 
void loop()
{
  Wire.beginTransmission (slave);
  Wire.send ("G1"); 
  Wire.endTransmission();
  
  Wire.beginTransmission (slave);
  Wire.send ("G28");
  Wire.endTransmission();
}

And this in Arduino.
#include 

#define slave 5
#define LED 13

void receiveEvent (int howMany)
 {
  char buf [10];
  byte i = 0;

  while (Wire.available () > 0)
    {
    char c = Wire.receive ();
    if (i < sizeof (buf) - 1)  // check for overflow
      buf [i++] = c;
    }  // end of while

   buf  = 0;  // terminating null
   
  if (memcmp (buf, "G1", 2) == 0)
    digitalWrite (LED, HIGH);
  else if (memcmp (buf, "G28", 3) == 0)
    digitalWrite (LED, LOW);
}  // end of receiveEvent


void setup () 
{
  Wire.begin (slave);
  Wire.onReceive (receiveEvent);
  pinMode (LED, OUTPUT);
}  // end of setup

void loop() 
{
// nothing in main loop
}
This is the code that compares any string starting with G1 or G28 with the incoming data.
But the problem is that the LED remains ON and when I send G1 or G28, it turns OFF. It does flicker very quickly on execution of each command but still not turning ON when it receives G1 and OFF on G28. Just have a look and see if you can figure it out.
Sorry, only registered users may post in this forum.

Click here to login