Wednesday, January 18, 2017

Minimalist XY firmware for arduino leonardo + CNC Shield

everything is hardcoded
the commands contain relative movements
there's no units or you could say the units are motor steps

only XY are taken in considerations

to move the pen by ( 12,23 ) steps the command is

G0 X12 Y23 Z0

//the basic hardware control was inspired HC_CNC_Shield_Test by Andrew Davies

#define EN 8

#define X_STEP 2
#define X_DIR 5 
#define Y_STEP 3
#define Y_DIR 6
#define Z_STEP 4
#define Z_DIR 7

#define A_DIR 13  /* Direction pin for Aux driver. Requires D13 and A-DIR pins to be shorted */
#define A_STEP 12 /* Direction pin for Aux driver. Requires D12 and A-STEP pins to be shorted */

#define X_ENDSTOP 9   /* X axis endstop input pin */
#define Y_ENDSTOP 10  /* Y axis endstop input pin */
#define Z_ENDSTOP 11  /* Z axis endstop input pin */
#define ABORT A0  /* Abort input pin */
#define HOLD A1   /* Hold input pin */
#define RESUME A2 /* Resume input pin */

#define COMMAND_SIZE 128
char command[COMMAND_SIZE];
byte serial_count = 0;
boolean bytes_received = false;

void setup()
{
    /* Configure the steper drive pins as outputs */ 
  pinMode(EN, OUTPUT); 
  pinMode(X_DIR, OUTPUT); 
  pinMode(X_STEP, OUTPUT); 
  pinMode(Y_DIR, OUTPUT); 
  pinMode(Y_STEP, OUTPUT); 
  pinMode(Z_DIR, OUTPUT); 
  pinMode(Z_STEP, OUTPUT); 
  pinMode(A_DIR, OUTPUT); 
  pinMode(A_STEP, OUTPUT); 
  
  /* Configure the control pins as inputs with pullups */
  pinMode(X_ENDSTOP, INPUT_PULLUP);
  pinMode(Y_ENDSTOP, INPUT_PULLUP);
  pinMode(Z_ENDSTOP, INPUT_PULLUP);

  pinMode(ABORT, INPUT_PULLUP);
  pinMode(HOLD, INPUT_PULLUP);
  pinMode(RESUME, INPUT_PULLUP);

  /* Enable the X, Y, Z & Aux stepper outputs */
  digitalWrite(EN, HIGH); //Low to enable
  Serial.begin(9600);
}

void loop()
{
  char c;
  if( Serial.available() > 0 )
  {
    c = Serial.read();
    if( c != '\n' )
    {
        command[serial_count] = c;
        serial_count++;
    }
    else
    {
        command[serial_count] = 0;
        serial_count++;
    }
    bytes_received = true;
  }
  if( bytes_received && ( c == '\n' ) )
  {
    doit( command );
    serial_count = 0;
    bytes_received = false;
  }
}

void doit( char *input )
{
  char* command = strtok( input, " \n" );
  int G_val = -1;
  long X_val = 0;
  long Y_val = 0;
  long Z_val = 0;
  while( command != 0 )
  {
    switch( command[0] )
    {
      case 'G':
        G_val = atoi( command + 1 );
      break;
      case 'X':
        X_val = atol( command + 1 );
      break;
      case 'Y':
        Y_val = atol( command + 1 );
      break;
      case 'Z':
        Z_val = atol( command + 1 );
      break;
    }
    command = strtok(0, " \n" );
  }
  char mesg[32];
  sprintf( mesg, "%d %ld %ld %ld", G_val, X_val, Y_val, Z_val );
  Serial.println( mesg );
  digitalWrite(EN, LOW); //Low to enable
  motorizin( G_val, X_val, Y_val, Z_val );
  digitalWrite(EN, HIGH); //Low to enable
}

//------------------------------------------------------------------------------
void motorizin( int G_val, long X_val, long Y_val, long Z_val )
{
  switch( G_val )
  {
    case 0:
    case 1:
      line( X_val, Y_val, Z_val );
    break;
  }
}

//------------------------------------------------------------------------------
//first version of this program - only relative moves supported
//------------------------------------------------------------------------------
void line( long dx, long dy, long dz )
{
  digitalWrite( X_DIR, dx >= 0?LOW:HIGH );  
  digitalWrite( Y_DIR, dy >= 0?LOW:HIGH ); 
  long adx = abs( dx );
  long ady = abs( dy );
  if( adx >= ady ) 
  {
    subplot( 0, adx, ady );
  }
  else
  {
    subplot( 1, ady, adx );
  }
}

//------------------------------------------------------------------------------
void subplot( bool horizVert, long dx, long dy )
{  
  long D = 2 * dy - dx;
  bool stepy = false;
  for( long x = 1; x <= dx; x++ )
  {
    if( D > 0 )
    {
       stepy = true;
       D = D - dx;
    }
    D = D + dy;
    if( horizVert == 0 )
    {
      subsubplot( true, stepy );
  }
  else
  {
      subsubplot( stepy, true );
  }    
  stepy = false;
  }
}

//------------------------------------------------------------------------------
void subsubplot( long stepx, long stepy )
{
  if( stepx > 0 )
  {
    digitalWrite( X_STEP, HIGH ); 
  }
  if( stepy > 0 )
  {
    digitalWrite( Y_STEP, HIGH ); 
  }
  delay( 10 );
  digitalWrite( X_STEP, LOW );  
  digitalWrite( Y_STEP, LOW ); 
}

Tuesday, January 17, 2017

Bresenham Line ( relative ) implementation
written/tested in Daz Studio but will be in an arduino Leonardo plotter firmware


var dirx;
var diry;

plot( -10, -3 );

function plot( dx, dy ) // this is a relative movement
{
  dirx = ( dx >= 0 );
  diry = ( dy >= 0 );
  var adx = Math.abs( dx );
  var ady = Math.abs( dy );
  if( adx >= ady ) 
  {
   subplot( 0, adx, ady );
  }
  else
  {
   subplot( 1, ady, adx );
  }
}

function subplot( horizVert, dx, dy )
{  
  var D = 2 * dy - dx;
  var stepy = 0;
  for( x = 1; x <= dx; x++ )
  {
    if( D > 0 )
    {
       stepy = 1;
       D = D - dx;
 }
    D = D + dy;
   if( horizVert == 0 )
   {
     subsubplot( 1, stepy );
 }
 else
 {
     subsubplot( stepy, 1 );
 }    
 stepy = 0;
  }
}

function subsubplot( stepx, stepy )
{
 debug( stepx, stepy );
}

Bresenham Line implementation in Daz Studio Script

// DAZ Studio version 4.9.2.70 filetype DAZ Script

//plot( 3, 10 );
//plot( 10, 3 );
//plot( -3, 10 );
//plot( 10, -3 );
//plot( 3, -10 );
//plot( -10, 3 );
//plot( -3, -10 );
plot( -10, -3 );

var dirx;
var diry;

function plot( dx, dy )
{
  var adx = Math.abs( dx );
  var ady = Math.abs( dy );
  dirx = dx >= 0; //replace by set motor x direction
  diry = dy >= 0; //replace by set motor x direction
  if( adx >= ady ) 
  {
   subplot( 0, adx, ady );
  }
  else
  {
   subplot( 1, ady, adx );
  }
}

function subplot( horizVert, dx, dy )
{  
  var D = 2 * dy - dx;
  var y = 0;
  for( x = 0; x <= dx; x++ )
  {
   if( horizVert == 0 )
   {
     subsubplot( x, y );
   }
   else
   {
     subsubplot( y, x );
   }
   if( D > 0 )
   {
     y = y + 1
     D = D - dx
   }
  D = D + dy
  }
}

function subsubplot( x, y )
{
 debug( !dirx ? -x : x, !diry ? -y : y ); 
}



New ideas for an A.I.

Stupid idea that gave rise to this

while trying to adapt the Bresenham line algorithm for the firmware of a CNC machine ( 2 stepper motors ) i was thinking "it would be a good thing to really understand the principle Bresenham founded this on. I think it's called "accumulation of error"

https://en.wikipedia.org/wiki/Bresenham's_line_algorithm

i would add that the "auto-correction" phase is the other important cog of that machine

-----

then i thought, simple machinations of this type could be the base for my A.I.

-----

then i thought in a way solving a problem is just re-stating the question, or rather the object at the center of this question. Among the infinity(?) of ways to restate it, we want to pick the re-stating that satisfies the questioner ... or ... pick the restating that


for example, the question "what is the universe?"

we determine the object of the question is "the universe"

we presume there are many ways to restate "the universe"

- something big
- something we live in
- something humans think it is
- something infinite
- something eternal
- something finite

then it gets complicated

----

since i want to stay on simplicity-road  ...

----

 i'll just keep in mind the idea of  re-stating ( paraphrasing, tautologies, vérité de la Palice  etc )


Sunday, January 1, 2017

I may try to invent an A.I. in 2017

so i'll post about this sometimes

--------
mcjAI - 00000

Maybe an important step in inventing an A.I. is to name it properly, for instance, the A.I. that strive to pass the Turing test could be named Fake-Human-Dialog'ers. Personally, the A.I. that interests me more is an Artist Intelligence, a machine that produces interesting art, like drawings, songs and storytellings. Not necessarily a machine that answers questions or invents other machines. But but but, maybe if i can devise an art-generating machine then, as an aside, it will be able to (understand and ) answer questions and invent machines.
----------

here's a test on a daz script that uses a dictionary to identify nouns and adjectives in a given text, then replaces them by other nouns and verbs



the original text was a translation of Nostradamus quatrains

------------
from vertiginous deepest matriarch of visceral europe man
a class-conscious houri will be heroic to helical people mesosphere
who will by his airstrip seduce a calculating multitude, likeness
his fat-head will increase in numerate generality of recent east"
------------------------------------
beasts subliminal with whig will nymph oxonian rivers,
heart-rending greater harpoon of woolly posterity will be against hister.
into a fighter of orang-utan will corny stagnant silky be drawn,
when wall-to-wall fen of germany observes nothing.
------------------------------------
in preshrunk gland very not far from venus,
unreadable questionable greatest ones of asia and of africa:
they are said to have come from soppy rhine and from molecule
hister cries, tears at malta and puffy ligurian sea-coast.
------------------------------------
amperage will not be regained,
it will be occupied by a black, proud, stocky and get-at-able man:
when lewd intervention of prone lumber-room is opened,
serried leakage of venice will be inviolate by hister.
------------------------------------
hardy scrappy and linnaean armed over-sensitive will sol-fa historic decorative furnace,
operatic unwitting chosen one, collective captives not returning
prenatal world's treacherous crime, delectable aperient politic irale - israel - not at ease,
barb, hister, malta, and unborn remittent unreflecting does not return.
Happy New Year !