by VaulB » Fri Jun 12, 2009 2:20 pm
by VaulB
Fri Jun 12, 2009 2:20 pm
Ok, here is the code. Some things are written in finnish
mittaus means measurement and ajotus means timing. Kaatui mean fell down and jatka means continue. analin mean analog in. Now you know some finnish
. I wrote this code when i was really tired so it's not the best. I think the transfer from analin to mittaus is unnecessary and maybe the delays too(i just put them there to give adc conversion some time). There is probably a better way to shift the measuring data.
#include "C:\PIC\JEEJEE\main.h"
int16 mittaus1;
int16 mittaus2;
int16 mittaus3;
int16 mittaus4;
int16 mittaus5;
int16 mittaus6;
int16 mittaus7;
int16 mittaus8;
int16 ajotus;
int16 analin;
void main()
{
setup_adc(ADC_CLOCK_INTERNAL); // ADC clock
setup_adc_ports(pin_a0);
set_adc_channel(0); // Select RA0
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// TODO: USER CODE!!
#use delay(clock=4M)
while (1)
{ kaatui:
analin = read_adc();
mittaus1 = analin;
delay_us(4);
analin = read_adc();
mittaus2 = analin;
delay_us(4);
analin = read_adc();
mittaus3 = analin;
delay_us(4);
analin = read_adc();
mittaus4 = analin;
delay_us(4);
analin = read_adc();
mittaus5 = analin;
delay_us(4);
analin = read_adc();
mittaus6 = analin;
delay_us(4);
analin = read_adc();
mittaus7 = analin;
delay_us(4);analin = read_adc();
mittaus8 = analin;
delay_us(4);
ajotus = (mittaus1+mittaus2+mittaus3+mittaus4+mittaus5+mittaus6+mittaus7+mittaus8)/8;
goto jatka;
for(;;)
{
mittaus8 = mittaus7;
mittaus7 = mittaus6;
mittaus6 = mittaus5;
mittaus5 = mittaus4;
mittaus4 = mittaus3;
mittaus3 = mittaus2;
mittaus2 = mittaus1;
analin = read_adc();
mittaus1 = analin;;
ajotus = 1*((mittaus1+mittaus2+mittaus3+mittaus4+mittaus5+mittaus6+mittaus7+mittaus8)/8);
if (ajotus>820)
{ for (count1 = 0; count1 < 7; count1++)
{
output_high(pin_c1);
delay_us(650);
output_low(pin_c1);
delay_ms(20);
output_high(pin_c2);
delay_us(850);
output_low(pin_c2);
delay_ms(20);
}
goto kaatui;
}
if (ajotus<260)
{ for (count1 = 0; count1 <7; count1++)
{
output_high(pin_c2);
delay_us(2100);
output_low(pin_c2);
delay_ms(20);
output_high(pin_c1);
delay_us(2100);
output_low(pin_c1);
delay_ms(20);
}
goto kaatui;
}
else
jatka:
output_high(pin_c2);
output_high(pin_c1);
delay_us(1950-ajotus);
output_low(pin_c2);
output_low(pin_c1);
delay_ms(20);
}
}
}
Right now I am having a headache with the feet sensors. There are now couple of microswitches between the servo and foot in both feet. Problem is that the robot is not heavy enough to press down the microswitches in every position. It's crucial for balancing to know if your feet are on the ground. How would one walk if he didn't know if hes feet were on the ground or not
Ok, here is the code. Some things are written in finnish
mittaus means measurement and ajotus means timing. Kaatui mean fell down and jatka means continue. analin mean analog in. Now you know some finnish
. I wrote this code when i was really tired so it's not the best. I think the transfer from analin to mittaus is unnecessary and maybe the delays too(i just put them there to give adc conversion some time). There is probably a better way to shift the measuring data.
#include "C:\PIC\JEEJEE\main.h"
int16 mittaus1;
int16 mittaus2;
int16 mittaus3;
int16 mittaus4;
int16 mittaus5;
int16 mittaus6;
int16 mittaus7;
int16 mittaus8;
int16 ajotus;
int16 analin;
void main()
{
setup_adc(ADC_CLOCK_INTERNAL); // ADC clock
setup_adc_ports(pin_a0);
set_adc_channel(0); // Select RA0
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// TODO: USER CODE!!
#use delay(clock=4M)
while (1)
{ kaatui:
analin = read_adc();
mittaus1 = analin;
delay_us(4);
analin = read_adc();
mittaus2 = analin;
delay_us(4);
analin = read_adc();
mittaus3 = analin;
delay_us(4);
analin = read_adc();
mittaus4 = analin;
delay_us(4);
analin = read_adc();
mittaus5 = analin;
delay_us(4);
analin = read_adc();
mittaus6 = analin;
delay_us(4);
analin = read_adc();
mittaus7 = analin;
delay_us(4);analin = read_adc();
mittaus8 = analin;
delay_us(4);
ajotus = (mittaus1+mittaus2+mittaus3+mittaus4+mittaus5+mittaus6+mittaus7+mittaus8)/8;
goto jatka;
for(;;)
{
mittaus8 = mittaus7;
mittaus7 = mittaus6;
mittaus6 = mittaus5;
mittaus5 = mittaus4;
mittaus4 = mittaus3;
mittaus3 = mittaus2;
mittaus2 = mittaus1;
analin = read_adc();
mittaus1 = analin;;
ajotus = 1*((mittaus1+mittaus2+mittaus3+mittaus4+mittaus5+mittaus6+mittaus7+mittaus8)/8);
if (ajotus>820)
{ for (count1 = 0; count1 < 7; count1++)
{
output_high(pin_c1);
delay_us(650);
output_low(pin_c1);
delay_ms(20);
output_high(pin_c2);
delay_us(850);
output_low(pin_c2);
delay_ms(20);
}
goto kaatui;
}
if (ajotus<260)
{ for (count1 = 0; count1 <7; count1++)
{
output_high(pin_c2);
delay_us(2100);
output_low(pin_c2);
delay_ms(20);
output_high(pin_c1);
delay_us(2100);
output_low(pin_c1);
delay_ms(20);
}
goto kaatui;
}
else
jatka:
output_high(pin_c2);
output_high(pin_c1);
delay_us(1950-ajotus);
output_low(pin_c2);
output_low(pin_c1);
delay_ms(20);
}
}
}
Right now I am having a headache with the feet sensors. There are now couple of microswitches between the servo and foot in both feet. Problem is that the robot is not heavy enough to press down the microswitches in every position. It's crucial for balancing to know if your feet are on the ground. How would one walk if he didn't know if hes feet were on the ground or not