Programming Hexy in c with Atmel Studio6

Hello,

I´m not a friend of python so i decided to programm my hexy with atmel studio6 in c. Maybe someone has also fun by programming the hexy like this, so i will give you my files with some explanations. I will do my best with this but english is not my home language.

Please notice:

  • by wrong handling the programm you can destroy your servos (i will tell more later)
  • if you flash your ATMega32U4 with this, you can not use the programms of the developers anymore. I think you can reverse it but I dont know how …
  • please read this all carefully
  • you have to install Atmel Studio6 ( atmel.com/microsite/atmel_studio6/ )
  • you need a AVRISP programmer ( I use this one, just google: Atmel AVRISP mkII In-System Programmer)
  • i will not answer questions to my assembler code. just so far:

it runs with the 8 bit timer interrupt ( TIMER0 ) witch jumps in the interrupt 3 times in a servo cycle:

  1. start a 400microsecond high on all 32 servo outputs
  2. pushes precalculated highs and lows in the shift registers
  3. calculate the next servo push
    In step 3, 800byte of data are precalculated for the next servopush. 200 for each shift register. i actually use 60 of the 200, but i think about more optimising the programm and will keep this. So there are 60 positions you can go with your servos in 180 degrees, so you can move them in 3 degrees steps.

Here we are at the problem how you can destroy your servos: If you go over the servo range (by me its 0 - 60) the servo trys to do this and will break (im thinking about fixing this, but im just to lacy). my advice is, to plug out all servos an do the first tests with only one spare servo.

Programming: You will have to handle with 2 variables. one to adjust the servos, the second to move the servos. By the way, you can plug the servos in any connector on the board, just feel free, you will have to find out what number he is, of your own.

variable1: servokor[15] = 1;
the number of the servo, you will find in the [], it goes from 0 to 31, it depends on where you have plug in your servos on board
the adjust is 1, do not use 0 or negative numbers for this

variable2: servopos[15] = 30;
the number of the servo is the same as at the servokor
here you can use the 0 up to the maximum amplitude of your servo (test this!)

So here we go: Open you atmel studio6, choose a c programm with the ATMega32U4 prozessor. copy this in:

//----------------------------------------------------------------------------------------

#include “hexifunctions.h”

int main(void)
{
servoload();

while(1)
{

}

}

//----------------------------------------------------------------------------------------

Add an include file to the project and rename it to hexifunctions.h
put this in here:

//----------------------------------------------------------------------------------------

#define F_CPU 16000000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile uint8_t servopos[32];
volatile uint8_t servokor[32];
volatile uint8_t servofinal[32];
volatile uint8_t savedata[800];

void long_delay(uint16_t ms) {

for(; ms>0; ms--) _delay_ms(1);

}

void servoload(void) {
// MUST BE MINIMUM 1

servokor[0] = 1;
servokor[1] = 1;
servokor[2] = 1;
servokor[3] = 1;
servokor[4] = 1;
servokor[5] = 1;
servokor[6] = 1;
servokor[7] = 1;
servokor[8] = 1;
servokor[9] = 1;
servokor[10] = 1;
servokor[11] = 1;
servokor[12] = 1;
servokor[13] = 1;
servokor[14] = 1;
servokor[15] = 1;
servokor[16] = 1;
servokor[17] = 1;
servokor[18] = 1;
servokor[19] = 1;
servokor[20] = 1;
servokor[21] = 1;
servokor[22] = 1;
servokor[23] = 1;
servokor[24] = 1;
servokor[25] = 1;
servokor[26] = 1;
servokor[27] = 1;
servokor[28] = 1;
servokor[29] = 1;
servokor[30] = 1;
servokor[31] = 1;

// prefill
for (int i=0; i<32; i++)
{
	servopos[i] = 30;

}

hexapod_init();
sei();

}

//----------------------------------------------------------------------------------------

add an assembler file to your project ond put in this:

//----------------------------------------------------------------------------------------

#define _SFR_ASM_COMPAT1
#define __SFR_OFFSET 0
#include <avr/io.h>
.data
timer123: .BYTE 1
.extern servopos
.extern servokor
.extern servofinal
.extern savedata
.text
.global hexapod_init, TIMER0_OVF_vect
hexapod_init:
sbi DDRB,2
cbi PORTB,2
sbi DDRB,1
cbi PORTB,1
ldi r18, 0b11110000
out DDRF, r18
ldi r18, 0b1
sts TIMSK0, r18
ldi r18, 0b011
out TCCR0B, r18
ldi r18, 0b00000001
sts timer123, r18
ret
TIMER0_OVF_vect:
push r18
push r19
push r20
push r21
push r22
push ZL
push ZH
push YL
push YH
push XL
push XH
lds r18, timer123
SBRC R18,0
rjmp Timer1
SBRC R18,1
rjmp Timer2
SBRC R18,2
rjmp Timer3
Timer1:
ldi r19, 0b011
out TCCR0B, r19
ldi r19, 0b10010110
out TCNT0, r19
lsl r18
sts timer123, r18
sbi PORTB,2
ldi r18, 0b00001000
pushall:
sbi PORTB,1
cbi PORTB,1
dec r18
brne pushall
ldi r18, 0b11110000
out PORTF,r18
ldi r18, 0b00000000
out PORTF,r18
sbi PORTB,2
pop XH
pop XL
pop YH
pop YL
pop ZH
pop ZL
pop r22
pop r21
pop r20
pop r19
pop r18
reti
Timer2:
ldi r19, 0b100
out TCCR0B, r19
ldi r19, 0b10000011
out TCNT0, r19
ldi r21, 0b11001000
ldi ZH, hi8(savedata)
ldi ZL, lo8(savedata)
ld r18, Z
noout:
ldi r20, 0b00001000
cbi PORTB,2
loadbanks:
ldi r19, 0b00001000
bank_byteloop:
lsl r18
brcc writepush
sbi PORTB,2
writepush:
sbi PORTB,1
cbi PORTB,1
cbi PORTB,2
dec r19
brne bank_byteloop
lsl r20
out PORTF,r20
adiw ZL, 1
ld r18,z
sbrs r20,7
rjmp loadbanks
cbi PORTF, 7
dec r21
brne noout
ldi r18, 0b00000100
sts timer123, r18
pop XH
pop XL
pop YH
pop YL
pop ZH
pop ZL
pop r22
pop r21
pop r20
pop r19
pop r18
reti
Timer3:
ldi r19, 0b101
out TCCR0B, r19
ldi r19, 0b00111000
out TCNT0, r19
ldi r18, 0b00000001
sts timer123, r18
ldi ZH, hi8(servopos)
ldi ZL, lo8(servopos)
ldi YH, hi8(servokor)
ldi YL, lo8(servokor)
ldi XH, hi8(servofinal)
ldi XL, lo8(servofinal)
ldi r20, 0b00100000
zweiundreissigerloop:
ld r18, Z
ld r19, Y
add r18, r19
st X, r18
adiw ZL, 1
adiw YL, 1
adiw XL, 1
dec r20
brne zweiundreissigerloop
ldi YH, hi8(savedata)
ldi YL, lo8(savedata)
ldi r18, 0b11001000
zweihunderterloop:
ldi ZH, hi8(servofinal)
ldi ZL, lo8(servofinal)
ldi r20, 0b00000100
viererloop:
ldi r22, 0b00000000
ldi r19, 0b00001000
achterloop:
lsl r22
ld r21, Z
dec r21
brne onebit
rjmp nobit
onebit:
inc r22
rjmp nextbit
nobit:
inc r21
rjmp nextbit
nextbit:
st Z, r21
adiw ZL, 1
dec r19
brne achterloop
st Y, r22
adiw YL, 1
dec r20
brne viererloop
dec r18
brne zweihunderterloop
pop XH
pop XL
pop YH
pop YL
pop ZH
pop ZL
pop r22
pop r21
pop r20
pop r19
pop r18
reti

//----------------------------------------------------------------------------------------

build it and burn it on your hexy. i hope i did not forget something.

greetings, arcanix

Hello,

i´m just working on a shorter ASM code. You can still use the “old” one in my first post. There will be no different for you. The main changing is that the interrupt will only be called once a time in 20 milliseconds. Its already done, but i have to test and optimize it.

I also will ad a variable what you have to check before you do an ultrasonic scan. This is for the reason that the scan will not be interrupted by the servo-interrupt.

At the end i also will ad an ultrasonic scan function what you can use easy.

I also have a question, maybe someone has an idea:
I want to make the hexy “feel” at the end of the feeds. I think about a taster at the end of the feeds like this:


(sorry for this bad retouching)

the problem is that hexy will lose grip and i don´t have an idea how to fix it. Someone has an idea?

Greetings, arcanix

Hello,

someone interested in my new code? If not, than i can save me this time for it :slight_smile:

Greetings, Arcanix

Sure m8 post any code you want to share. im sure ppl will find a use for it.

Hello,

i´m on holiday so i dont spend much time on my computer. But i want to post my first test of the US-sensor. You can see the blue box on the left side which i push more far away from the sensor and back to the sensor. You can also see that sometomes a wrong number appears on the display, so i would recommend to check the distance twice before working with it:

youtu.be/HZ81Q3iZTkw

i will post the code for it in a few days or so.

Have fun & cu.

Arcanix

p.s. Sorry for the cracy musik, it just was running on my radio :slight_smile: