Hi, I work on my project a laser projector and I need to accelerate compute the position x, y.
Slowest is compute the separation step to obtain the shift in the x and y axis.
step_x = diff_x / steps_f;
step_y = diff_y / steps_f;
You do not know someone faster method of calculating the displacement between two points?
Thanks for your help
Peter B
ps. I use MCP4922 ( 12-Bit DAC with SPI Interface )
Here is my code:
#define LDACPIN_LOW ( PORTB &= B11111011 ) // pin 10
#define LDACPIN_HIGH ( PORTB |= B00000100 )
#define SDIPIN_LOW ( PORTB &= B11110111 ) // pin 11
#define SDIPIN_HIGH ( PORTB |= B00001000 )
#define SCKPIN_LOW ( PORTB &= B11101111 ) // pin 12
#define SCKPIN_HIGH ( PORTB |= B00010000 )
#define CSPIN_LOW ( PORTB &= B11011111 ) // pin 13
#define CSPIN_HIGH ( PORTB |= B00100000 )
#define CLOCK SCKPIN_HIGH; SCKPIN_LOW;
#define SET_HIGH SDIPIN_HIGH; CLOCK;
#define SET_LOW SDIPIN_LOW; CLOCK;
int last_x = 0;
int last_y = 0;
int move_counter = 0;
void setup(){
DDRB = DDRB | B11111111;
CSPIN_HIGH;
SCKPIN_LOW;
SDIPIN_LOW;
LDACPIN_HIGH;
}
void set_dac_2 ( int value_a, int value_b ){
// --- output A ---
CSPIN_LOW;
SDIPIN_HIGH; CLOCK; // 15
SDIPIN_LOW; CLOCK; // 14
SDIPIN_HIGH; CLOCK; // 13
CLOCK; // 12
SDIPIN_LOW; if ( 2048 & value_a ){ SDIPIN_HIGH; } CLOCK; // 11
SDIPIN_LOW; if ( 1024 & value_a ){ SDIPIN_HIGH; } CLOCK; // 10
SDIPIN_LOW; if ( 512 & value_a ){ SDIPIN_HIGH; } CLOCK; // 09
SDIPIN_LOW; if ( 256 & value_a ){ SDIPIN_HIGH; } CLOCK; // 08
SDIPIN_LOW; if ( 128 & value_a ){ SDIPIN_HIGH; } CLOCK; // 07
SDIPIN_LOW; if ( 64 & value_a ){ SDIPIN_HIGH; } CLOCK; // 06
SDIPIN_LOW; if ( 32 & value_a ){ SDIPIN_HIGH; } CLOCK; // 05
SDIPIN_LOW; if ( 16 & value_a ){ SDIPIN_HIGH; } CLOCK; // 04
SDIPIN_LOW; if ( 8 & value_a ){ SDIPIN_HIGH; } CLOCK; // 03
SDIPIN_LOW; if ( 4 & value_a ){ SDIPIN_HIGH; } CLOCK; // 02
SDIPIN_LOW; if ( 2 & value_a ){ SDIPIN_HIGH; } CLOCK; // 01
SDIPIN_LOW; if ( 1 & value_a ){ SDIPIN_HIGH; } CLOCK; // 00
CSPIN_HIGH;
// --- output B ---
CSPIN_LOW;
SDIPIN_LOW; CLOCK; // 15
CLOCK; // 14
SDIPIN_HIGH; CLOCK; // 13
CLOCK; // 12
SDIPIN_LOW; if ( 2048 & value_b ){ SDIPIN_HIGH; } CLOCK; // 11
SDIPIN_LOW; if ( 1024 & value_b ){ SDIPIN_HIGH; } CLOCK; // 10
SDIPIN_LOW; if ( 512 & value_b ){ SDIPIN_HIGH; } CLOCK; // 09
SDIPIN_LOW; if ( 256 & value_b ){ SDIPIN_HIGH; } CLOCK; // 08
SDIPIN_LOW; if ( 128 & value_b ){ SDIPIN_HIGH; } CLOCK; // 07
SDIPIN_LOW; if ( 64 & value_b ){ SDIPIN_HIGH; } CLOCK; // 06
SDIPIN_LOW; if ( 32 & value_b ){ SDIPIN_HIGH; } CLOCK; // 05
SDIPIN_LOW; if ( 16 & value_b ){ SDIPIN_HIGH; } CLOCK; // 04
SDIPIN_LOW; if ( 8 & value_b ){ SDIPIN_HIGH; } CLOCK; // 03
SDIPIN_LOW; if ( 4 & value_b ){ SDIPIN_HIGH; } CLOCK; // 02
SDIPIN_LOW; if ( 2 & value_b ){ SDIPIN_HIGH; } CLOCK; // 01
SDIPIN_LOW; if ( 1 & value_b ){ SDIPIN_HIGH; } CLOCK; // 00
CSPIN_HIGH;
LDACPIN_LOW;
SCKPIN_HIGH;
// SCKPIN_HIGH;
LDACPIN_HIGH;
SCKPIN_LOW;
}
void jump ( int x, int y ){
last_x = x;
last_y = y;
set_dac_2 ( last_x, last_y );
}
void move ( int x, int y ){
byte max_step_size = 16;
word diff_x = 0;
word diff_y = 0;
word steps = 0;
float steps_f = 0;
float step_x = 0;
float step_y = 0;
byte directions = 0;
float val_x = last_x;
float val_y = last_y;
if ( x > last_x ){
diff_x = x - last_x;
} else {
diff_x = last_x - x;
directions |= 1;
}
if ( y > last_y ){
diff_y = y - last_y;
} else {
diff_y = last_y - y;
directions |= 2;
}
if ( diff_x < diff_y ){
steps = ( diff_y / max_step_size ) + 1;
} else {
steps = ( diff_x / max_step_size ) + 1;
}
steps_f = steps;
step_x = diff_x / steps_f;
step_y = diff_y / steps_f;
while ( steps ){
if ( directions & 1 ){
val_x -= step_x;
} else {
val_x += step_x;
}
if ( directions & 2 ){
val_y -= step_y;
} else {
val_y += step_y;
}
set_dac_2 ( ( int ) val_x, ( int ) val_y );
move_counter++;
steps--;
}
last_x = x;
last_y = y;
}
void loop() {
// int start = millis();
move_counter = 0;
jump ( 2048, 1 );
move ( 2048, 1 );
move ( 2048, 2048 );
move ( 1, 2048 );
move ( 1, 1 );
/*
Serial.println ( millis() - start );
Serial.println ( move_counter );
*/
}