Permalink
Browse files

- added first version firmware working with example gcode "octopus"

  • Loading branch information...
cemonds committed May 16, 2016
1 parent 1767188 commit 30ed715b7ba6564f2ed01a573c613a2594190b94
@@ -140,21 +140,24 @@ void processCommand() {
case 0: // move linear
case 1: // move linear
// Move Linear here
do_move(parsenumber('Y',0), parsenumber('Z',0), parsenumber('F',30000));
//do_move(parsenumber('Y',0), parsenumber('Z',0), parsenumber('F',30000));
break;
case 3:
delay(parsenumber('D',0));
break;
case 4:
set_exposing_cycles(parsenumber('E',0));
break; // dwell
case 5:
fill_laser_buffer(parsedistance('D',0));
break;
case 6:
delay(20);
expose_line(parsenumber('E',1000));
expose_line();
break;
case 7:
set_exposing_cycles(5000);
create_test_pattern();
expose_line(parsenumber('E',50000));
expose_line();
break;
case 29:
//home z- axis
@@ -41,7 +41,7 @@ uint16_t laser_timing_center_offset = LASER_TIMING_CENTER_OFFSET;

enum class LaserState : uint8_t { WAITING_FOR_SYNC, READY_FOR_LINE };

static LaserState laser_state;
volatile static LaserState laser_state;
static uint16_t last_line_start;

uint16_t exposing_cycles = 5;
@@ -52,8 +52,10 @@ LaserTimer laserTimer;

// Opto Capture ISR
ISR(TIMER1_CAPT_vect){
//Serial.println("Opto");
// Serial.print(".");
last_line_start = laserTimer.getInputCaptureTime(); // save current counter value
// last_line_start = ICR1; // save current counter value
//last_line_start = ICR1; // save current counter value

laserTimer.forceClearPinOnMatch();
//TCCR1A = (1<<COM1A1) ; //turn off output on compare match
@@ -63,8 +65,8 @@ ISR(TIMER1_CAPT_vect){
laserTimer.disableInputCapture();
//TIMSK1 &= ~(1<<ICIE1); //turn ourselves off

uint16_t start_time = last_line_start + begin_delay; //calculate at which timer value to begin lasering
//uint16_t start_time = ICR1 + begin_delay; //calculate at which timer value to begin lasering
//uint16_t start_time = last_line_start + begin_delay; //calculate at which timer value to begin lasering
uint16_t start_time = ICR1 + begin_delay; //calculate at which timer value to begin lasering
laserTimer.setOutputCompareRegisterA(start_time);
//OCR1A = start_time; //set OCR1A to that value (this is used from laser write line)
laserTimer.setOutputCompareRegisterB(start_time - 500);
@@ -84,12 +86,13 @@ void laser_write_line(){
//TIFR1 = (1<<OCF1A);

laserTimer.toggleOnCompareMatch();
//TCCR1A = (1<<COM1A0); //Toggle laser on compare match from now
//TCCR1A = (1<<COM1A0); //Toggle laser on compare match from now

do {
laserTimer.waitForCompareRegisterAMatch();
//while ((TIFR1 & (1 << OCF1A)) == 0); //and wait for it...
//TIFR1 = (1 << OCF1A);
//Serial.print(".");

uint16_t tmp = *timings++;

@@ -99,14 +102,17 @@ void laser_write_line(){
laserTimer.addToOutputCompareRegisterA(tmp);
//OCR1A += tmp; //no: delay that amount
} while(1);
//Serial.println("");
}

ISR(TIMER1_COMPB_vect){
if(laser_state == LaserState::READY_FOR_LINE){
ISR(TIMER1_COMPB_vect){
if(laser_state == LaserState::READY_FOR_LINE){

TCCR1A = (1<<COM1A1) | (1<<FOC1A);

if(exposing){
// Serial.print("Exposing ");
// Serial.println(exposed_cycles);
laser_write_line();
++exposed_cycles;
} else {
@@ -147,6 +153,7 @@ ISR(TIMER1_COMPB_vect){


} else {
// Serial.println("Missing sync");

// WAITING_FOR_SYNC (we have missed the sync pulse)
laserTimer.forceClearPinOnMatch();
@@ -170,61 +177,99 @@ ISR(TIMER1_COMPB_vect){

// fill data table with test pattern
void create_test_pattern(){

for(int i=0; i < 80; ++i){
laser_buffer.laser_timings[i] = 500-i;
#define TEST_BLOCKS 32
for(int i=0; i < TEST_BLOCKS; ++i){
// laser_buffer.laser_timings[i] = 500;
laser_buffer.positions[i] = (i==0) ? (2000 * (i-TEST_BLOCKS/2)) : 2000;
}
laser_buffer.laser_timings[81] = 0;
laser_buffer.length = 0;
// laser_buffer.laser_timings[80] = 0;
laser_buffer.positions[TEST_BLOCKS] = 0;
laser_buffer.length = TEST_BLOCKS;
}

void set_exposing_cycles(uint16_t cycles) {
exposing_cycles = cycles;
}

void expose_line(uint16_t cycles) {
exposing_cycles = cycles;
exposed_cycles = 0;
void expose_line() {
convert_laser_buffer();
exposed_cycles = 0;

exposing = 1;
exposing = 1;
//Serial.println("Start exposing");

while(exposing);
while(exposing);

laser_buffer.length = 0;
//Serial.println("Raus");
}

// initialize laser
void init_laser_driver(){

create_test_pattern();

SET_DDR(LASER);

SET_DDR(LASER_PWM);
OUTPUT_ON(LASER_PWM);
pinMode(LASER_ENABLE_PIN, OUTPUT);
digitalWrite(LASER_ENABLE_PIN, HIGH);

SET_DDR(LASER_PWM);
OUTPUT_ON(LASER_PWM);

laserTimer.startTimer();
//TCCR1B = 1;

laser_state = LaserState::WAITING_FOR_SYNC;
laserTimer.enableInputCapture();
}

void convert_positions_to_timings() {
long current_position = 0;
long distance = 0;
uint16_t timing;
//Serial.println("--");
for(int i = 0; i < laser_buffer.length; ++i) {
distance = laser_buffer.positions[i];
current_position += laser_buffer.positions[i];
timing = (uint16_t) ((distance * laser_timing_scale_dividend) / laser_timing_scale_divisor);
if(i == 0) {
timing += laser_timing_center_offset;
}
/*
Serial.print(current_position);
Serial.print(";");
Serial.print(distance);
Serial.print(";");
Serial.print(timing);
Serial.print("|");*/
laser_buffer.laser_timings[i] = timing;
}
laser_buffer.laser_timings[laser_buffer.length] = 0;
}

void convert_laser_buffer(){
if(!laser_buffer.contains_timings) {
laser_buffer.positions[laser_buffer.length] = 0;
convert_positions_to_timings();
laser_buffer.contains_timings = true;
}
}

void fill_laser_buffer(long distance){
if (distance == 0) {
laser_buffer.positions[laser_buffer.length] = 0;
convert_positions_to_timings();
if(laser_buffer.contains_timings) {
laser_buffer.length = 0;
laser_buffer.contains_timings = false;
}
if (distance == 0) {
// convert_laser_buffer();
} else {
laser_buffer.positions[laser_buffer.length] = distance;
++laser_buffer.length;
}
// delay(1);
}

void convert_positions_to_timings() {
long current_position = 0;
for(int i = 0; i < laser_buffer.length; ++i) {
current_position += laser_buffer.positions[i];
laser_buffer.laser_timings[i] = current_position * laser_timing_scale_dividend / laser_timing_scale_divisor + laser_timing_center_offset;
}
laser_buffer.laser_timings[laser_buffer.length] = 0;
}

// Turn Laser Timer on and activate Opto Capture
void laser_on(){

laserTimer.setValue(1);
//TCNT1 = 1;

@@ -29,6 +29,7 @@
#include "configuration.h"

typedef struct {
bool contains_timings;
uint8_t length;
union {
long positions[LASER_POSITIONS_BUFFER_SIZE];
@@ -39,10 +40,13 @@ typedef struct {
void init_laser_driver();
void laser_on();
void laser_off();
void expose_line(uint16_t cycles);
void set_exposing_cycles(uint16_t cycles);
void expose_line();
void fill_laser_buffer(int32_t distance);
void create_test_pattern();
void convert_positions_to_timings();
void convert_laser_buffer();


#endif // OPENEXPOSER_LASERDRIVER_H

@@ -30,25 +30,25 @@ class LaserTimer
{

public:
void startTimer();
void setValue(uint16_t value);
void disableTimerPin();
void forceClearPinOnMatch();
void setOnCompareMatch();
void toggleOnCompareMatch();
void enableInputCapture();
void disableInputCapture();
void clearInputCaptureFlag();
void clearInputCaptureAndOutputCompareBFlag();
uint16_t getInputCaptureTime();
void enableInputCaptureAndOutputCompareBInterrupt();
void disableInputCaptureAndOutputCompareBInterrupt();
void setOutputCompareRegisterA(uint16_t value);
uint16_t getOutputCompareRegisterA();
void addToOutputCompareRegisterA(uint16_t value);
void setOutputCompareRegisterB(uint16_t value);
uint16_t getOutputCompareRegisterB();
void waitForCompareRegisterAMatch();
static void startTimer() __attribute__((always_inline));
static void setValue(uint16_t value) __attribute__((always_inline));
static void disableTimerPin() __attribute__((always_inline));
static void forceClearPinOnMatch() __attribute__((always_inline));
static void setOnCompareMatch() __attribute__((always_inline));
static void toggleOnCompareMatch() __attribute__((always_inline));
static void enableInputCapture() __attribute__((always_inline));
static void disableInputCapture() __attribute__((always_inline));
static void clearInputCaptureFlag() __attribute__((always_inline));
static void clearInputCaptureAndOutputCompareBFlag() __attribute__((always_inline));
static uint16_t getInputCaptureTime() __attribute__((always_inline));
static void enableInputCaptureAndOutputCompareBInterrupt() __attribute__((always_inline));
static void disableInputCaptureAndOutputCompareBInterrupt() __attribute__((always_inline));
static void setOutputCompareRegisterA(uint16_t value) __attribute__((always_inline));
static uint16_t getOutputCompareRegisterA() __attribute__((always_inline));
static void addToOutputCompareRegisterA(uint16_t value) __attribute__((always_inline));
static void setOutputCompareRegisterB(uint16_t value) __attribute__((always_inline));
static uint16_t getOutputCompareRegisterB() __attribute__((always_inline));
static void waitForCompareRegisterAMatch() __attribute__((always_inline));
};

inline void LaserTimer::startTimer()
@@ -117,15 +117,15 @@ void do_move(float y_distance, float z_distance, float feedrate){
if(y_distance != 0){

enable_y_motor();
step(y, y_distance*Y_STEPS_PER_MM, y_speed, y_acceleration);
step(y, y_distance*Y_STEPS_PER_MM/1000, y_speed, y_acceleration);
//motors_release();
//release_y_motor();
}

if(z_distance != 0){
enable_z_motor();
//motors_enable();
step(z, z_distance*Z_STEPS_PER_MM, z_speed, z_acceleration);
step(z, z_distance*Z_STEPS_PER_MM/1000, z_speed, z_acceleration);
//release_z_motor();
//motors_release();
}
@@ -30,7 +30,7 @@ void setup(){


Serial.begin(BAUD);

init_motor_driver();

noInterrupts();
@@ -60,6 +60,7 @@

// LASER
#define LASER_PIN 9
#define LASER_ENABLE_PIN 10
#define LASER_PORT B
#define LASER_BIT 1
#define LASER_PWM_PORT D
@@ -69,10 +70,10 @@
#define LASER_EXPOSING_CYCLES 1000
#define LASER_POINT_SCALER 15

#define LASER_TIMING_SCALE_DIVIDEND 2
#define LASER_TIMING_SCALE_DIVISOR 3
#define LASER_TIMING_SCALE_DIVIDEND 1
#define LASER_TIMING_SCALE_DIVISOR 8

#define LASER_TIMING_CENTER_OFFSET 25000
#define LASER_TIMING_CENTER_OFFSET 24000

#define LASER_POSITIONS_BUFFER_SIZE 255

@@ -37,6 +37,7 @@ uint8_t TIMSK1 = 0;
uint8_t TIFR1 = 0;



/* Timer 2 */
uint8_t OCR2 = 0;
uint8_t TCNT2 = 0;
@@ -67,7 +67,7 @@ extern uint8_t TCCR1A;
#define TOIE0 0

extern uint8_t TIMSK1;
extern uint8_t TIFR1;
extern volatile uint8_t TIFR1;


/* Timer 2 */
@@ -71,7 +71,7 @@
$scope.layers[layer] = [];
} else if(line.indexOf("G01 Y") == 0) {
// adjust Y position
var remainingString = line.substring(5);
var remainingString = line.substring(5).replace(';','');
var yDistance = Number(remainingString);
lineSpacings.unshift(yDistance);
yPosition += yDistance;
@@ -82,7 +82,7 @@
minY = yPosition;
}
xPosition = 0;
} else if(line.indexOf("G06 E") == 0) {
} else if(line.indexOf("G06") == 0) {
if(lineFinished) {
console.error("Error in line "+i+": line finished without started");
} else {
@@ -96,8 +96,11 @@
if(lineFinished) {
lineFinished = false;
}
var remainingString = line.substring(5);
var remainingString = line.substring(5).replace(';','');
var xDistance = Number(remainingString);
if(xDistance == 0) {
continue;
}
if(lineOpen) {
$scope.layers[layer].unshift({y:yPosition, x1:xPosition, x2: xPosition+xDistance});
lineOpen = false;
Oops, something went wrong.

0 comments on commit 30ed715

Please sign in to comment.