From: Michael Wiebusch Date: Thu, 11 Dec 2014 15:54:26 +0000 (+0100) Subject: cleaned up a lot, but program does not work anymore ... X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=1edd950af94af767f22f8c8245ebefcd553209ec;p=coral.git cleaned up a lot, but program does not work anymore ... --- diff --git a/firmware/USBtoSerial.c b/firmware/USBtoSerial.c index 685511f..b290168 100644 --- a/firmware/USBtoSerial.c +++ b/firmware/USBtoSerial.c @@ -39,13 +39,8 @@ #include #include "USBtoSerial.h" #include -#include "TM1001A.h" -// #include "rfm70.c" -#include "pins.h" -#include "leds.c" -int16_t plate_pos_x = 0,plate_pos_y = 0; -char stringbuffer[16]; + /** Circular buffer to hold data from the host before it is sent to the device via the serial port. */ static RingBuffer_t USBtoUSART_Buffer; @@ -161,6 +156,11 @@ void SetupHardware(void) /* Hardware Initialization */ // LEDs_Init(); USB_Init(); + + RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); + RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); + + GlobalInterruptEnable(); } /** Event handler for the library USB Connection event. */ @@ -269,445 +269,17 @@ void uart_puts(const char *s ) }/* uart_puts */ - -// convert an unsigned integer to string -void my_uitoa(uint32_t zahl, char* string, uint8_t no_digits, char leading_char) { - int8_t i; // schleifenzähler - - string[no_digits] = '\0'; // String Terminator - for (i = (no_digits - 1); i >= 0; i--) { - if (zahl == 0 && i < (no_digits - 1)) { - string[i] = leading_char; - } else { - string[i] = (zahl % 10) + '0'; - } // Modulo rechnen, dann den ASCII-Code von '0' addieren - zahl /= 10; - } - -} - -int8_t sign(int16_t x) { - return (x > 0) - (x < 0); -} - - -void uart_print_number(uint32_t zahl, uint8_t no_digits) { - my_uitoa(abs(zahl),stringbuffer,no_digits,' '); - uart_puts(stringbuffer); -} - - -void uart_print_number_wlzeros(uint32_t zahl, uint8_t no_digits) { - my_uitoa(abs(zahl),stringbuffer,no_digits,'0'); - uart_puts(stringbuffer); -} - -void uart_print_signed_number(uint32_t zahl, uint8_t no_digits) { - my_uitoa(abs(zahl),stringbuffer,no_digits,' '); - if (sign(zahl) < 0) { - uart_putc('-'); - } else { - uart_putc('+'); - } - uart_puts(stringbuffer); - -} - - -/** Main program entry point. This routine contains the overall program flow, including initial - * setup of all components and the main program loop. - */ - -/* motor stuff */ - -uint8_t phase_pattern[4] = { 0b00001010, 0b00001001, 0b00000101, 0b00000110}; - - -void set_x(uint8_t byte) { - PORTX0 &= ~(1<>0)<>1)<>2)<>3)<>0)<>1)<>2)<>3)<= 48 && cmdbuffer[i] <= 57 ){ // is it a number? - if ( num_start == 0) { // this is the first digit in the string - num_start = i; - } - } else { // no digit! - if ( num_start != 0) { // digits have been found before - strncpy(numbuffer,cmdbuffer+num_start,i-num_start); // copy number found to - // numbuffer - numbuffer[i-num_start] = '\0'; // make sure it's always a terminated string - nums_found++; - if(nums_found == 1) { // its the predot digits - predot = atoi(numbuffer); - } else { // its the postdot digits - uint8_t postdotlen = i-num_start; - if (postdotlen < 3){ // if too small ,fill with zeros - for( uint8_t j = postdotlen; j <=2; j++) { - numbuffer[j] = '0'; - } - } - // crop the number to three post dot digits - numbuffer[3] = '\0'; - - postdot = atoi(numbuffer); - } - num_start = 0; - } - } - } - - } - - int16_t steps = 0,dest=0; - - switch (action) { - case GOTO: - uart_puts("GOTO "); - uart_putc(88+axis);// x or y - uart_putc(' '); - uart_print_signed_number(predot*num_sign,3); - uart_putc('.'); - uart_print_number_wlzeros(postdot,3); - uart_puts("\r\n"); - - dest = num_sign *( predot*24 +(postdot*10)/416); - - if (axis == X) { - steps = dest - plate_pos_x; // experimental correction! - move_plate(steps,0); - plate_pos_x += steps; - } else if (axis == Y) { - steps = dest - plate_pos_y; - move_plate(0,steps); - plate_pos_y += steps; - } - pos_report(); - - break; - case MOVEREL: - uart_puts("MOVE "); - uart_putc(88+axis);// x or y - uart_putc(' '); - uart_print_signed_number(predot*num_sign,3); - uart_putc('.'); - uart_print_number_wlzeros(postdot,3); - uart_puts("\r\n"); - - steps = num_sign *( predot*24 +(postdot*10)/416); - - if (axis == X) { - move_plate(steps,0); - plate_pos_x += steps; - } else if (axis == Y) { - move_plate(0,steps); - plate_pos_y += steps; - } - pos_report(); - break; - - case SETZERO: - plate_pos_x = 0; - plate_pos_y = 0; - pos_report(); - break; - - case POSITION: - pos_report(); - break; - - } - - - - } else { // queue command - if( cmdPos == 0 ){ - uart_puts("\r\n$ "); - } - - if( byte == 8 ){ // backspace - cmdPos--; - } else { - cmdbuffer[cmdPos++] = byte; - } - uart_putc(byte); - - - } + return RingBuffer_Remove(&USBtoUSART_Buffer); + } else { + return EMPTY; } } -int main(void) -{ - - init_motors(); -// init_leds(); -// init_sw(); - - - char dummy; - uint8_t field_val = 0; - SetupHardware(); - - RingBuffer_InitBuffer(&USBtoUSART_Buffer, USBtoUSART_Buffer_Data, sizeof(USBtoUSART_Buffer_Data)); - RingBuffer_InitBuffer(&USARTtoUSB_Buffer, USARTtoUSB_Buffer_Data, sizeof(USARTtoUSB_Buffer_Data)); - -// LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); - GlobalInterruptEnable(); - - touchpad_init(); // you need to call this to setup the I/O pin! - _delay_ms(500); - sei(); - - - - - uint16_t loopcounter=0; - -// uart_puts("you selected the relative position demo modus:\n\r"); - touchpad_set_rel_mode_100dpi();// use touchpad in relative mode -// touchpad_set_rel_mode_200dpi(); // uncomment this line if you want double resolution - int16_t x, y = 0; - int8_t dx, dy = 0; - uint8_t busy = 0, last_busy = 0; - - while (1) { - -// set_led0(sw0_state()); -// set_led1(sw1_state()); -// set_led2(sw2_state()); - - Usb2SerialTask(); -// loopcounter++; -// if(loopcounter<2000) { -// continue; -// } -// loopcounter=0; - parse_command(); // read data from virtual comport - touchpad_read(); // read data from touchpad -// if(sw0_state()){ // if left switch is active - dx = -4*delta_x();// returns the amount your finger has moved in x direction since last readout -// } else { -// dx = 0; -// } -// if(sw1_state()){ // if middle switch is active - dy = -4*delta_y();// returns the amount your finger has moved in y direction since last readout -// } else { -// dy = 0; -// } - - // increment/decrement some dummy variables with the - - plate_pos_x += dx; - plate_pos_y += dy; - - - last_busy = busy; - busy = move_plate(dx,dy); - - - if (last_busy && !(busy)){ - pos_report(); - } - - - } - // end of relative mode demo block - -// #endif - - - - -} // end of main \ No newline at end of file diff --git a/firmware/USBtoSerial.h b/firmware/USBtoSerial.h index 7a210f0..3c5bb16 100644 --- a/firmware/USBtoSerial.h +++ b/firmware/USBtoSerial.h @@ -61,6 +61,8 @@ /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */ #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3) + +#define EMPTY 0xFF00 /* Function Prototypes: */ void SetupHardware(void); @@ -71,6 +73,10 @@ void EVENT_USB_Device_ControlRequest(void); void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo); + void uart_putc(unsigned char data); + void uart_puts(const char *s ); + uint16_t uart_getc(void); + void Usb2SerialTask(void); #endif diff --git a/firmware/main.c b/firmware/main.c new file mode 100644 index 0000000..25065c1 --- /dev/null +++ b/firmware/main.c @@ -0,0 +1,245 @@ + +#include +#include +#include "USBtoSerial.h" +#include +#include "TM1001A.h" +#include "motors.h" +#include "misc.h" +#include "pins.h" +#include "leds.c" + + + +int16_t plate_pos_x = 0,plate_pos_y = 0; + + + + + +void print_steps_in_mm(int16_t steps) { + int16_t predot,postdot; + + predot = steps/24; + postdot = ((abs(steps)%24)*417)/10; + uart_print_signed_number(predot,3); + uart_putc('.'); + uart_print_number_wlzeros(postdot,3); + +} + +void pos_report(void){ + uart_puts("x_pos: "); + print_steps_in_mm(plate_pos_x); + uart_puts(" y_pos: "); + print_steps_in_mm(plate_pos_y); + uart_puts("\r"); +} + + + +typedef enum {POSITION, GOTO, MOVEREL, SETZERO} action_t; + +void parse_command(void){ + + static char cmdbuffer[32]; + static char numbuffer[16]; + static uint16_t predot = 0,postdot = 0; + static uint8_t cmdPos, curCmdLen, num_start = 0, nums_found = 0; + uint8_t axis=0; + action_t action = POSITION; + int8_t num_sign = 1; + char byte; + + /* Load the next byte from the USART transmit buffer into the USART */ + + uint16_t pop = uart_getc(); + if(!(pop == EMPTY)){ + + byte = (char) pop; + + if (byte == '\r' || byte == '\n') {// end of command, evaluate cemmand! + uart_puts("\r\n"); + cmdbuffer[cmdPos] = '\0'; // terminate new command string + curCmdLen = cmdPos; + cmdPos = 0; + + + if (cmdbuffer[0] == 'g' || cmdbuffer[0] == 'G') { // goto command + action = GOTO; + } else if ( cmdbuffer[0] == 'm' || cmdbuffer[0] == 'M') { + action = MOVEREL; + } else if ( cmdbuffer[0] == 'z' || cmdbuffer[0] == 'Z' ) { + action = SETZERO; + } else { + action = POSITION; + } + + if (cmdbuffer[1] == 'x' || cmdbuffer[1] == 'X') { + axis = X; + } else if (cmdbuffer[1] == 'y' || cmdbuffer[1] == 'Y') { + axis = Y; + } + + // if you expect coordinate, parse number! + if (action == GOTO || action == MOVEREL){ + + predot = 0; + postdot = 0; + num_sign = 1; + num_start = 0; + nums_found = 0; + + for (uint8_t i=2; i<=curCmdLen; i++) { + if ( num_start == 0 && cmdbuffer[i] == '-' ) { // if you find a minus before + // you find a digit, it's a negative number + num_sign = -1; + } + + if ( cmdbuffer[i] >= 48 && cmdbuffer[i] <= 57 ){ // is it a number? + if ( num_start == 0) { // this is the first digit in the string + num_start = i; + } + } else { // no digit! + if ( num_start != 0) { // digits have been found before + strncpy(numbuffer,cmdbuffer+num_start,i-num_start); // copy number found to + // numbuffer + numbuffer[i-num_start] = '\0'; // make sure it's always a terminated string + nums_found++; + if(nums_found == 1) { // its the predot digits + predot = atoi(numbuffer); + } else { // its the postdot digits + uint8_t postdotlen = i-num_start; + if (postdotlen < 3){ // if too small ,fill with zeros + for( uint8_t j = postdotlen; j <=2; j++) { + numbuffer[j] = '0'; + } + } + // crop the number to three post dot digits + numbuffer[3] = '\0'; + + postdot = atoi(numbuffer); + } + num_start = 0; + } + } + } + + } + + int16_t steps = 0,dest=0; + + switch (action) { + case GOTO: + uart_puts("GOTO "); + uart_putc(88+axis);// x or y + uart_putc(' '); + uart_print_signed_number(predot*num_sign,3); + uart_putc('.'); + uart_print_number_wlzeros(postdot,3); + uart_puts("\r\n"); + + dest = num_sign *( predot*24 +(postdot*10)/416); + + if (axis == X) { + steps = dest - plate_pos_x; // experimental correction! + move_plate(steps,0); + plate_pos_x += steps; + } else if (axis == Y) { + steps = dest - plate_pos_y; + move_plate(0,steps); + plate_pos_y += steps; + } + pos_report(); + + break; + case MOVEREL: + uart_puts("MOVE "); + uart_putc(88+axis);// x or y + uart_putc(' '); + uart_print_signed_number(predot*num_sign,3); + uart_putc('.'); + uart_print_number_wlzeros(postdot,3); + uart_puts("\r\n"); + + steps = num_sign *( predot*24 +(postdot*10)/416); + + if (axis == X) { + move_plate(steps,0); + plate_pos_x += steps; + } else if (axis == Y) { + move_plate(0,steps); + plate_pos_y += steps; + } + pos_report(); + break; + + case SETZERO: + plate_pos_x = 0; + plate_pos_y = 0; + pos_report(); + break; + + case POSITION: + pos_report(); + break; + + } + + + + } else { // queue command + if( cmdPos == 0 ){ + uart_puts("\r\n$ "); + } + + if( byte == 8 ){ // backspace + cmdPos--; + } else { + cmdbuffer[cmdPos++] = byte; + } + uart_putc(byte); + + + } + } +} + +int main(void) +{ + + init_motors(); + + char dummy; + uint8_t field_val = 0; + SetupHardware(); + + touchpad_init(); // you need to call this to setup the I/O pin! + _delay_ms(500); + sei(); + + touchpad_set_rel_mode_100dpi();// use touchpad in relative mode + int16_t x, y = 0; + int8_t dx, dy = 0; + uint8_t busy = 0, last_busy = 0; + + while (1) { + Usb2SerialTask(); + parse_command(); // read data from virtual comport + touchpad_read(); // read data from touchpad + dx = -4*delta_x();// returns the amount your finger has moved in x direction since last readout + dy = -4*delta_y();// returns the amount your finger has moved in y direction since last readout + + plate_pos_x += dx; + plate_pos_y += dy; + + last_busy = busy; + busy = move_plate(dx,dy); + + if (last_busy && !(busy)){ + pos_report(); + } + } + + +} // end of main \ No newline at end of file diff --git a/firmware/makefile b/firmware/makefile index 0bbdfba..3ee47e0 100644 --- a/firmware/makefile +++ b/firmware/makefile @@ -17,8 +17,8 @@ BOARD = USBKEY F_CPU = 16000000 F_USB = $(F_CPU) OPTIMIZATION = s -TARGET = USBtoSerial -SRC = $(TARGET).c Descriptors.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS) TM1001A.c +TARGET = main +SRC = $(TARGET).c Descriptors.c $(LUFA_SRC_USB) $(LUFA_SRC_USBCLASS) TM1001A.c motors.c misc.c USBtoSerial.c LUFA_PATH = ../LUFA/LUFA-130303/LUFA CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/ LD_FLAGS = diff --git a/firmware/misc.c b/firmware/misc.c new file mode 100644 index 0000000..b09a03d --- /dev/null +++ b/firmware/misc.c @@ -0,0 +1,58 @@ +#include +#include +#include "USBtoSerial.h" +#include "misc.h" + + +char stringbuffer[16]; + +// convert an unsigned integer to string +void my_uitoa(uint32_t zahl, char* string, uint8_t no_digits, char leading_char) { + int8_t i; // schleifenzähler + + string[no_digits] = '\0'; // String Terminator + for (i = (no_digits - 1); i >= 0; i--) { + if (zahl == 0 && i < (no_digits - 1)) { + string[i] = leading_char; + } else { + string[i] = (zahl % 10) + '0'; + } // Modulo rechnen, dann den ASCII-Code von '0' addieren + zahl /= 10; + } + +} + +uint32_t times_ten_pow(uint8_t exponent) { + uint32_t val = 1; + for (uint8_t i = 1; i <= exponent; i++) { + val *= 10; + } + return val; +} + +int8_t sign(int16_t x) { + return (x > 0) - (x < 0); +} + + +void uart_print_number(uint32_t zahl, uint8_t no_digits) { + my_uitoa(abs(zahl),stringbuffer,no_digits,' '); + uart_puts(stringbuffer); +} + + +void uart_print_number_wlzeros(uint32_t zahl, uint8_t no_digits) { + my_uitoa(abs(zahl),stringbuffer,no_digits,'0'); + uart_puts(stringbuffer); +} + +void uart_print_signed_number(uint32_t zahl, uint8_t no_digits) { + my_uitoa(abs(zahl),stringbuffer,no_digits,' '); + if (sign(zahl) < 0) { + uart_putc('-'); + } else { + uart_putc('+'); + } + uart_puts(stringbuffer); + +} \ No newline at end of file diff --git a/firmware/misc.h b/firmware/misc.h new file mode 100644 index 0000000..c2a9b16 --- /dev/null +++ b/firmware/misc.h @@ -0,0 +1,12 @@ + + +void my_uitoa(uint32_t zahl, char* string, uint8_t no_digits, char leading_char); +uint32_t times_ten_pow(uint8_t exponent); +int8_t sign(int16_t x); +void uart_print_number(uint32_t zahl, uint8_t no_digits); +void uart_print_number_wlzeros(uint32_t zahl, uint8_t no_digits); +void uart_print_signed_number(uint32_t zahl, uint8_t no_digits); + + + + diff --git a/firmware/motors.c b/firmware/motors.c new file mode 100644 index 0000000..2c1f134 --- /dev/null +++ b/firmware/motors.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include "motors.h" +#include "misc.h" + + + + + +/* motor stuff */ + +uint8_t phase_pattern[4] = { 0b00001010, 0b00001001, 0b00000101, 0b00000110}; + + +void set_x(uint8_t byte) { + PORTX0 &= ~(1<>0)<>1)<>2)<>3)<>0)<>1)<>2)<>3)<