From d2f14e3cf13a8f716641a372b4769ae883b30e26 Mon Sep 17 00:00:00 2001 From: pfrison Date: Mon, 1 Apr 2019 18:07:13 +0200 Subject: [PATCH] Debut programme arduino principal --- PercTeacher/Sources/Robot/robot.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ PercTeacher/Sources/Robot/servoAndStepperControl.c | 79 ------------------------------------------------------------------------------- Robot/deplacement.c | 41 +++++++++++++++++++++++++++++++++++++++++ Robot/deplacement.h | 30 ++++++++++++++++++++++++++++++ Robot/main.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Robot/main.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Robot/makefile | 36 ++++++++++++++++++++++++++++++++++++ Robot/serial.c | 18 ++++++++++++++++++ Robot/serial.h | 8 ++++++++ 9 files changed, 374 insertions(+), 79 deletions(-) create mode 100644 PercTeacher/Sources/Robot/robot.c delete mode 100644 PercTeacher/Sources/Robot/servoAndStepperControl.c create mode 100644 Robot/deplacement.c create mode 100644 Robot/deplacement.h create mode 100644 Robot/main.c create mode 100644 Robot/main.h create mode 100644 Robot/makefile create mode 100644 Robot/serial.c create mode 100644 Robot/serial.h diff --git a/PercTeacher/Sources/Robot/robot.c b/PercTeacher/Sources/Robot/robot.c new file mode 100644 index 0000000..8828c19 --- /dev/null +++ b/PercTeacher/Sources/Robot/robot.c @@ -0,0 +1,79 @@ +#include +#include + +#define SERIAL_BAUDE_RATE 9600 +#define DELAI_STEP 2 +#define PIN_MOTEUR_GAUCHE 0x40 // pin 12 +#define PIN_MOTEUR_DROIT 0x20 // pin 11 + +#define CPU_FREQ 16000000L + +//gestion des moteurs +void init_pins(void){ + DDRB = PIN_MOTEUR_GAUCHE | PIN_MOTEUR_DROIT; +} + +void pas_moteur(int g, int d){ + if (g) + PORTB |= PIN_MOTEUR_GAUCHE; + if (d) + PORTB |= PIN_MOTEUR_DROIT; + _delay_ms(DELAI_STEP); + if (g) + PORTB &= ~PIN_MOTEUR_GAUCHE; + if (d) + PORTB &= ~PIN_MOTEUR_DROIT; + _delay_ms(DELAI_STEP); +} + +//gestion de la liaison serie +void init_serial(int speed){ + UBRR0 = CPU_FREQ / (((unsigned long int) speed) << 4) - 1; //Set baud rate + UCSR0B = (1 << TXEN0 | 1 << RXEN0); //Enable transmitter & receiver + UCSR0C = (1 << UCSZ01 | 1 << UCSZ00); //Set 8 bits character and 1 stop bit + UCSR0A &= ~(1 << U2X0); //Set off UART baud doubler +} + +void send_serial(unsigned char c){ + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = c; +} + +unsigned char get_serial(void){ + loop_until_bit_is_set(UCSR0A, RXC0); + return UDR0; +} + +int main(void){ + init_pins(); + init_serial(SERIAL_BAUDE_RATE); + + while(1){ + unsigned char deltaL1 = get_serial(); + unsigned char deltaL2 = get_serial(); + unsigned char deltaR1 = get_serial(); + unsigned char deltaR2 = get_serial(); + + + send_serial('a'); + + // conversion 2x8 bits => 16 bits + int deltaL = (deltaL1 << 8) + deltaL2; + int deltaR = (deltaR1 << 8) + deltaR2; + + int deltaMax = deltaL > deltaR ? deltaL : deltaR; + for(int i=0; i i && deltaR > i) + pas_moteur(1, 1); + else if(deltaL > i) + pas_moteur(1, 0); + else if(deltaR > i) + pas_moteur(0, 1); + } + + send_serial('b'); + send_serial(0x01); + } + return 0; +} + diff --git a/PercTeacher/Sources/Robot/servoAndStepperControl.c b/PercTeacher/Sources/Robot/servoAndStepperControl.c deleted file mode 100644 index 8828c19..0000000 --- a/PercTeacher/Sources/Robot/servoAndStepperControl.c +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include - -#define SERIAL_BAUDE_RATE 9600 -#define DELAI_STEP 2 -#define PIN_MOTEUR_GAUCHE 0x40 // pin 12 -#define PIN_MOTEUR_DROIT 0x20 // pin 11 - -#define CPU_FREQ 16000000L - -//gestion des moteurs -void init_pins(void){ - DDRB = PIN_MOTEUR_GAUCHE | PIN_MOTEUR_DROIT; -} - -void pas_moteur(int g, int d){ - if (g) - PORTB |= PIN_MOTEUR_GAUCHE; - if (d) - PORTB |= PIN_MOTEUR_DROIT; - _delay_ms(DELAI_STEP); - if (g) - PORTB &= ~PIN_MOTEUR_GAUCHE; - if (d) - PORTB &= ~PIN_MOTEUR_DROIT; - _delay_ms(DELAI_STEP); -} - -//gestion de la liaison serie -void init_serial(int speed){ - UBRR0 = CPU_FREQ / (((unsigned long int) speed) << 4) - 1; //Set baud rate - UCSR0B = (1 << TXEN0 | 1 << RXEN0); //Enable transmitter & receiver - UCSR0C = (1 << UCSZ01 | 1 << UCSZ00); //Set 8 bits character and 1 stop bit - UCSR0A &= ~(1 << U2X0); //Set off UART baud doubler -} - -void send_serial(unsigned char c){ - loop_until_bit_is_set(UCSR0A, UDRE0); - UDR0 = c; -} - -unsigned char get_serial(void){ - loop_until_bit_is_set(UCSR0A, RXC0); - return UDR0; -} - -int main(void){ - init_pins(); - init_serial(SERIAL_BAUDE_RATE); - - while(1){ - unsigned char deltaL1 = get_serial(); - unsigned char deltaL2 = get_serial(); - unsigned char deltaR1 = get_serial(); - unsigned char deltaR2 = get_serial(); - - - send_serial('a'); - - // conversion 2x8 bits => 16 bits - int deltaL = (deltaL1 << 8) + deltaL2; - int deltaR = (deltaR1 << 8) + deltaR2; - - int deltaMax = deltaL > deltaR ? deltaL : deltaR; - for(int i=0; i i && deltaR > i) - pas_moteur(1, 1); - else if(deltaL > i) - pas_moteur(1, 0); - else if(deltaR > i) - pas_moteur(0, 1); - } - - send_serial('b'); - send_serial(0x01); - } - return 0; -} - diff --git a/Robot/deplacement.c b/Robot/deplacement.c new file mode 100644 index 0000000..3562e25 --- /dev/null +++ b/Robot/deplacement.c @@ -0,0 +1,41 @@ +#include +#include + +#define STEP_GAUCHE 0x10 // pin 7 +#define DIR_GAUCHE 0x08 // pin 6 +#define STEP_DROIT 0x20 // pin 12 +#define DIR_DROIT 0x10 // pin 11 + +const uint8_t deltaL[] = {}; +const uint8_t delaiL[] = {}; +const uint8_t deltaR[] = {}; +const uint8_t delaiR[] = {}; + +#define GAUCHE 0 +#define DROIT 1 + +void init_deplacements(void){ + DDRH |= STEP_GAUCHE | DIR_GAUCHE; + DDRB |= STEP_DROIT | DIR_DROIT; +} + +uint16_t* deplacement_commande_suivante(uint16_t num){ + return {deltaL[num], delaiL[num], deltaR[num], delaiR[num]}; +} + +void deplacement_moteur_gauche(uint16_t delta, uint16_t delai){ deplacement_moteur(GAUCHE, delta, delai); } +void deplacement_moteur_droit(uint16_t delta, uint16_t delai){ deplacement_moteur(DROIT, delta, delai); } +static void deplacement_moteur(uint8_t mot, uint16_t delta, uint16_t delai){ + for(uint16_t i=0; i +#include +#include + +#include "serial.h" +#include "main.h" + +#define NB_TICK 104 //1563 + +/** ----- Taches ----- */ +void task_deplacement(void){ + +} +void task_print_b(void){ + while(1){ + send_serial('b'); + _delay_ms(700); + } +} + +/** ----- Ordonnancement ----- */ +struct task{ + uint16_t sp_vise; +}; +uint8_t cpt = 0; +uint8_t premier_lancement = 0; +struct task deplacement = {0x0300}; +struct task print_b = {0x0500}; + +// Démarre le timer pour l'ordonnancement +void init_timer(){ + TCCR1B |= _BV(WGM12); // CTC mode with value in OCR1A + TCCR1B |= _BV(CS12); // CS12 = 1; CS11 = 1; CS10 =1 => CLK/1024 prescaler + TCCR1B |= _BV(CS10); + OCR1A = NB_TICK; + TIMSK1 |= _BV(OCIE1A); +} + +// Changement de contexte +ISR(TIMER1_COMPA_vect){ + if(premier_lancement == 0){ + premier_lancement++; + sei(); + SP = deplacement.sp_vise; + task_deplacement(); + }else if(premier_lancement == 1){ + SAVE_CONTEXT(); + deplacement.sp_vise = SP; + premier_lancement++; + sei(); + SP = print_b.sp_vise; + task_print_b(); + }else{ + if(cpt==0){ + SAVE_CONTEXT(); + print_b.sp_vise = SP; + SP = deplacement.sp_vise; + RESTORE_CONTEXT(); + cpt++; + }else if(cpt==1){ + SAVE_CONTEXT(); + deplacement.sp_vise = SP; + SP = print_b.sp_vise; + RESTORE_CONTEXT(); + cpt = 0; + } + } + sei(); +} + +int main(void){ + // Initialisation + init_timer(); + init_serial(9600); + sei(); + + while(1){} + return 0; +} + diff --git a/Robot/main.h b/Robot/main.h new file mode 100644 index 0000000..42902c6 --- /dev/null +++ b/Robot/main.h @@ -0,0 +1,82 @@ +#ifndef MAIN +#define MAIN + +#define SAVE_CONTEXT() \ +asm volatile ( \ +"push r0 \n\t" \ +"in r0, __SREG__ \n\t" \ +"cli \n\t" \ +"push r0 \n\t" \ +"push r1 \n\t" \ +"clr r1 \n\t" \ +"push r2 \n\t" \ +"push r3 \n\t" \ +"push r4 \n\t" \ +"push r5 \n\t" \ +"push r6 \n\t" \ +"push r7 \n\t" \ +"push r8 \n\t" \ +"push r9 \n\t" \ +"push r10 \n\t" \ +"push r11 \n\t" \ +"push r12 \n\t" \ +"push r13 \n\t" \ +"push r14 \n\t" \ +"push r15 \n\t" \ +"push r16 \n\t" \ +"push r17 \n\t" \ +"push r18 \n\t" \ +"push r19 \n\t" \ +"push r20 \n\t" \ +"push r21 \n\t" \ +"push r22 \n\t" \ +"push r23 \n\t" \ +"push r24 \n\t" \ +"push r25 \n\t" \ +"push r26 \n\t" \ +"push r27 \n\t" \ +"push r28 \n\t" \ +"push r29 \n\t" \ +"push r30 \n\t" \ +"push r31 \n\t" \ +); + +#define RESTORE_CONTEXT() \ +asm volatile ( \ +"pop r31 \n\t" \ +"pop r30 \n\t" \ +"pop r29 \n\t" \ +"pop r28 \n\t" \ +"pop r27 \n\t" \ +"pop r26 \n\t" \ +"pop r25 \n\t" \ +"pop r24 \n\t" \ +"pop r23 \n\t" \ +"pop r22 \n\t" \ +"pop r21 \n\t" \ +"pop r20 \n\t" \ +"pop r19 \n\t" \ +"pop r18 \n\t" \ +"pop r17 \n\t" \ +"pop r16 \n\t" \ +"pop r15 \n\t" \ +"pop r14 \n\t" \ +"pop r13 \n\t" \ +"pop r12 \n\t" \ +"pop r11 \n\t" \ +"pop r10 \n\t" \ +"pop r9 \n\t" \ +"pop r8 \n\t" \ +"pop r7 \n\t" \ +"pop r6 \n\t" \ +"pop r5 \n\t" \ +"pop r4 \n\t" \ +"pop r3 \n\t" \ +"pop r2 \n\t" \ +"pop r1 \n\t" \ +"pop r0 \n\t" \ +"out __SREG__, r0 \n\t" \ +"pop r0 \n\t" \ +); + +#endif \ No newline at end of file diff --git a/Robot/makefile b/Robot/makefile new file mode 100644 index 0000000..96923b6 --- /dev/null +++ b/Robot/makefile @@ -0,0 +1,36 @@ +export CC = avr-gcc + +export MCU = atmega2560 +export TARGET_ARCH = -mmcu=$(MCU) + +export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os #-g +export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os + +TARGET = servoControl +TERM = /dev/ttyACM0 +CPPFLAGS = -mmcu=$(MCU) +PGMERISP = -c wiring -b 115200 -P $(TERM) -D +export DUDE = /usr/bin/avrdude -F -v -p $(MCU) + +C_SRC = $(wildcard *.c) +OBJS = $(C_SRC:.c=.o) + +all: $(TARGET).hex + +clean: + rm -f *.o *.hex *.elf + +%.o:%.c + $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ + +$(TARGET).elf: $(OBJS) + $(CC) $(LDFLAGS) -o $@ $(OBJS) + +$(TARGET).hex: $(TARGET).elf + avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex + avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex + +upload: $(TARGET).hex + stty -F $(TERM) hupcl # reset + $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex + make clean diff --git a/Robot/serial.c b/Robot/serial.c new file mode 100644 index 0000000..a047bc7 --- /dev/null +++ b/Robot/serial.c @@ -0,0 +1,18 @@ +#include + +#define CPU_FREQ 16000000L + +void init_serial(int speed){ + UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1; //Set baud rate + UCSR0B = (1<