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

Added configurations files for the controllers and communications modules, as...

Added configurations files for the controllers and communications modules, as well as a global configuration file.
Moved the source code of the controllers module to the root source folder.
Moved the balance code to a separate source file.
Added a user time module to allow periodic and one shot time functions to the user.
parent 5c438132
No related branches found
No related tags found
No related merge requests found
Showing
with 345 additions and 65 deletions
......@@ -20,7 +20,7 @@ ARFLAGS=rsc
.PHONY: show_banner all
all: show_banner $(PROJECT).a examples
all: show_banner $(PROJECT).a
show_banner:
@echo "------------------------------------------------------";
......
#ifndef _COMM_CFG_H
#define _COMM_CFG_H
#ifndef DYN_MASTER_MAX_TX_BUFFER_LEN
#define DYN_MASTER_MAX_TX_BUFFER_LEN 128
#endif
#ifndef DYN_MASTER_MAX_RX_BUFFER_LEN
#define DYN_MASTER_MAX_RX_BUFFER_LEN 128
#endif
#ifndef DYN_MASTER_DEFAULT_BAUDRATE
#define DYN_MASTER_DEFAULT_BAUDRATE 1000000
#endif
#ifndef DYN_MASTER_DEFAULT_TIMEOUT_US
#define DYN_MASTER_DEFAULT_TIMEOUT_US 10000
#endif
#ifndef SERIAL_CONSOLE_MAX_BUFFER_LEN
#define SERIAL_CONSOLE_MAX_BUFFER_LEN 128
#endif
#endif
......@@ -3,14 +3,6 @@
#include "dynamixel.h"
#ifndef MAX_DYN_MASTER_TX_BUFFER_LEN
#define MAX_DYN_MASTER_TX_BUFFER_LEN 128
#endif
#ifndef MAX_DYN_MASTER_RX_BUFFER_LEN
#define MAX_DYN_MASTER_RX_BUFFER_LEN 128
#endif
/* public functions */
void dyn_master_init(void);
void dyn_master_set_rx_timeout(uint16_t timeout_ms);
......
......@@ -4,8 +4,6 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#define SERIAL_CONSOLE_BUFFER 128
void serial_console_init(uint32_t baudrate);
#endif
#include "comm_cfg.h"
#include "dynamixel_master.h"
/* private variables */
uint8_t dyn_master_tx_buffer[MAX_DYN_MASTER_TX_BUFFER_LEN];
uint8_t dyn_master_tx_buffer[DYN_MASTER_MAX_TX_BUFFER_LEN];
uint8_t dyn_master_sent_bytes;
volatile uint8_t dyn_master_sent_done;
uint8_t dyn_master_rx_buffer[MAX_DYN_MASTER_RX_BUFFER_LEN];
uint8_t dyn_master_rx_buffer[DYN_MASTER_MAX_RX_BUFFER_LEN];
uint8_t dyn_master_received_bytes;
volatile uint8_t dyn_master_packet_ready;
return_level_t dyn_master_return_level;
......@@ -204,10 +205,10 @@ void dyn_master_init(void)
// bit3: stop bit(0 = stop bit 1, 1 = stop bit 2)
// bit2,bit1: data size(11 = 8bit)
UCSR0C = 0b00000110;
dyn_master_set_baudrate((uint32_t)1000000);
dyn_master_set_baudrate((uint32_t)DYN_MASTER_DEFAULT_BAUDRATE);
/* initialize the timeout timer */
dyn_master_rx_timeout_us=10000;
dyn_master_rx_timeout_us=DYN_MASTER_DEFAULT_TIMEOUT_US;
TCCR0A=0x00;// normal mode, output disabled on both channels
TCCR0B=0x00;// normal mode, prescaler 0 (timer disabled)
TIMSK0=0x00;// all interrupts disabled
......
#include "comm_cfg.h"
#include "serial_console.h"
#include <stdio.h>
/* private variables */
volatile uint8_t serial_console_buffer[SERIAL_CONSOLE_BUFFER];
volatile uint8_t serial_console_buffer[SERIAL_CONSOLE_MAX_BUFFER_LEN];
volatile uint8_t serial_console_read_ptr;
volatile uint8_t serial_console_write_ptr;
volatile uint8_t serial_console_num_data;
......@@ -11,10 +12,10 @@ static FILE *device;
/* interrupt handlers */
SIGNAL(USART1_RX_vect)
{
if(serial_console_num_data<SERIAL_CONSOLE_BUFFER)
if(serial_console_num_data<SERIAL_CONSOLE_MAX_BUFFER_LEN)
{
serial_console_buffer[serial_console_write_ptr]=UDR1;
serial_console_write_ptr=(serial_console_write_ptr+1)%SERIAL_CONSOLE_BUFFER;
serial_console_write_ptr=(serial_console_write_ptr+1)%SERIAL_CONSOLE_MAX_BUFFER_LEN;
serial_console_num_data++;
}
}
......@@ -49,7 +50,7 @@ int serial_console_getchar(FILE *dev)
while(serial_console_num_data==0);
rx=serial_console_buffer[serial_console_read_ptr];
serial_console_read_ptr=(serial_console_read_ptr+1)%SERIAL_CONSOLE_BUFFER;
serial_console_read_ptr=(serial_console_read_ptr+1)%SERIAL_CONSOLE_MAX_BUFFER_LEN;
serial_console_num_data--;
if(rx=='\r')
rx='\n';
......
#AVR-GCC Makefile
PROJECT=libcontrollers
SOURCES=src/cm510/cm510.c src/cm510/gpio.c src/cm510/adc.c src/cm510/buzzer.c
SOURCES=src/cm510.c src/gpio.c src/adc.c src/buzzer.c src/user_time.c
OBJS=$(SOURCES:.c=.o)
SRC_DIR=./src/
BIN_DIR=./build/
LIB_DIR=./lib/
DEV_DIR=../dyn_devices/
COMM_DIR=../communications/
MAN_DIR=../motion/
CONT_DIR=./
CC=avr-gcc
OBJCOPY=avr-ar
MMCU=atmega2561
INC_DIRS=-I./include/ -I$(COMM_DIR)include
INC_DIRS=-I$(CONT_DIR)include/ -I$(COMM_DIR)include -I$(DEV_DIR)include -I$(MAN_DIR)include
LIBS=$(COMM_DIR)lib/libcomm.a
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
......@@ -21,7 +24,7 @@ ARFLAGS=rsc
.PHONY: all show_banner clean
all: show_banner communications dyn_devices $(PROJECT).a examples
all: show_banner communications dyn_devices motion_manager $(PROJECT).a
show_banner:
@echo "------------------------------------------------------";
......@@ -47,8 +50,14 @@ communications:
dyn_devices:
$(MAKE) -C ../dyn_devices
motion_manager:
$(MAKE) -C ../motion
examples:
$(MAKE) -C src/examples
$(MAKE) -C ../communications examples
$(MAKE) -C ../dyn_devices examples
$(MAKE) -C ../motion examples
download:
$(MAKE) -C src/examples download
......@@ -59,3 +68,4 @@ clean:
$(MAKE) -C src/examples clean
$(MAKE) -C ../communications clean
$(MAKE) -C ../dyn_devices clean
$(MAKE) -C ../motion clean
......@@ -7,10 +7,6 @@
// ADC channel identifiers
typedef enum {ADC_VCC=0,ADC_PORT_1=1,ADC_PORT_2=2,ADC_PORT_3=3,ADC_PORT_4=4,ADC_PORT_5=5,ADC_PORT_6=6} adc_t;
#define NUM_ADC 7
// number of samples to average
#define ADC_MAX_NUM_SAMPLES 16
#define ADC_SAMPLE_PERIOD_MS 10
#define ADC_VOLTAGE_ALARM_TH 559
/**
* \brief Function to initialize the ADC module
......
......@@ -18,6 +18,15 @@
#ifndef _CM510_H
#define _CM510_H
#include "cont_cfg.h"
#include "motion_cfg.h"
#include "comm_cfg.h"
#include "gpio.h"
#include "adc.h"
#include "buzzer.h"
#include "motion_manager.h"
#include "user_time.h"
// general interface
/**
* \brief Function to initialize all the cm510 modules
......
#ifndef _CM510_CFG_H
#define _CM510_CFG_H
#include "buzzer.h"
// controller configuration parameters
#define ADC_MAX_NUM_SAMPLES 16
#define ADC_SAMPLE_PERIOD_MS 10
#define ADC_VOLTAGE_ALARM_TH 559
#define ADC_VOLTAGE_ALARM_WINDOW 20
#define ADC_VOLTAGE_ALARM_NOTE NOTE_LA
#define ADC_VOLTAGE_ALARM_TIME_ON 30
#define ADC_VOLTAGE_ALARM_TIME_OFF 30
// communication configuration parameters
#define DYN_MASTER_MAX_TX_BUFFER_LEN 128
#define DYN_MASTER_MAX_RX_BUFFER_LEN 128
#define DYN_MASTER_DEFAULT_BAUDRATE 1000000
#define DYN_MASTER_DEFAULT_TIMEOUT_US 10000
#define SERIAL_CONSOLE_MAX_BUFFER_LEN 128
// motion configuration parameters
#define MANAGER_MAX_NUM_SERVOS 18
#define MANAGER_MISSING_SERVOS_ALARM_NOTE NOTE_SOL
#define MANAGER_MISSING_SERVOS_ALARM_TIME_ON 10
#define MANAGER_MISSING_SERVOS_ALARM_TIME_OFF 100
#define BALANCE_GYRO_CAL_NUM_SAMPLES 10
#define BALANCE_GYRO_X_CHANNEL 3
#define BALANCE_GYRO_Y_CHANNEL 4
#define BALANCE_GYRO_CAL_FAILED_ALARM_NOTE NOTE_SOL
#define BALANCE_GYRO_CAL_FAILED_ALARM_TIME_ON 10
#define BALANCE_GYRO_CAL_FAILED_ALARM_TIME_OFF 100
#define BALANCE_MAX_CAL_GYRO_ERROR 20.0
#define BALANCE_FORWARD_FALL_THD_VALUE (-200)
#define BALANCE_BACKWARD_FALL_THD_VALUE 200
#define BALANCE_KNEE_GAIN (4.0/54.0)
#define BALANCE_ANKLE_ROLL_GAIN (4.0/18.0)
#define BALANCE_HIP_PITCH_GAIN (4.0/20.0)
#define BALANCE_ANKLE_PITCH_GAIN (4.0/40.0)
#endif
#ifndef _CONT_CFG_H
#define _CONT_CFG_H
#include "buzzer.h"
// number of samples to average
#ifndef ADC_MAX_NUM_SAMPLES
#define ADC_MAX_NUM_SAMPLES 16
#endif
#ifndef ADC_SAMPLE_PERIOD_MS
#define ADC_SAMPLE_PERIOD_MS 10
#endif
#ifndef ADC_VOLTAGE_ALARM_TH
#define ADC_VOLTAGE_ALARM_TH 559
#endif
#ifndef ADC_VOLTAGE_ALARM_WINDOW
#define ADC_VOLTAGE_ALARM_WINDOW 20
#endif
#ifndef ADC_VOLTAGE_ALARM_NOTE
#define ADC_VOLTAGE_ALARM_NOTE NOTE_LA
#endif
#ifndef ADC_VOLTAGE_ALARM_TIME_ON
#define ADC_VOLTAGE_ALARM_TIME_ON 30
#endif
#ifndef ADC_VOLTAGE_ALARM_TIME_OFF
#define ADC_VOLTAGE_ALARM_TIME_OFF 30
#endif
#endif
#ifndef _USER_TIME_H
#define _USER_TIME_H
#include <avr/io.h>
#include <avr/interrupt.h>
/* public functions */
void init_user_time(void);
void user_time_set_period(uint16_t period_ms);
void user_time_set_one_time(uint16_t time_ms);
uint8_t user_time_is_period_done(void);
uint8_t user_time_is_done(void);
void user_time_stop(void);
#endif
#include "cm510_cfg.h"
#include "adc.h"
#include "gpio.h"
#include "buzzer.h"
#include <util/delay.h>
......@@ -14,6 +14,7 @@ volatile uint16_t adc_values[NUM_ADC];
volatile uint16_t adc_avg_values[NUM_ADC];
volatile uint8_t adc_current_ch;
volatile uint8_t adc_current_sample;
volatile uint8_t adc_voltage_alarm;
/* private functions */
void adc_set_channel(uint8_t ch_id)
......@@ -94,15 +95,21 @@ void adc_loop(void)
{
// voltage_mv=((uint32_t)data*(uint32_t)5000*(uint32_t)133)/((uint16_t)1024*(uint16_t)33);
// compare with hysteresis
if(buzzer_is_playing_alarm())
if(adc_voltage_alarm==0x01)
{
if(data>=(ADC_VOLTAGE_ALARM_TH+20))// voltage under 11 V
if(data>=(ADC_VOLTAGE_ALARM_TH+ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V
{
buzzer_stop_alarm();
adc_voltage_alarm=0x00;
}
}
else
{
if(data<(ADC_VOLTAGE_ALARM_TH-20))// voltage under 11 V
if(data<(ADC_VOLTAGE_ALARM_TH-ADC_VOLTAGE_ALARM_WINDOW))// voltage under 11 V
{
buzzer_start_alarm(NOTE_LA,30,30);
adc_voltage_alarm=0x01;
}
}
}
adc_current_ch=(adc_current_ch+1)%NUM_ADC;
......@@ -114,7 +121,7 @@ void adc_loop(void)
/* public functions */
void init_adc(void)
{
uint8_t i=0;
uint8_t i=0,j=0;
ADCSRA=(1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);// enable ADC set prescaler 1/128
// disable auto trigger, interrupts disabled
......@@ -127,9 +134,16 @@ void init_adc(void)
DDRA = 0xFC;
PORTA = 0xFC;// all sensors disabled
// initialize internal sample variables to 12V to avoid initial voltage alarm
for(i=0;i<NUM_ADC;i++)
adc_values[i]=0x0000;
{
adc_values[i]=0x0266;//equivalent to 12 V
adc_avg_values[i]=0x0266;//equivalent to 12 V
for(j=0;j<ADC_MAX_NUM_SAMPLES;j++)
adc_ch_data[i][j]=0x0266;
}
adc_current_ch=0;
adc_voltage_alarm=0x00;
// configure the timer 2 to perform periodic conversions (1 ms period)
TCCR2A=(1<<WGM21);// CTC mode, no output
......
......@@ -68,22 +68,6 @@ void buzzer_loop(void)
}
}
void buzzer_start_alarm(note_t note, uint16_t on_time_100ms,uint16_t off_time_100ms)
{
if(buzzer_playing_alarm==0x00)// the alram is not playing
{
if(buzzer_playing)// if the normal buzzer is playing, stop it.
buzzer_stop();
buzzer_playing_alarm=0x01;
buzzer_time_on_100ms=on_time_100ms;
buzzer_time_off_100ms=off_time_100ms;
buzzer_note=note;
// enable PWM output
TCCR1A&=(~(1<<COM1A0));
TCCR1A|=(1<<COM1A1);
}
}
void buzzer_stop_alarm(void)
{
if(buzzer_playing_alarm==0x01)
......@@ -94,6 +78,27 @@ void buzzer_stop_alarm(void)
}
}
void buzzer_start_alarm(note_t note, uint16_t on_time_100ms,uint16_t off_time_100ms)
{
if(buzzer_playing_alarm)// the alram is not playing
buzzer_stop_alarm();
else if(buzzer_playing)// if the normal buzzer is playing, stop it.
buzzer_stop();
// set top count value
ICR1H = 0x00;
ICR1L = buzzer_note_freq[note];
// Set the compare value to half the period
OCR1AH = 0x00;
OCR1AL = buzzer_note_freq[note]>>1;
buzzer_time_on_100ms=on_time_100ms;
buzzer_time_off_100ms=off_time_100ms;
buzzer_note=note;
// enable PWM output
TCCR1A&=(~(1<<COM1A0));
TCCR1A|=(1<<COM1A1);
buzzer_playing_alarm=0x01;
}
/* public functions */
void init_buzzer(void)
{
......
......@@ -15,14 +15,12 @@
* along with iri-libbioloid. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cm510.h"
#include "gpio.h"
#include "adc.h"
#include "buzzer.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <util/delay.h>
#include "cm510_cfg.h"
#include "cm510.h"
/* external user functions */
extern void user_init(void);
......@@ -32,6 +30,8 @@ extern void user_loop(void);
extern void pushbuttons_loop(void);
extern void adc_loop(void);
extern void buzzer_loop(void);
extern void manager_loop(void);
extern void user_time_loop(void);
// general interface
void init_cm510(void)
......@@ -40,20 +40,26 @@ void init_cm510(void)
init_buttons();
init_adc();
init_buzzer();
init_user_time();
}
int16_t main(void)
{
init_cm510();
sei();
manager_init(MANAGER_MAX_NUM_SERVOS);
/* call the user initialization function */
user_init();
sei();
// turn BAT_LED on to indicate the initialization is done
turn_led_on(LED_BAT);
while(1)
{
pushbuttons_loop();
adc_loop();
buzzer_loop();
manager_loop();
user_time_loop();
user_loop();
}
}
......@@ -6,15 +6,17 @@ SOURCES=main.c
OBJS=$(SOURCES:.c=.o)
SRC_DIR=./src/
CM510_DIR=../../lib
COMM_DIR=../../../communications/lib
DEV_DIR=../../../dyn_devices/
COMM_DIR=../../../communications/
MAN_DIR=../../../motion/
CONT_DIR=../../
CC=avr-gcc
OBJCOPY=avr-objcopy
MMCU=atmega2561
INCLUDE_DIRS=-I../../include/ -I../../../communications/include
INCLUDE_DIRS=-I$(CONT_DIR)include/ -I$(COMM_DIR)include -I$(MAN_DIR)include -I$(DEV_DIR)include
LIBS=$(CM510_DIR)/libcontrollers.a $(COMM_DIR)/libcomm.a
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
......
......@@ -20,24 +20,27 @@
#include "adc.h"
#include "buzzer.h"
#include "serial_console.h"
#include "user_time.h"
#include <stdio.h>
#include <util/delay.h>
void user_init(void)
{
serial_console_init(57600);
user_time_set_period(10);
}
void user_loop(void)
{
if(user_time_is_period_done())
{
printf("Gyro X: %d\n",get_adc_avg_channel(ADC_PORT_3));
printf("Gyro Y: %d\n",get_adc_avg_channel(ADC_PORT_4));
}
if(is_button_pressed(BTN_START))
turn_led_on(LED_AUX);
else
turn_led_off(LED_AUX);
// if(get_adc_avg_channel(ADC_VCC)<512)
// turn_led_on(LED_PLAY);
// else
// turn_led_off(LED_PLAY);
if(is_button_falling_edge(BTN_UP))
buzzer_start(NOTE_SOL,30,30);
else
......@@ -45,5 +48,4 @@ void user_loop(void)
if(is_button_falling_edge(BTN_DOWN))
buzzer_stop();
}
printf("loop\n");
}
File moved
#include "user_time.h"
/* private variables */
uint8_t user_time_single;
uint16_t user_time_time;
uint16_t user_time_period;
volatile uint8_t user_time_done;
volatile uint8_t user_time_period_done;
/* private funcitons */
void user_time_loop(void)
{
static uint16_t count=0;
if(TIFR5&(1<<OCF5A))
{
TIFR5|=(1<<OCF5A);// clear any interrupt
TCNT5H=0x00;
TCNT5L=0x00;
if(user_time_single==0x01)
{
if(count==user_time_time)
{
count=0;
user_time_done=0x01;
TCCR5B&=0xF8;// disable timer
}
else
count++;
}
else
{
if(count==user_time_period)
{
count=0;
user_time_period_done=0x01;
}
else
count++;
}
}
}
/* public functions */
void init_user_time(void)
{
// initialize timer3 for buzzer time control (100ms period)
TIMSK5=0x00;// disable all interrupts
TIFR5=0x2F;// clear any pending interrupt
TCNT5H=0x00;
TCNT5L=0x00;
// set PWM mode with ICR top-count (CTC mode)
TCCR5A=0x00;
TCCR5A&=~(1<<WGM50);
TCCR5A&=~(1<<WGM51);
TCCR5B=0x00;
TCCR5B|=(1<<WGM52);
TCCR5B&=~(1<<WGM53);
// clear output compare value A
OCR5AH=0x18;
OCR5AL=0x6A;
TCCR5B&=0xF8;// disable timer by default
/* initialize internal variables */
user_time_single=0x00;
user_time_time=0x00;
user_time_period=0x00;
user_time_done=0x00;
user_time_period_done=0x00;
}
void user_time_set_period(uint16_t period_ms)
{
user_time_single=0x00;
user_time_period=period_ms/100;
user_time_period_done=0x00;
TCNT5H=0x00;
TCNT5L=0x00;
TIFR5=0x2F;// clear any pending interrupt
TCCR5B&=0xF8;// disable timer by default
TCCR5B|=0x04;
}
void user_time_set_one_time(uint16_t time_ms)
{
user_time_single=0x01;
user_time_time=time_ms/100;
user_time_done=0x00;
TCNT5H=0x00;
TCNT5L=0x00;
TIFR5=0x2F;// clear any pending interrupt
TCCR5B&=0xF8;// disable timer by default
TCCR5B|=0x04;
}
uint8_t user_time_is_period_done(void)
{
if(user_time_period_done==0x01)
{
user_time_period_done=0x00;
return 0x01;
}
else
return 0x00;
}
uint8_t user_time_is_done(void)
{
if(user_time_done==0x01)
{
user_time_done=0x00;
return 0x01;
}
else
return 0x00;
}
void user_time_stop(void)
{
TCCR5B&=0xF8;// disable timer
TIFR5|=(1<<OCF5A);// clear any interrupt
TCNT5H=0x00;
TCNT5L=0x00;
user_time_period_done=0x00;
user_time_done=0x00;
}
......@@ -19,7 +19,7 @@ ARFLAGS=rsc
.PHONY: all show_banner clean
all: show_banner $(PROJECT).a examples
all: show_banner communications $(PROJECT).a
show_banner:
@echo "------------------------------------------------------";
......@@ -34,11 +34,14 @@ show_banner:
@echo "------------------------------------------------------";
$(PROJECT).a: ${OBJS}
$(OBJCOPY) $(ARFLAGS) ${LIB_DIR}$(PROJECT).a $(LIBS) $(OBJS)
$(OBJCOPY) $(ARFLAGS) ${LIB_DIR}$(PROJECT).a $(OBJS)
%.o: %.c
$(CC) -c $(CFLAGS) $(INC_DIRS) -o $@ $<
communications:
$(MAKE) -C $(COMM_DIR)
examples:
$(MAKE) -C src/examples
......
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