From b2bcd0e04c192bc01f6453072b17d71161c54c99 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Tue, 5 May 2015 17:29:26 +0200
Subject: [PATCH] Minor changes to make project compile with new version of avr
 toolchain.

---
 linear_pendulum_2009/avr/.gitignore           |  1 +
 linear_pendulum_2009/avr/Makefile             | 97 +++----------------
 .../avr/compileUploadCurrentControl.sh        |  3 -
 .../avr/compileUploadVelControl.sh            |  3 -
 linear_pendulum_2009/avr/current_control.c    | 26 ++---
 linear_pendulum_2009/avr/pccom.c              |  9 +-
 linear_pendulum_2009/avr/serialio_core.h      |  3 +-
 linear_pendulum_2009/avr/vel_control.c        | 72 ++++++++------
 linear_pendulum_2009/avr/vel_control.h        |  3 +-
 9 files changed, 84 insertions(+), 133 deletions(-)
 create mode 100644 linear_pendulum_2009/avr/.gitignore
 delete mode 100755 linear_pendulum_2009/avr/compileUploadCurrentControl.sh
 delete mode 100755 linear_pendulum_2009/avr/compileUploadVelControl.sh

diff --git a/linear_pendulum_2009/avr/.gitignore b/linear_pendulum_2009/avr/.gitignore
new file mode 100644
index 0000000..a13427f
--- /dev/null
+++ b/linear_pendulum_2009/avr/.gitignore
@@ -0,0 +1 @@
+compiled
\ No newline at end of file
diff --git a/linear_pendulum_2009/avr/Makefile b/linear_pendulum_2009/avr/Makefile
index 91fe37a..0aaf4ae 100644
--- a/linear_pendulum_2009/avr/Makefile
+++ b/linear_pendulum_2009/avr/Makefile
@@ -1,81 +1,16 @@
-PROJECT=current_control
-CHIP=atmega16
-
-ARCH=avr
-AVRSOURCE=
-AVRAUTO=compiled/$(ARCH)/
-AVRARCH=compiled/$(ARCH)/
-LIB_TARGET=$(AVRARCH)lib$(TARGET).a
-
-CC=$(ARCH)-gcc
-CXX=$(ARCH)-gcc
-CCFLAGS=-mmcu=$(CHIP) -O5 -I. -I$(AVRAUTO) 
-AS=$(ARCH)-as
-ASFLAGS=-mmcu=$(CHIP)
-AR=$(ARCH)-ar
-LD=$(ARCH)-ld
-OBJDUMP=$(ARCH)-objdump
-LDFLAGS= -T $(CHIP).link
-
-all: $(AVRARCH)$(PROJECT)
-
-$(LIB_TARGET)(%.o):     $(AVRSOURCE)%.c Makefile
-	$(CC) $(CCFLAGS) -c -o $(AVRARCH)$(%) -I$(AVRSOURCE) -I$(AVRAUTO) $(<) 
-	$(AR) r $(@) $(AVRARCH)$(%)
-	rm $(AVRARCH)$(%)
-
-$(LIB_TARGET)(%.o):     $(AVRSOURCE)%.cc Makefile
-	$(CXX) $(CCFLAGS) -c -o $(AVRARCH)$(%) -I$(AVRSOURCE) -I$(AVRAUTO) $(<) 
-	$(AR) r $(@) $(AVRARCH)$(%)
-	rm $(AVRARCH)$(%)
-
-$(LIB_TARGET)(%.o):     $(AVRSOURCE)%.s Makefile
-	$(AS) $(ASFLAGS) -o $(AVRARCH)$(%) $(AVRSOURCE)$(*).s
-	$(AR) r $(@) $(AVRARCH)$(%)
-	rm $(AVRARCH)$(%)
-
-$(AVRARCH)%.o:           $(AVRSOURCE)%.s Makefile
-	$(AS) $(ASFLAGS) -o $@ $(AVRSOURCE)$(*).s
-
-$(AVRARCH)%.o:           $(AVRSOURCE)%.c Makefile
-	$(CC) $(CCFLAGS) -c -o $@ -I. -I$(AVRAUTO) $< 
-
-
-$(AVRARCH)%:           $(AVRARCH)%.o Makefile
-	$(CC) $(CCFLAGS) -o $@ $<  -lm
-
-$(AVRARCH)%.sr:	all $(AVRARCH)%
-	avr-objcopy -O srec $(AVRARCH)/$(*) $@ 
-
-%.sr:	$(AVRARCH)%.sr
-	@/bin/true
-
-%.load:	compiled/avr/%.sr
-#	/work/andersb/atmel/uisp/uisp-0.2b/src/uisp \
-#	 	-dno-poll -dstk200 --erase --upload if=$(AVRARCH)/$(*).sr
-	uisp \
-		-dprog=stk200 \
-		--erase \
-		--upload if=compiled/avr/$*.sr
-
-%.load.stk500:	compiled/avr/%.sr
-	uisp \
-		-dprog=stk500 \
-		--erase \
-		--upload if=compiled/avr/$*.sr
-
-%.dump:	all $(AVRARCH)/%
-	$(OBJDUMP) -D $(AVRARCH)/$(*)
-
-$(AVRARCH)fuse.sr:	fuse.s
-	avr-gcc -o $(AVRARCH)fuse.o -c fuse.s 
-	avr-objcopy -O srec $(AVRARCH)fuse.o $(AVRARCH)fuse.sr
-
-fuse.load:	$(AVRARCH)fuse.sr
-	uisp \
-		-dprog=stk200 \
-		--erase \
-		--segment=fuse \
-		--upload if=compiled/avr/fuse.sr
-
-load.stk500:	$(PROJECT).load.stk500
\ No newline at end of file
+TARGETS=current_control vel_control
+
+current_control.ARCH=avr
+current_control.CHIP=atmega16
+# 14.7456 MHz crystal, brown out
+current_control.FUSE=--wr_fuse_l=0x1f --wr_fuse_h=0xd9 --wr_fuse_e=0xff
+current_control.C=current_control
+
+vel_control.ARCH=avr
+vel_control.CHIP=atmega16
+# 14.7456 MHz crystal, brown out
+vel_control.FUSE=--wr_fuse_l=0x1f --wr_fuse_h=0xd9 --wr_fuse_e=0xff
+vel_control.C=vel_control pccom
+vel_control.H=pccom serialio_core
+
+include ../../common/avr/Makefile.common
diff --git a/linear_pendulum_2009/avr/compileUploadCurrentControl.sh b/linear_pendulum_2009/avr/compileUploadCurrentControl.sh
deleted file mode 100755
index e97cfdf..0000000
--- a/linear_pendulum_2009/avr/compileUploadCurrentControl.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-avr-gcc -mmcu=atmega16 -g -Wall -o current_control current_control.c
-avr-objcopy -Osrec current_control current_control.sr
-uisp -dprog=stk200 --erase --upload if=current_control.sr
\ No newline at end of file
diff --git a/linear_pendulum_2009/avr/compileUploadVelControl.sh b/linear_pendulum_2009/avr/compileUploadVelControl.sh
deleted file mode 100755
index bad2b4b..0000000
--- a/linear_pendulum_2009/avr/compileUploadVelControl.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-avr-gcc -mmcu=atmega16 -O -g -Wall -o vel_control.o vel_control.c pccom.c
-avr-objcopy -Osrec vel_control.o vel_control.sr
-uisp -dprog=stk200 --erase --upload if=vel_control.sr
\ No newline at end of file
diff --git a/linear_pendulum_2009/avr/current_control.c b/linear_pendulum_2009/avr/current_control.c
index 5736fce..8c87daf 100644
--- a/linear_pendulum_2009/avr/current_control.c
+++ b/linear_pendulum_2009/avr/current_control.c
@@ -6,11 +6,11 @@
 
 ***************************************************************
 */
-#include <avr/twi.h>
+#include <util/twi.h>
 #include <avr/io.h>
-#include <avr/signal.h>
 #include <avr/interrupt.h>
 #include <inttypes.h>
+#include <compat/deprecated.h>
 
 
 // control variables
@@ -80,10 +80,10 @@ static inline void sendData() {
 
 
 /* Timer 2 compare match interupt, 28.8 kHz, syncronized with pwm-period */
-SIGNAL(SIG_OUTPUT_COMPARE2) {
+SIGNAL(TIMER2_COMP_vect) {
 
   // Start AD conversion
-  ADCSRA |= BV(ADSC);
+  ADCSRA |= _BV(ADSC);
   
   // Read previous AD-conversion result
   low = inp(ADCL);
@@ -129,7 +129,7 @@ SIGNAL(SIG_OUTPUT_COMPARE2) {
 
   
   // TWI-communication, recieve reference from velocity controller
-  if ((BV(TWINT)&inp(TWCR))) {
+  if ((_BV(TWINT)&inp(TWCR))) {
     status = (inp(TWSR)&0xf8);
     // status 0x80 means data recieved
     if (status == 0x80) {
@@ -138,7 +138,7 @@ SIGNAL(SIG_OUTPUT_COMPARE2) {
     } 
     else {
     }
-    outp(BV(TWINT)|BV(TWEN)|BV(TWEA),TWCR);
+    outp(_BV(TWINT)|_BV(TWEN)|_BV(TWEA),TWCR);
   }
 
   // For logging purposes
@@ -176,8 +176,8 @@ int main()
 
   /* Timer section */
   // Timer 1, fast PWM no prescaling (non-inverting mode (start high, switch to low))
-  outp(BV(COM1B1)|BV(WGM11)|BV(WGM10),TCCR1A);
-  outp(BV(CS10)|BV(WGM13)|BV(WGM12),TCCR1B);
+  outp(_BV(COM1B1)|_BV(WGM11)|_BV(WGM10),TCCR1A);
+  outp(_BV(CS10)|_BV(WGM13)|_BV(WGM12),TCCR1B);
   
   // Reset Timer1 and set TOP-value to 128 (means 7-bit pwm-signal-> 115.2 kHz)
   outp(0x00,OCR1AH);
@@ -190,13 +190,13 @@ int main()
 
   
   /* Timer 2, 4 times pwm-period, for control sampling, prescaler 8, 28.8 kHz */
-  outp(BV(WGM21)|BV(CS21),TCCR2);
+  outp(_BV(WGM21)|_BV(CS21),TCCR2);
   outp(0x3f,OCR2);
   /* Reset timer 2 */
   outp(0,TCNT2);
 
   // Enable timer2 compare match interrupts
-  outp(BV(OCIE2),TIMSK);
+  outp(_BV(OCIE2),TIMSK);
 
   //Serial communication
   outp(0x00, UCSRA);	// USART:
@@ -206,16 +206,16 @@ int main()
   outp(7,UBRRL);	// USART: 115200 @ 14.7456MHz
   
   /* AREF (AREF is 5V) pin external capacitor, MUX0 for current measurement */
-  outp(BV(REFS0)|BV(MUX0),ADMUX); 	
+  outp(_BV(REFS0)|_BV(MUX0),ADMUX); 	
   
   // Enable ADC, start first conversion, prescaler 32, not free running mode
-  outp(BV(ADEN)|BV(ADSC)|BV(ADPS2)|BV(ADPS0),ADCSRA);
+  outp(_BV(ADEN)|_BV(ADSC)|_BV(ADPS2)|_BV(ADPS0),ADCSRA);
 
 
   // Initialize TWI
   outp(0x02,TWBR);  // set SCL-frequency CPU-freq/(16+2*2)
   outp(0x02,TWAR);  // slave address
-  outp(BV(TWEA)|BV(TWEN),TWCR); // enable TWI, enable ACK
+  outp(_BV(TWEA)|_BV(TWEN),TWCR); // enable TWI, enable ACK
 
   //Enable interrupts
   sei();
diff --git a/linear_pendulum_2009/avr/pccom.c b/linear_pendulum_2009/avr/pccom.c
index 7d074ad..f85bd4b 100644
--- a/linear_pendulum_2009/avr/pccom.c
+++ b/linear_pendulum_2009/avr/pccom.c
@@ -69,7 +69,10 @@ void pccom_receiveByte(char ch)
   case serialio_setchannel: {
     switch (serialio_channel) {
     case 0: { 
-      setRef(serialio_value - (1L<<12));
+      setVelRef(serialio_value - (1L<<12));
+    } break;
+    case 1: {
+      setAccRef(serialio_value - (1L<<15));
     } break;
     }
   } break;
@@ -107,6 +110,10 @@ static uint8_t sendConfigPacket(uint8_t position)
     case 13: CONF_ANALOG_OUT(0, CONF_MIN(CONF_NEGATIVE_VOLT(1))); break;
     case 14: CONF_ANALOG_OUT(0, CONF_MAX(CONF_POSITIVE_VOLT(1))); break;
       
+    case 15: CONF_ANALOG_OUT(1, CONF_RESOLUTION(16)); break;    // Reference to acc-ctrl
+    case 16: CONF_ANALOG_OUT(1, CONF_MIN(CONF_NEGATIVE_VOLT(1))); break;
+    case 17: CONF_ANALOG_OUT(1, CONF_MAX(CONF_POSITIVE_VOLT(1))); break;
+
     default: CONF_END(); return 0;
   }
   
diff --git a/linear_pendulum_2009/avr/serialio_core.h b/linear_pendulum_2009/avr/serialio_core.h
index 5ca09ae..4115ec5 100644
--- a/linear_pendulum_2009/avr/serialio_core.h
+++ b/linear_pendulum_2009/avr/serialio_core.h
@@ -137,6 +137,7 @@ static void serialio_putchannel(unsigned char channel, unsigned long value);
 static volatile unsigned long serialio_value;
 static volatile unsigned char serialio_channel, serialio_length;
 
+__attribute__ ((unused))
 static void serialio_putbit(unsigned char channel, unsigned char value) 
 {
   if (value) {
@@ -144,7 +145,7 @@ static void serialio_putbit(unsigned char channel, unsigned char value)
   } else {
     serialio_putchar(0x00 | (channel & 0x1f));
   }
-}
+} 
 
 static void serialio_putchannel(unsigned char channel, unsigned long value) 
 {
diff --git a/linear_pendulum_2009/avr/vel_control.c b/linear_pendulum_2009/avr/vel_control.c
index ea57e49..5df3ca8 100644
--- a/linear_pendulum_2009/avr/vel_control.c
+++ b/linear_pendulum_2009/avr/vel_control.c
@@ -6,20 +6,21 @@
 
 ***************************************************************
 */
-#include <avr/twi.h>
+#include <util/twi.h>
 #include <avr/io.h>
-#include <avr/signal.h>
 #include <avr/interrupt.h>
 #include <inttypes.h>
+#include <compat/deprecated.h>
 
 #include "pccom.h"
 #include "vel_control.h"
 
 
 // reference variables
-volatile int32_t ref = 0;        // 11 frac bits
+volatile int32_t ref = 0;        // 11 frac bits (variable in mm/s, max 2^12)
 volatile int32_t refCtrl = 0;    // ref used in ctrl-loop (=ref sent from simulink)
-
+volatile int32_t aRef = 0;       // 14 frac bits (variable in mm/s, max 2^15)
+volatile uint16_t nbrSamples = 0; // nbr of samples between ctrl-ref updates
 
 // velocity control variables
 volatile int32_t u = 0;          // 11 frac bits
@@ -93,9 +94,16 @@ int32_t getCurrentRef() {
 }
 
 
-/* Set new reference value */
-void setRef(int32_t newRef) {
-  ref = newRef;
+/* Set new acceleration reference value */
+void setAccRef(int32_t newAccRef) {
+  aRef = newAccRef;
+  nbrSamples = 0;
+}
+
+
+/* Set new velocity reference value */
+void setVelRef(int32_t newVelRef) {
+  ref = newVelRef;
 }
 
 /* Routine used to initialize the positional encoders */
@@ -106,7 +114,7 @@ void initPos()
 }
 
 /* Timer 2, Encoder counter, 73 kHz updates position variable */
-SIGNAL(SIG_OUTPUT_COMPARE2) {
+SIGNAL(TIMER2_COMP_vect) {
 
   // Update position from encoder
   newX = ENCODERX;
@@ -138,9 +146,9 @@ SIGNAL(SIG_OUTPUT_COMPARE2) {
 
 
 /* Timer 0, serial communication with simulink */
-SIGNAL(SIG_OUTPUT_COMPARE0) {
+SIGNAL(TIMER0_COMP_vect) {
 
-  TIMSK &= ~(BV(OCIE0)|BV(OCIE1A));   // Disable communication and ctrl-interrupts
+  TIMSK &= ~(_BV(OCIE0)|_BV(OCIE1A));   // Disable communication and ctrl-interrupts
 
   sei(); // enable interrupts from encoder-counter
   
@@ -163,14 +171,14 @@ SIGNAL(SIG_OUTPUT_COMPARE0) {
 
   TIFR = (1<<OCF0);        // skip pending interrupts from serial comm, (but not from ctrl)
 
-  TIMSK |= (BV(OCIE0)|BV(OCIE1A)); // reenable communication and ctrl-interrupts
+  TIMSK |= (_BV(OCIE0)|_BV(OCIE1A)); // reenable communication and ctrl-interrupts
 
 }
 
 
 
 /* Timer 0, control loop , 1 kHz */
-SIGNAL(SIG_OUTPUT_COMPARE1A) {
+SIGNAL(TIMER1_COMPA_vect) {
   
   posCtrl = pos; // store pos to use in this loop
 
@@ -183,7 +191,11 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) {
   // store velEst and ref to be sent/used here
   cli();
   velEstSend = velEst;
-  refCtrl = ref;
+  nbrSamples++;
+  refCtrl = ref+((aRef*nbrSamples)>>10);  //shift nbrSamples 10 steps (= nbrSamples*h)
+  if (nbrSamples > 500) {
+    refCtrl = 0;   // for safety reasons if doesn't send anything in 0.5 s
+  }
   // ref = ref*(1-brake); // emergency stop
   sei();
 
@@ -230,21 +242,21 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) {
 
   // TWI-communication to set current reference on the other atmel
   // send start command
-  outp(BV(TWINT)|BV(TWEN)|BV(TWSTA),TWCR);
-  while (!(TWCR&BV(TWINT))) {}
+  outp(_BV(TWINT)|_BV(TWEN)|_BV(TWSTA),TWCR);
+  while (!(TWCR&_BV(TWINT))) {}
     
   // Contact slave  
   outp(0x02,TWDR);  // slave is 0x02 (sla+w)
-  outp(BV(TWINT)|BV(TWEN),TWCR);
-  while (!(TWCR&BV(TWINT))) {}
+  outp(_BV(TWINT)|_BV(TWEN),TWCR);
+  while (!(TWCR&_BV(TWINT))) {}
  
   // Send reference byte to slave
   outp((int8_t)u,TWDR);  // send 8 bits reference
-  outp(BV(TWINT)|BV(TWEN),TWCR);
-  while (!(TWCR&BV(TWINT))) {}
+  outp(_BV(TWINT)|_BV(TWEN),TWCR);
+  while (!(TWCR&_BV(TWINT))) {}
     
   // stop transmission
-  outp(BV(TWINT)|BV(TWEN)|BV(TWSTO),TWCR);
+  outp(_BV(TWINT)|_BV(TWEN)|_BV(TWSTO),TWCR);
 
 }
 
@@ -261,26 +273,26 @@ int main()
 
   /* Timer section */
   // Enable timer0, timer1, timer2 compare match interrupts
-  outp(BV(OCIE0)|BV(OCIE1A)|BV(OCIE2),TIMSK);
+  outp(_BV(OCIE0)|_BV(OCIE1A)|_BV(OCIE2),TIMSK);
   
   /* Timer 2, 73 kHz Prescaler 1, encoder counter for position measurement */
-  outp(BV(WGM21)|BV(CS20),TCCR2);
+  outp(_BV(WGM21)|_BV(CS20),TCCR2);
   outp(200,OCR2);
   /* Reset timer 2 */
   outp(0,TCNT2);
   
   /* Timer 1, 1 kHz , prescaler 1, ctrl-loop */
-  outp(BV(WGM12)|BV(CS10),TCCR1B);
-  outp(0x39,OCR1AH);
-  outp(0x7f,OCR1AL);
+  outp(_BV(WGM12)|_BV(CS10),TCCR1B);
+  outp(0x38,OCR1AH);
+  outp(0x3f,OCR1AL);
   outp(0,TCNT1H);
   outp(0,TCNT1L);
   
 
   /* Timer 0, 40 kHz, Prescaler 8, serial communication */
-  outp(BV(WGM01)|BV(CS01),TCCR0);
+  outp(_BV(WGM01)|_BV(CS01),TCCR0);
   //outp(184-1,OCR0); // 10 kHz
-  outp(46-1,OCR0); // 40 kHz
+  outp(45-1,OCR0); // 40 kHz
    /* Reset timer 0 */
   outp(0,TCNT0);
 
@@ -298,15 +310,15 @@ int main()
   
   
   /* AREF (AREF is 5V) pin external capacitor, ADC3 for pendulum angle */
-  outp(BV(REFS0)|BV(MUX0)|BV(MUX1),ADMUX);
+  outp(_BV(REFS0)|_BV(MUX0)|_BV(MUX1),ADMUX);
   
   // Enable ADC on adc3, start first conversion, prescaler 128, free running mode
-  outp(BV(ADEN)|BV(ADSC)|BV(ADATE)|BV(ADPS2)|BV(ADPS1)|BV(ADPS0),ADCSRA);
+  outp(_BV(ADEN)|_BV(ADSC)|_BV(ADATE)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0),ADCSRA);
 
 
   // Initialize Master TWI
   outp(0x10,TWBR);  // set SCL-frequency CPU-freq/(16+2*16)
-  outp(BV(TWEN),TWCR); // enable TWI
+  outp(_BV(TWEN),TWCR); // enable TWI
 
   // initialize position measurements
   initPos();
diff --git a/linear_pendulum_2009/avr/vel_control.h b/linear_pendulum_2009/avr/vel_control.h
index 88d833b..2d3ad6c 100644
--- a/linear_pendulum_2009/avr/vel_control.h
+++ b/linear_pendulum_2009/avr/vel_control.h
@@ -2,7 +2,8 @@
 #define __vel_control_h
 
 
-void setRef(int32_t newRef);
+void setVelRef(int32_t newVelRef);
+void setAccRef(int32_t newAccRef);
 int32_t getPosition();
 int32_t getVelocity();
 int16_t getAngle();
-- 
GitLab