Skip to content
Snippets Groups Projects
Commit 25b9d124 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Added custom printf and scanf functions to avoid unnecessary text send via the...

Added custom printf and scanf functions to avoid unnecessary text send via the serial port in the real robot.
It also helps redirect the stdin and stdout streams to a virtual serial port in simulation.
Added a macro definition __REAL__ in all Makefile's
Added a new example of communication throw a vurtual serial port and visualization of the robot camera outside ROS.
parent 6be031df
No related branches found
No related tags found
No related merge requests found
Showing
with 139 additions and 53 deletions
......@@ -15,7 +15,7 @@ CC=avr-gcc
OBJCOPY=avr-ar
MMCU=atmega2561
CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
ARFLAGS=rsc
......
......@@ -9,7 +9,9 @@ extern "C" {
#include <avr/interrupt.h>
void serial_console_init(uint32_t baudrate);
int printf(const char *fmt, ...);
uint8_t serial_console_get_num_data(void);
int cm510_printf(const char *fmt, ...);
int cm510_scanf(const char *fmt, ... );
#ifdef __cplusplus
}
......
......@@ -14,7 +14,7 @@ MMCU=atmega2561
LIBS=$(COMM_DIR)libcomm.a
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -19,12 +19,12 @@ int16_t main(void)
dyn_master_scan(&num,ids);
if(num==1)
{
printf("Found one device\n");
cm510_printf("Found one device\n");
PORTC=0x3F;
}
else
{
printf("No device found\n");
cm510_printf("No device found\n");
PORTC=0x7F;
}
}
......
......@@ -140,8 +140,16 @@ void serial_console_init(uint32_t baudrate)
device=fdevopen(serial_console_putchar,serial_console_getchar);
}
int printf(const char *fmt, ...)
uint8_t serial_console_get_num_data(void)
{
return serial_console_rx_num_data;
}
int cm510_printf(const char *fmt, ...)
{
#ifdef __REAL__
return 0;
#else
va_list ap;
int i;
......@@ -149,4 +157,17 @@ int printf(const char *fmt, ...)
i = vfprintf(stdout, fmt, ap);
va_end(ap);
return i;
#endif
}
int cm510_scanf(const char *fmt, ...)
{
va_list arg;
int done;
va_start (arg, fmt);
done = vfscanf (stdin, fmt, arg);
va_end (arg);
return done;
}
......@@ -18,7 +18,7 @@ INC_DIRS=-I$(CONT_DIR)include/ -I$(COMM_DIR)include -I$(DEV_DIR)include -I$(MAN_
LIBS=$(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a $(MAN_DIR)lib/libmotion_manager.a
CFLAGS=-mmcu=$(MMCU) -Wall -O3 -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -O3 -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
ARFLAGS=rsc
......
......@@ -18,7 +18,7 @@ INCLUDE_DIRS=-I$(CONT_DIR)include/ -I$(COMM_DIR)include -I$(MAN_DIR)include -I$(
LIBS=$(CONT_DIR)lib/libcontrollers.a $(MAN_DIR)lib/libmotion_manager.a $(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -29,8 +29,8 @@ void user_loop(void)
{
if(user_time_is_period_done())
{
printf("Gyro X: %d\n",get_adc_channel(ADC_PORT_3));
printf("Gyro Y: %d\n",get_adc_channel(ADC_PORT_4));
cm510_printf("Gyro X: %d\n",get_adc_channel(ADC_PORT_3));
cm510_printf("Gyro Y: %d\n",get_adc_channel(ADC_PORT_4));
}
if(is_button_pressed(BTN_START))
turn_led_on(LED_AUX);
......
......@@ -14,7 +14,7 @@ INC_DIRS=-I./include/ -I$(COMM_DIR)/include -I$(CONT_DIR)/include
LIBS=$(COMM_DIR)/lib/libcomm.a
CFLAGS=-mmcu=$(MMCU) -Wall -O3 -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -O3 -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
ARFLAGS=rsc
......
......@@ -19,7 +19,7 @@ LIBS=$(COMM_DIR)lib/libcomm.a $(DEV_DIR)lib/libdyn_devices.a
INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -32,41 +32,41 @@ int main(void)
_delay_ms(4000);
if(exp_board_init(0xC0)==0x00)
{
printf("expansion baord found\n");
cm510_printf("expansion baord found\n");
/* GPIO test */
/* if(exp_gpio_config(GPIO0,GPIO_OUT))
printf("Error configuring the GPIO0 pin\n");
cm510_printf("Error configuring the GPIO0 pin\n");
if(exp_gpio_config(GPIO1,GPIO_IN))
printf("Error configuring the GPIO1 pin\n");
cm510_printf("Error configuring the GPIO1 pin\n");
for(i=0;i<10;i++)
{
if(exp_gpio_set_value(0,1))
printf("Error Error setting the value of the GPIO0 pin\n");
cm510_printf("Error Error setting the value of the GPIO0 pin\n");
if(exp_gpio_get_value(GPIO1)==0x01)
printf("GPIO pin is HIGH\n");
cm510_printf("GPIO pin is HIGH\n");
else if(exp_gpio_get_value(GPIO1)==0x00)
printf("GPIO pin is LOW\n");
cm510_printf("GPIO pin is LOW\n");
else
printf("Error getting the GPIO1 value\n");
cm510_printf("Error getting the GPIO1 value\n");
_delay_ms(1000);
if(exp_gpio_set_value(0,0))
printf("Error Error setting the value of the GPIO1 pin\n");
cm510_printf("Error Error setting the value of the GPIO1 pin\n");
if(exp_gpio_get_value(GPIO1)==0x00)
printf("GPIO pin is LOW\n");
cm510_printf("GPIO pin is LOW\n");
else if(exp_gpio_get_value(GPIO1)==0x01)
printf("GPIO pin is HIGH\n");
cm510_printf("GPIO pin is HIGH\n");
else
printf("Error getting the GPIO1 value\n");
cm510_printf("Error getting the GPIO1 value\n");
_delay_ms(1000);
}*/
/* LED and switches test */
/* for(i=0;i<10;i++)
{
exp_gpio_set_led();
printf("Switches value: %d\n",exp_gpio_get_switches());
cm510_printf("Switches value: %d\n",exp_gpio_get_switches());
_delay_ms(1000);
exp_gpio_clear_led();
printf("Switches value: %d\n",exp_gpio_get_switches());
cm510_printf("Switches value: %d\n",exp_gpio_get_switches());
_delay_ms(1000);
}*/
/* PWM test */
......@@ -89,58 +89,58 @@ int main(void)
for(i=0;i<10;i++)
{
exp_dac_set_voltage(DAC0,1000);
printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
cm510_printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
_delay_ms(1000);
exp_dac_set_voltage(DAC0,2000);
printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
cm510_printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
_delay_ms(1000);
exp_dac_set_voltage(DAC0,3000);
printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
cm510_printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
_delay_ms(1000);
exp_dac_set_voltage(DAC0,4000);
printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
cm510_printf("Volatge: %d mV\n",exp_dac_get_voltage(DAC0));
_delay_ms(1000);
}
dac_stop();*/
/* compass test */
exp_compass_start();
printf("Number of samples to average: %d\n",exp_compass_get_avg_samples());
cm510_printf("Number of samples to average: %d\n",exp_compass_get_avg_samples());
for(i=0;i<100;i++)
{
printf("Current heading: %d\n",exp_compass_get_heading());
printf("Current averaged heading: %d\n",exp_compass_get_avg_heading());
cm510_printf("Current heading: %d\n",exp_compass_get_heading());
cm510_printf("Current averaged heading: %d\n",exp_compass_get_avg_heading());
_delay_ms(1000);
}
exp_compass_stop();
/* ADC test */
/* exp_adc_start();
printf("Number of samples to average: %d\n",exp_adc_get_average_samples());
printf("Sampling period: %d ms\n",exp_adc_get_sample_period());
cm510_printf("Number of samples to average: %d\n",exp_adc_get_average_samples());
cm510_printf("Sampling period: %d ms\n",exp_adc_get_sample_period());
for(i=0;i<100;i++)
{
printf("Current voltage at ADC0: %d mV\n",exp_adc_get_channel(ADC0));
printf("Current averaged voltage at ADC0: %d mV\n",exp_adc_get_avg_channel(ADC0));
cm510_printf("Current voltage at ADC0: %d mV\n",exp_adc_get_channel(ADC0));
cm510_printf("Current averaged voltage at ADC0: %d mV\n",exp_adc_get_avg_channel(ADC0));
_delay_ms(1000);
}
exp_adc_stop();*/
/* UART USB test */
/* exp_uart_usb_start();
printf("current baudrate: %d\n",exp_uart_usb_get_baudrate());
cm510_printf("current baudrate: %d\n",exp_uart_usb_get_baudrate());
for(i=0;i<10;i++)
{
while(!exp_uart_usb_is_data_available())
_delay_ms(100);
data=exp_uart_usb_receive_data();
printf("Received data: %d\n",data);
cm510_printf("Received data: %d\n",data);
exp_uart_usb_send_byte(data);
while(exp_uart_usb_is_sending())
_delay_ms(100);
printf("data sent\n");
cm510_printf("data sent\n");
}
exp_uart_usb_stop();*/
}
else
printf("expansion board not found\n");
cm510_printf("expansion board not found\n");
while(1);
}
......@@ -35,7 +35,7 @@ int main(void)
if(num>0)
{
model=get_model_number(ids[0]);
printf("%d\n",model);
cm510_printf("%d\n",model);
set_target_speed(ids[0],100);
for(;;)
{
......@@ -50,7 +50,7 @@ int main(void)
}
}
else
printf("No device found");
cm510_printf("No device found");
while(1);
}
......@@ -18,7 +18,7 @@ LIBS=$(MAN_DIR)lib/libmotion_manager.a $(CONT_DIR)lib/libcontrollers.a $(COMM_DI
INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -18,7 +18,7 @@ LIBS=$(MAN_DIR)lib/libmotion_manager.a $(CONT_DIR)lib/libcontrollers.a $(COMM_DI
INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -18,7 +18,7 @@ LIBS=$(MAN_DIR)lib/libmotion_manager.a $(CONT_DIR)lib/libcontrollers.a $(COMM_DI
INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include -I../movements
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -35,7 +35,7 @@ void user_loop(void)
}
else
state=wait_start;
printf("wait_start %d\n",(int)state);
cm510_printf("wait_start %d\n",(int)state);
break;
case wait_cmd: if(is_button_rising_edge(BTN_LEFT))
{
......
......@@ -18,7 +18,7 @@ LIBS=$(CONT_DIR)lib/libcontrollers.a $(MAN_DIR)lib/libmotion_manager.a $(DEV_DIR
INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
......@@ -21,7 +21,7 @@ void user_loop(void)
{
static main_states state=wait_start;
/* switch(state)
switch(state)
{
case wait_start: if(is_button_rising_edge(BTN_START))
{
......@@ -37,9 +37,9 @@ void user_loop(void)
else
state=read_sensors;
break;
case read_sensors: printf("CM510 ADC port 1: %d\n",get_adc_avg_channel(ADC_PORT_2));
printf("Exp. Board compass: %d\n",exp_compass_get_avg_heading());
printf("Exp. Board ADC port 7: %d\n",exp_adc_get_avg_channel(ADC7));
case read_sensors: cm510_printf("CM510 ADC port 1: %d\n",get_adc_avg_channel(ADC_PORT_2));
cm510_printf("Exp. Board compass: %d\n",exp_compass_get_avg_heading());
cm510_printf("Exp. Board ADC port 7: %d\n",exp_adc_get_avg_channel(ADC7));
break;
}*/
}
}
......@@ -18,7 +18,7 @@ LIBS=$(MAN_DIR)lib/libmotion_manager.a $(CONT_DIR)lib/libcontrollers.a $(COMM_DI
INCLUDE_DIRS=-I$(DEV_DIR)include -I$(COMM_DIR)include -I$(CONT_DIR)include -I$(MAN_DIR)include -I../movements
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
CFLAGS=-mmcu=$(MMCU) -Wall -Os $(defines) -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(PROJECT).map -DF_CPU=16000000UL
......
# computer code
INPUT_FILE = vision.cpp
OUTPUT_FILE = vision
LIBS = -lpthread -L/usr/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -L/usr/local/lib/iridrivers -ldetectqrcode -lcomm -liriutils
INCLUDES = -I. -I/usr/include/opencv -I/usr/local/include/iridrivers
# microcontroller code
CM510_PROJECT = cm510_vision
CM510_SOURCES = cm510_vision.c
CM510_OBJS=$(CM510_SOURCES:.c=.o)
CM510_SRC_DIR=./
CM510_DEV_DIR=../../dyn_devices/
CM510_COMM_DIR=../../communications/
CM510_CONT_DIR=../../controllers/
CM510_MAN_DIR=../../motion/
CC=avr-gcc
OBJCOPY=avr-objcopy
MMCU=atmega2561
CM510_LIBS=$(CM510_MAN_DIR)lib/libmotion_manager.a $(CM510_CONT_DIR)lib/libcontrollers.a $(CM510_COMM_DIR)lib/libcomm.a $(CM510_DEV_DIR)lib/libdyn_devices.a
CM510_INCLUDE_DIRS=-I$(CM510_DEV_DIR)include -I$(CM510_COMM_DIR)include -I$(CM510_CONT_DIR)include -I$(CM510_MAN_DIR)include -I../movements
CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=16000000UL -D__REAL__ -gdwarf-2 -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes
LDFLAGS=-mmcu=$(MMCU) -Wl,-Map=$(CM510_PROJECT).map -DF_CPU=16000000UL
HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature
all: communications dyn_devices controllers motion_manager $(CM510_PROJECT).hex
$(CXX) $(INPUT_FILE) $(LIBS) $(INCLUDES) -o $(OUTPUT_FILE)
$(CM510_PROJECT).hex: $(CM510_PROJECT).elf
$(OBJCOPY) -O ihex $(HEX_FLASH_FLAGS) $< $@
$(CM510_PROJECT).elf: $(CM510_OBJS)
$(CC) $(LDFLAGS) $(CM510_OBJS) $(CM510_LIBS) -o $(CM510_PROJECT).elf
%.o:%.c
$(CC) -c $(CFLAGS) $(CM510_INCLUDE_DIRS) -o $@ $<
communications:
$(MAKE) -C $(CM510_COMM_DIR)
dyn_devices:
$(MAKE) -C $(CM510_DEV_DIR)
controllers:
$(MAKE) -C $(CM510_CONT_DIR)
motion_manager:
$(MAKE) -C $(CM510_MAN_DIR)
download: $(MAIN_OUT_HEX)
fw_downloader -d /dev/ttyUSB0 -f ./$(CM510_PROJECT).hex -p cm510
clean:
-rm $(CM510_PROJECT).map
-rm $(CM510_PROJECT).hex
-rm $(CM510_PROJECT).elf
-rm $(CM510_OBJS)
$(RM) $(OUTPUT_FILE)
.PHONY: all clean
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment