00001 /*************************************************************************** 00002 *** Fraunhofer IPA 00003 *** Robotersysteme 00004 *** Projekt: 3D cartography demonstrator 00005 **************************************************************************** 00006 **************************************************************************** 00007 *** Autor: Julio Sagardoy (goa-js) 00008 ***************************************************************************/ 00009 00017 00018 #include <avr/io.h> 00019 #include <avr/interrupt.h> 00020 #include <util/delay.h> 00021 00023 #include <stdbool.h> 00024 #include <stdlib.h> 00025 #include <string.h> 00026 00028 #include <usart.h> 00029 #include <led.h> 00030 #include <stepper.h> 00031 #include <encoder.h> 00032 #include <servo.h> 00033 #include <scheduler.h> 00034 #include <publisher.h> 00035 00038 void pan_command() 00039 { 00040 int16_t p_pos; // target position 00041 uint16_t p_lat; // inter-step latency 00042 int16_t steps; // signed integer containing the number of steps to jump 00043 char* message; // will contain the entire message from uart 00044 char* comma_ptr; 00045 char* token[2]; 00046 00047 message = malloc(32*sizeof(char)); // allocate space for uart message 00048 00049 uart_gets( message ); // ref receive string (null terminated already) 00050 00051 comma_ptr = strchr(message, ','); // locate comma position 00052 *comma_ptr = '\0'; // replace comma by null, so substring is created 00053 00054 token[0] = message; // token[0] is ptr to first substring -position 00055 token[1] = comma_ptr+1; // comma_ptr+1 is ptr to second substring -latency 00056 00057 p_pos = atoi(token[0]); 00058 p_lat = atoi(token[1]); 00059 00060 free(message); 00061 00063 steps = p_pos - stepper_read(); 00064 00066 if (steps > 0) 00067 stepper_set_direction(1); 00068 else 00069 stepper_set_direction(0); 00070 00071 stepper_set_rem_steps( abs(steps) ); 00072 stepper_set_latency(p_lat); 00073 00074 /*for(int16_t i=0; i<steps; i++) 00075 { 00076 stepper_advance(); 00077 _delay_ms(p_lat); 00078 }*/ 00079 } 00080 00083 void tilt_command() 00084 { 00085 int8_t t_pos; // target position 00086 int16_t t_lat=1; // inter-step latency 00087 char* message; // will contain the entire message from uart 00088 char* comma_ptr; 00089 char* token[2]; 00090 00091 message = malloc(32*sizeof(int8_t)); // allocate space for uart message 00092 00093 uart_gets( message ); // ref receive string (null terminated already) 00094 00095 comma_ptr = strchr(message, ','); // locate comma position 00096 *comma_ptr = '\0'; // replace comma by null, so substring is created 00097 00098 token[0] = message; // token[0] is ptr to first substring -position 00099 token[1] = comma_ptr+1; // comma_ptr+1 is ptr to second substring -latency 00100 00101 t_pos = (int8_t)atoi(token[0]); // token[0] is ptr to first substring -position 00102 t_lat = atoi(token[1]); // comma_ptr+1 is ptr to second substring -latency 00103 00104 free(message); 00105 00106 int8_t old_pos = servo_read(); // Get current servo value 00107 00108 if (t_pos > old_pos) // Compare with target position, to see direction of rotation 00109 servo_set_direction(1); 00110 else 00111 servo_set_direction(0); 00112 00113 uint8_t steps = abs(t_pos - old_pos); 00114 00115 servo_set_rem_steps( steps ); 00116 servo_set_latency( t_lat ); 00117 //servo_set_val( t_pos, t_lat ); 00118 } 00119 00120 void StopMovement() 00121 { 00122 stepper_set_rem_steps(0); 00123 servo_set_rem_steps(0); 00124 } 00125 00128 int main() 00129 { 00130 char keychar; 00131 00132 // Initialisation routines 00133 cli(); //Disable global interrupt flag 00134 led_init(); 00135 uart_init(); 00136 servo_init(); 00137 encoder_init(); 00138 stepper_init(); 00139 scheduler_init(); 00140 sei(); //Enable global interrupt flag 00141 00142 led_red_on(); // red led is always lit 00143 servo_set_neutral(); // set servo to neutral position 00144 00145 while(1) 00146 { 00147 if ( uart_DataInReceiveBuffer() ) // poll for available data in rx buffer 00148 { 00149 keychar = uart_getc(); // read a single character, check if its a key character 00150 00151 switch( keychar ) 00152 { 00153 case 'P': 00154 led_grn_on(); 00155 pan_command(); 00156 led_grn_off(); 00157 break; 00158 00159 case 'T': 00160 led_grn_on(); 00161 tilt_command(); 00162 led_grn_off(); 00163 break; 00164 00165 case 'R': 00166 StopMovement(); 00167 if( uart_getc() == '1' ) 00168 scheduler_Set_sl1( 1 ); 00169 else 00170 scheduler_Set_sl1( 0 ); 00171 break; 00172 00173 case 'E': 00174 StopMovement(); 00175 uart_bufferFlush(2); 00176 uart_puts("E"); 00177 break; 00178 00179 case 'B': 00180 uart_bufferFlush(1); 00181 break; 00182 00183 case 'r': 00184 led_grn_on(); 00185 publishData(); 00186 led_grn_off(); 00187 break; 00188 00189 case 'L': 00190 led_grn_on(); 00191 StopMovement(); 00192 uart_bufferFlush(2); 00193 scheduler_Set_sl1( 0 ); 00194 servo_set_neutral(); // set servo to neutral, reducing torque moment when calibrating 00195 _delay_ms(500); // wait for servo to settle 00196 stepper_cal(); // begins calibration 00197 led_grn_off(); 00198 break; 00199 00200 case 'C': 00201 StopMovement(); 00202 scheduler_Set_sl1( 0 ); 00203 uart_loopback(); // loopback test until 'q' is received 00204 break; 00205 00206 case 'N': 00207 StopMovement(); 00208 scheduler_Set_sl1( 0 ); 00209 uart_puts( "COB3DMD" ); // retrieve robot name 00210 break; 00211 } 00212 } 00213 } 00214 }