/*
	Function name : stepper_flow
  	return : void
  	parameters : unsigned char direction_flow :- to determine the direction of motion
  	Method of operation : it is used to move the stepper motor in a very big nummber of steps that it seems it moves for infinityyyyy, but it doesn't,
  						  best used for moving the stepper motor till a physical event happens to stop it
*/
void ext_stepper_3d::stepper_flow (unsigned char direction_flow)
{
    if (direction_flow == clockwise )
    {
        stepper_move (2147483647, 164933 );	//move max number of steps in a direction
    }
    else if (direction_flow == anticlockwise )
    {
        stepper_move (-2147483647, 164933 );	//move max number of steps oin the other direction
    }
    status_var = FLOW;

}
Exemplo n.º 2
0
void gcode_move(double x, double y, bool down, bool rel) {
    if (null_mode) return;

    if (rel) {
        xpos += x;
        ypos += y;
        x = xpos;
        y = ypos;
    } else {
        xpos = x;
        ypos = y;
    }
    printf("move to %f,%f\n", x, y);

    x += xoff;
    y += yoff;

    x = x*DPI_X;
    y = y*DPI_Y;

    blink();
    if (!down) {
        stepper_move(x, y);
    } else {
        stepper_draw(x, y);
    }
}
Exemplo n.º 3
0
static int control_request(usbd_device *usbd_dev, struct usb_setup_data *req, u8 **buf,
        u16 *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
{
    (void)complete;
    (void)buf;
    (void)usbd_dev;
    (void)len;

    PCBUSBLOG("Got request type: bmRequestType: 0x%x bRequest: 0x%x len: 0x%x\n", req->bmRequestType, req->bRequest, *len);
    
    if((req->bmRequestType & 0x60) == 0x40) { // Vendor request
        if(req->bRequest == REQ_SET_SPEED) {
            if(!IS_HOST_TO_DEVICE(req->bmRequestType) || *len != 2)
                return USBD_REQ_NOTSUPP;
            
            int des_speed = (*buf)[0] + ((*buf)[1] << 8);
            PCBUSBLOG("Set speed: %d\n", des_speed);
            set_speed(des_speed);
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_ENABLE_DEBUG_OUT) {
            if(!IS_HOST_TO_DEVICE(req->bmRequestType) || *len != 1)
                return USBD_REQ_NOTSUPP;
            
            if((*buf)[0])
                enable_debug_out(1);
            else
                enable_debug_out(0);
            
            PCBUSBLOG("Set debug: %d\n", (*buf)[0]);

            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_SET_PERSISTENT_FLASH) {
            if (!IS_HOST_TO_DEVICE(req->bmRequestType) || *len != 0) {
                return USBD_REQ_NOTSUPP;
            }

            if (req->wIndex > 4096) {
                return USBD_REQ_NOTSUPP;
            }

            PCBUSBLOG("Store to flash: 0x%x at index: 0x%x\n", req->wValue, req->wIndex);
            pcb_flash_store(req->wIndex, req->wValue);

            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_GET_PERSISTENT_FLASH) {
            if (!IS_DEVICE_TO_HOST(req->bmRequestType) || *len <= 0) {
                return USBD_REQ_NOTSUPP;
            }

            int index = req->wIndex;
            int length = req->wValue;

            PCBUSBLOG("Get from flash: index: 0x%x len: 0x%x\n", index, length);

            if (length > DATA_LEN) {
                return USBD_REQ_NOTSUPP;
            }

            for (int i = 0; i < length; i++) {
                data[i] = pcb_flash_restore(index + i);
            }

            *buf = data;
            *len = length;

            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_GET_STEPPER_STATUS) {
            if(!IS_DEVICE_TO_HOST(req->bmRequestType)) {
                return USBD_REQ_NOTSUPP;
            }
            
            /* Sanity check */
            if(DATA_LEN < 4)
                return USBD_REQ_NOTSUPP;
            
            data[0] = 0;
            if(!stepper_idle()) data[0] |= 0x01;
            if(stepper_is_homed) data[0] |= 0x02;
            
            data[1] = 0;
            data[2] = (stepper_current_pos & 0x00FF);
            data[3] = (stepper_current_pos & 0xFF00) >> 8;
            
            *buf = data;
            *len = 4;
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_HOME_STEPPER) {
            stepper_home();
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_MOVE_STEPPER) {
            int16_t delta_pos = (int16_t) req->wValue;
            
            if(req->wIndex)
                stepper_move(delta_pos);
            else
                stepper_move_to(delta_pos);
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_STEPPER_OFF) {
            stepper_off();
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_SET_N_SCANS) {
            max_n_scans = req->wValue;
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_SET_AUTOSTEP) {
            if(req->wValue)
                autostep = 1;
            else
                autostep = 0;
            
            return USBD_REQ_HANDLED;
        } else if(req->bRequest == REQ_CAN_SEND) {
            /* Primitive flow control (FIXME) */
            
            if(!IS_DEVICE_TO_HOST(req->bmRequestType)) {
                return USBD_REQ_NOTSUPP;
            }
            
            data[0] = (dma_write_idx < K_IMAGE_WIDTH);
            *buf = data;
            *len = 4;
            
            return USBD_REQ_HANDLED;
        }
    }
Exemplo n.º 4
0
void get_command() {
    while (1) {
        int foo = usb_peek();
        if (foo < 0 || buflen >= BUFSIZE) {
            break;
        }
        serial_char = foo;

        if (serial_char == '\n' ||
                serial_char == '\r' ||
                (serial_char == ':' && comment_mode == false) ||
                serial_count >= (MAX_CMD_SIZE - 1)) {
            if (!serial_count) { //if empty line
                comment_mode = false; //for new command
                return;
            }
            cmdbuffer[bufindw][serial_count] = 0; //terminate string
            if (!comment_mode) {
                comment_mode = false; //for new command





                if (strstr(cmdbuffer[bufindw], "N") != NULL) {
                    strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
                    gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
                    if (gcode_N != gcode_LastN + 1 && (strstr(cmdbuffer[bufindw], "M110") == NULL)) {
                        printf("Error: Line Number is not Last Line Number+1, Last Line:%lu\n", gcode_LastN);
                        //Serial.println(gcode_N);
                        FlushSerialRequestResend();
                        serial_count = 0;
                        return;
                    }

                    if (strstr(cmdbuffer[bufindw], "*") != NULL) {
                        unsigned char checksum = 0;
                        unsigned char count = 0;
                        while (cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
                        strchr_pointer = strchr(cmdbuffer[bufindw], '*');

                        if ((int) (strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
                            printf("Error:checksum mismatch, Last Line:%lu\n", gcode_LastN);
                            FlushSerialRequestResend();
                            serial_count = 0;
                            return;
                            //if no errors, continue parsing
                        }
                    } else {
                        printf("Error:No Checksum with line number, Last Line:%lu\n", gcode_LastN);
                        FlushSerialRequestResend();
                        serial_count = 0;
                        return;
                    }

                    gcode_LastN = gcode_N;
                    //if no errors, continue parsing
                } else // if we don't receive 'N' but still see '*'
                {
                    if ((strstr(cmdbuffer[bufindw], "*") != NULL)) {
                        printf("Error:No Line Number with checksum, Last Line:%lu\n", gcode_LastN);
                        serial_count = 0;
                        return;
                    }
                }
                if ((strstr(cmdbuffer[bufindw], "G") != NULL)) {
                    strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
                    switch ((int) ((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))) {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                            printf("ok\n");
                            break;
                        default:
                            break;
                    }

                }
                bufindw = (bufindw + 1) % BUFSIZE;
                buflen += 1;
            }
            serial_count = 0; //clear buffer
        } else {
            bool skipmode = false;
            if (serial_char == ';') {

                // IN;SP1;PU698,727;PD453,632;PD236,779;PD251,517;PD44,356;PD298,289;PD387,42;PD529,263;PD791,272;PD625,475;PD698,727;PU;

                if (serial_count == 2 &&
                        cmdbuffer[bufindw][0] == 'I' &&
                        cmdbuffer[bufindw][1] == 'N') {
					hgpl_ack();
                    skipmode = true;
                }
                else if (serial_count == 3 &&
                        cmdbuffer[bufindw][0] == 'S' &&
                        cmdbuffer[bufindw][1] == 'P' &&
                        cmdbuffer[bufindw][2] == '1') {
					hgpl_ack();
                    skipmode = true;
                }
                else if (serial_count >= 2 &&
                        cmdbuffer[bufindw][0] == 'P' &&
                        (cmdbuffer[bufindw][1] == 'U' ||
                        cmdbuffer[bufindw][1] == 'D')) {
                    if (serial_count > 2) {
                        cmdbuffer[bufindw][serial_count] = '\0';
                        strchr_pointer = strchr(cmdbuffer[bufindw], ',');
                        if (strchr_pointer != NULL) {
                            int y = atoi(strchr_pointer + 1);
                            *strchr_pointer = '\0';
                            int x = atoi(&cmdbuffer[bufindw][2]);
                            if (cmdbuffer[bufindw][1] == 'U') {
                                stepper_move(x, y);
                            } else {
                                stepper_draw(x, y);
                            }
							hgpl_ack();
                        }
                    } else {
                        if (cmdbuffer[bufindw][1] == 'U') {
                            stepper_move(0, 0);
                            beeper_on(1360);
                            msleep(30);
                            beeper_off();
							hgpl_ack();
                        }
                    }
                    skipmode = true;
                } else {
                    comment_mode = true;
                }
            }

            if (skipmode) {
                serial_count = 0;
                skipmode = false;
            } else {
                if (!comment_mode) {
                    cmdbuffer[bufindw][serial_count++] = serial_char;
                }
            }
        }
    }

}
Exemplo n.º 5
0
void process_commands() {
    unsigned long codenum; //throw away variable
    //char *starpos = NULL;

    if (code_seen('G')) {
        switch ((int) code_value()) {
            case 0: // G0 -> G1
            case 1: // G1
                if (code_seen('Z')) {
                    codenum = code_value();
                    penup = codenum > 0;
                }

                if (relative_mode) {
                    double newx = 0;
                    double newy = 0;
                    if (code_seen('X')) {
                        newx = code_value();
                    }
                    if (code_seen('Y')) {
                        newy = code_value();
                    }
                    gcode_move(newx, newy, !penup, true);
                } else {
                    double newx = xpos;
                    double newy = ypos;
                    if (code_seen('X')) {
                        newx = code_value();
                    }
                    if (code_seen('Y')) {
                        newy = code_value();
                    }
                    gcode_move(newx, newy, !penup, false);
                }
                break;

            case 4: // G4 dwell
                codenum = 0;
                if (code_seen('P')) codenum = code_value(); // milliseconds to wait
                if (code_seen('S')) codenum = code_value() * 1000; // seconds to wait
                codenum = codenum / 10;
                while (codenum-- > 0) {
                    wdt_reset();
                    msleep(10);
                }
                break;

            case 28: //G28 Home all Axis one at a time
                stepper_move(0, 0);
                xoff = 0;
                yoff = 0;
                break;

            case 90: // G90
                relative_mode = false;
                break;

            case 91: // G91
                relative_mode = true;
                break;

            case 92: // G92 set coords
                if (code_seen('X')) {
                    xoff = -xpos + code_value();
                    xpos = 0;
                } else {
                    xoff = -xpos;
                    xpos = 0;
                }
                if (code_seen('Y')) {
                    yoff = -ypos + code_value();
                    ypos = 0;
                } else {
                    yoff = -ypos;
                    ypos = 0;
                }
                break;
        }
    } else
        if (code_seen('M')) {
        switch ((int) code_value()) {
            case 80://power on
                break;

            case 106://load paper
                stepper_load_paper();
                break;

            case 107://unload paper
                stepper_unload_paper();
                break;

            case 300://S30=down S50=up S255=off
                if (code_seen('S')) {
                    penup = code_value() > 40;
                }
                break;
        }
    }//M
    else {
        printf("Error:unknown commando\n");
    }

}
Exemplo n.º 6
0
static ssize_t stepper_write_move(struct kobj_t * kobj, void * buf, size_t size)
{
	struct stepper_t * m = (struct stepper_t *)kobj->priv;
	stepper_move(m, strtol(buf, NULL, 0), 0);
	return size;
}
Exemplo n.º 7
0
void cli_poll(void)
{
	STEPPER_COORD dstx, dsty;
	char c;
	int8_t cmd;
	uint8_t labelchar;
	
	
	while((c = (char)usb_getc())!=SERIAL_NO_DATA) 
	{
		switch(Lang)
		{
			case HPGL:
			cmd = hpgl_char(c, &dstx, &dsty, &labelchar);
			break;
			
			case G_CODE:
			cmd = gcode_char(c, &dstx, &dsty );
			break;
			
			//case GPGL:
			// TODO
			//break;
			
			default:
			continue;		// just consume everything and do nothing
			
		}
			

		switch(cmd) 
		{
			case CMD_PU:
			//sprintf(s,"PU: %d %d",dstx,dsty);
			//display_puts( s);
				if (dstx >= 0 && dsty >= 0)	// filter out illegal moves 
					stepper_move( dstx, dsty );
			break;
			case CMD_PD:
			//sprintf(s,"PD: %d %d",dstx,dsty);
			//	display_puts( s);
				if (dstx >= 0 && dsty >= 0)	// filter out illegal moves 
					stepper_draw( dstx, dsty );
			break;
		
			case CMD_INIT:
				// 1. home
				// 2. init scale etc
				// typically happens at start and end of each document;
				dstx = dsty = 0;				
		//	sprintf(s,"IN: %d %d",dstx,dsty);
			//display_puts( s);
				stepper_move( dstx, dsty );
				break;
			case CMD_SEEK0:
			//sprintf(s,"SK: %d %d",dstx,dsty);
			//display_puts( s);
				stepper_move( dstx, dsty  );
			break;
#ifdef non_supported_commands			
			case CMD_PA:  // not supported
			break;
			case CMD_ARCABS:  // not supported
				//arc_active = arc_init();
			break;
			case CMD_LB0:  // not supported
			//	text_beginlabel();
			break;
			case CMD_LB:  // not supported
			//	if (labelchar != 0) {
			//		char_active = text_char(labelchar, &dstx, &dsty, &penny);
			//	}
		
			break;
			case CMD_SI: // not supported
				//text_scale_cm(numpad[0], numpad[1]);
			break;
			case CMD_SR: // not supported
				//text_scale_rel(numpad[0], numpad[1]);
			break;
			case CMD_DI: // not supported
				//text_direction(numpad[0], numpad[1]);
			break;
			//case CMD_FS:
			//	stepper_pressure(numpad[0]*100);
			//	break;
			case CMD_VS:
			//sprintf(s,"VS: %d",(int)numpad[0]);
			//display_puts( s);
			//	stepper_speed(numpad[0]);
			break;
			case CMD_AS: // acceleration not supported
				//set_acceleration_mode(numpad[0]);
			break;
#endif
			default:
			break;
		}
	}
	
}
Exemplo n.º 8
0
void processGCode(const cmd_param param)
{
    switch ((int)param.g)
    {
        case 0: // G0 = Rapid move
            // Handle G0 as G1 (so no break here)
        case 1: // G1 = Controlled move

            if (param.f_set && (param.f != 0.0) )
            {
                stepper_set_feedrate(AXIS_X, param.f);
                stepper_set_feedrate(AXIS_Y, param.f);
                stepper_set_feedrate(AXIS_Z, param.f);
                stepper_set_feedrate(AXIS_E, param.f);
            }

            if ( param.x_set || param.y_set || param.z_set || param.e_set )
            {
                float pos[AXIS_NUM] = {param.x, param.y, param.z, param.e};
                bool set[AXIS_NUM] = {param.x_set, param.y_set, param.z_set, param.e_set};
                stepper_move(pos, set);
            }
            else
            {
                puts("ok\n");
            }
            break;
        case 4: // G4 = Dwell
            puts("ok\n");
            break;
        case 10: // G10 = Head Offset
            puts("ok\n");
            break;
        case 20: // G20 = Set Units to Inches
            /// \todo to handle
            puts("!!\n"); // Send Command Fail to block the system
            break;
        case 21: // G21 = Set Units to Millimeters
            /// \todo to handle
            puts("ok\n");
            break;
        case 28: // G28 = Move to Origin
            stepper_reset(); /// \todo change to real move to origin
            puts("ok\n");
            break;
        case 29:
        case 30:
        case 31:
        case 32: // G29-32 = Bed probing
            puts("ok\n");
            break;
        case 90: // G90 = Set to Absolute Positioning
            stepper_set_absolute(AXIS_X);
            stepper_set_absolute(AXIS_Y);
            stepper_set_absolute(AXIS_Z);
            puts("ok\n");
            break;
        case 91: // G91 = Set to Relative Positioning
            stepper_set_relative(AXIS_X);
            stepper_set_relative(AXIS_Y);
            stepper_set_relative(AXIS_Z);
            puts("ok\n");
            break;
        case 92: // G92 = Set Position
            if (param.x_set)
            {
                stepper_set_position(AXIS_X, param.x);
            }
            if (param.y_set)
            {
                stepper_set_position(AXIS_Y, param.y);
            }
            if (param.z_set)
            {
                stepper_set_position(AXIS_Z, param.z);
            }
            if (param.e_set)
            {
                stepper_set_position(AXIS_E, param.e);
            }

            puts("ok\n");
            break;
        default:
            puts("ok\n");
            break;
    }
}