2009/Nodos/Arduino y POV/Documentación

De Hackmeeting

Revisión a fecha de 08:38 17 oct 2009; Pang (Discutir | contribuciones)
(dif) ← Revisión anterior | Ver revisión actual (dif) | Revisión siguiente → (dif)
  /* arduino code
   *
   * Los modulos tienen 8 leds cada uno
   * Las longitudes se miden en centimetros
   * La velocidad del dibujo en centimetros por segundo
   *
   * Este codigo todavia no se ha probado!!
   * Quedaremos los lunes de cacharreo en el hamlab
   * despues del hackmeeting para rematar el proyecto
   *  
   * Os mantendremos al tanto: seguid el links a la documentacion de la charla
   *  
   */
 
 #include <math.h>
 #include <avr/pgmspace.h>
 
 #define RESOLUCION 16
 #define LARGO 8
 #define TOTAL_MODULOS 6
 #define LONGITUD_MODULO 10
 #define RADIO 30
 #define PIN_HALL 12
 #define PIN_DATA 11
 #define PIN_CLOCK 10
 #define PIN_LATCH 9
 #define VELOCIDAD 3
 
  cart[RESOLUCION*LARGO/8][RESOLUCION] PROGMEM={
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     {0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF},
     };
 
 double radios[TOTAL_MODULOS]={5,15,5,15,5,15};
 double thetas[TOTAL_MODULOS]={0,0,PI/3,PI/3,2*PI/3,2*PI/3};
 
 const double distancia=LONGITUD_MODULO/8; //distancia entre LEDs consecutivos
 double r;
 double theta;
 double seno, coseno;
 double drift;
 // cartesian coordinates
 int x ,y;
 uint8_t boton;
 uint8_t palabra;
 
 //tiempos
 unsigned long inicio;      //initial time
 unsigned long last;        //time of last pass of the magnet
 unsigned long before_last; //time of before-last pass of the magnet
 unsigned long current; //current time
 
 
 //setup
 void setup() {
 
    // arbitrary init values for last and before-last
    inicio = millis();
    last = millis();
    before_last = last - 2000;
 
     pinMode(PIN_DATA, OUTPUT);
     pinMode(PIN_LATCH, OUTPUT);
     pinMode(PIN_CLOCK, OUTPUT);
 }
 
 void loop(){
 
     uint_t bot2=analogRead(PIN_HALL);    
     //TODO
     if( bot2>600 & boton<=600){  //rising
         before_last = last;
         last = millis();
     }
     boton=bot2;
     
     current = millis();
 
     theta = (2*PI*(current-last))/(last-before_last);
     drift = (current-inicio)*VELOCIDAD/1000;
     //nos aseguramos que theta no sobrepase 2*PI
     if(theta> 2*PI)
         theta=0;
     //nos aseguramos que drift no sobrepase LARGO*2*RADIO
     if(drift>LARGO*2*RADIO)
         drift-=LARGO*2*RADIO;
     digitalWrite(PIN_LATCH, 0);
     for(short j=0;j<TOTAL_MODULOS;j++){
         seno = sin(theta+thetas[j]);
         coseno = cos(theta+thetas[j]);
         palabra = 0;
         for(short i=0; i<8; i++){
             r = radios[i]+distancia*i;
             x = (int)((r*coseno+drift)*RESOLUCION/(2*RADIO)+RESOLUCION/2);
             y = (int)(r*seno*RESOLUCION/(2*RADIO)+RESOLUCION/2);
             x=x%(RESOLUCION*LARGO);
             palabra| = (cart[x/8][y]&(1<<x%8)) <<i;
             }
         shiftOut(PIN_DATA,PIN_CLOCK,MSBFIRST,palabra);
         }
     digitalWrite(PIN_LATCH, 1);
 
 
 }
Herramientas personales
wiki-navigation
project-navigation