Author Topic: Please check my programming... Something is wrong...  (Read 1921 times)

0 Members and 1 Guest are viewing this topic.

Offline Hasan999Topic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 1
Please check my programming... Something is wrong...
« on: January 15, 2010, 05:14:11 AM »
I'm still looking for a solution to the problem in my programming code... since a week.

My post had been deleted twice when a few users "assumed" that I am asking the same question again.
[I have not asked this question before - it is rather a "general" question about "something wrong in my programming"]

Just because it is on the same topic (i.e. "interrupts") does not mean I am asking the same question.
And due to the same reason, I cannot use my older post for a "new" and completely "different" question. It was a long thread, and I got the answer to that. and Now, I have started programming, and there's a small problem...

Code: [Select]
#include "SoR_Utils.h"

int main(void)
{
int i,j;

uartInit();  // initialize the UART (serial port)
    uartSetBaudRate(0, 38400); // set UARTE speed, for Bluetooth
    uartSetBaudRate(1, 115200);// set UARTD speed, for USB connection
    uartSetBaudRate(2, 38400); // set UARTH speed
    uartSetBaudRate(3, 38400); // set UARTJ speed, for Blackfin

rprintfInit(uart1SendByte);// initialize rprintf system and configure uart1 (USB) for rprintf

configure_ports();

timer0Init();

//initialize ISR:
UCSR1B |= (1 << RXCIE1); // Enable the USART Recieve Complete interrupt (USART_RXCn)
sei();//turn on the Global Interrupt Enable flag
int servo_position[21]={650,690,660,700,700,700,715,710,235,1130,275,1090,330,1105,270,1090,650,650,640,650,670,575};
int servo_number[21]={6,6,7,2,1,5,2,6,0,4,3,5,3,2,4,4,4,1,5,3,7,3};
int *servo_port[5]={PORTJ,PORTA,PORTE,PORTH,PORTC,PORTA};

    while (1)
    {
int isDone=0;
for (i=0;(i<21-1)&&(!isDone);i++)
{
isDone=1;
for (j=0;j<21-i-1;j++)
{
if (servo_position[j]>servo_position[j+1])
{
// swap servo position
int tmp = servo_position[j];
servo_position[j] = servo_position[j+1];
servo_position[j+1] = tmp;

// keep number and port in sync with the position
tmp = servo_number[j];
servo_number[j] = servo_number[j+1];
servo_number[j+1] = tmp;

tmp = servo_port[j];
servo_port[j] = servo_port[j+1];
servo_port[j+1] = tmp;
isDone=0;
}
}
}
//turn off interrupts .. this is very important otherwise your servo will jitter!
cli();

// set all the ports to on that are enabled (i.e.>0)
for (i=0;i<21;i++)
{
if (servo_position[i]>0)
{
PORT_ON(*servo_port[i], servo_number[i]);
}
}
// delay until the last position has occured
int offset=0;
for (i=0;i<21;i++)
{
if (servo_position[i]>0)
{
if (servo_position[i]>offset)
{
// wait until this servo's position offset
delay_cycles(servo_position[i]-offset);
offset=servo_position[i];
}
// turn off this servo and continue with next servos
PORT_OFF(*servo_port[i], servo_number[i]);
}
}

// renable interrupts so that we can receive UART/serial values
sei();

// wait for a total of 20ms from the last servo timing value.
timerPause((4666-offset)/234);
}
}


The above code is to maintain all the 22 servos in their initial positions stored in servo_position array.

The same code would work perfectly, if I control 12 or less servos ! (i.e. if I make at least 10 servo_positions to "zero", or reduce the "for loops" to 12 loops)

However, if I use more than 12 servos, then all the servos jitter !... This jitteriness is like twice every second. (as if, it gives signal to the servos, wait for like 0.5 seconds, and give another boost, and so on)

Need help...
Thanks..

PS.: I'll repeat: "I have not asked the same or even a similar question before, unless you include those threads that have been deleted for no good reason".

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: Please check my programming... Something is wrong...
« Reply #1 on: January 15, 2010, 08:29:41 AM »
Logically if your code works fine up to 12 servos and then starts to jitter you are looking for a timing issue. This was hinted at in madsci1016's reply on this thread:
Quote
...If you need to do a lot of processing at the same time, you will slow down the servo loops until you get jitter again. That is why i recommended getting an external servo controller in addition to your microcontroller.

You are not doing a lot of processing since your code disables interrupts whilst it does the servo timing routine, effectively just working on that routine.

This code
Code: [Select]
if (servo_position[i]>offset)
{
// wait until this servo's position offset
delay_cycles(servo_position[i]-offset);
offset=servo_position[i];
}

is ultimately responsible for your servo timing. What is the delay (in microseconds) resulting from the call delay_cycles(1)? It is possible it may be too large.

Also are you sure about your values for servo_position? since they vary by a factor of 4 and more from minimum to maximum. Typically you would not expect them to vary by more than a factor of 2 (1ms - 2ms) as there does not appear to be any additional time added elsewhere in code.

Apologies if this reply is borken.... preview doesn't seem to be working for me. 

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: Please check my programming... Something is wrong...
« Reply #2 on: January 15, 2010, 07:58:00 PM »
There is no 'few users' that assumed anything. The only person here with the power to delete a post is Admin, and his word is final, as it's his website. You don't agree with that, you don't have to post here anymore. Frankly, if I was Admin, your account would have been suspended after the second repost.

As far as the reason why he deleted,

This post,
http://www.societyofrobots.com/robotforum/index.php?topic=10015.0
http://www.societyofrobots.com/robotforum/index.php?topic=9952
http://www.societyofrobots.com/robotforum/index.php?topic=9901.0
http://www.societyofrobots.com/robotforum/index.php?topic=9886.0

All have to do with you running many servo's at once. The only one that is slightly different is the last one, but the rest are all about you trying to get all 20 something servos to work at once, with no jitter. They should all be in one post titled "running multiple servos without jitter". Just because you moved forward to a different issue while trying to reach that goal, doesn't mean it's a new topic, nor worthy of a new thread. And again, before you flame me, this isn't 'my opinion', this is the general consensus of everyone here, more importantly it's Admin's ruling (or else he would not have deleted them), and it's his website.

As far as actually helping you...

Most of this forum has moved onto Webbot's Lib. Not many are going to help you with the old library, as it has many weaknesses. And what you are asking is very hard to do, it takes a lot of precise timing to run a single servo, let alone 20+ servos.

This video should help explain servo control, which what i was trying to help you understand in one of your last posts about this topic. Please watch it before continuing, as i will be adding to it for what you are trying to do.

DIY USB-Controlled Servo Squirter Water Pump

Now, your application, you are going to have 20+ compare value lines, one for each of your servos. You will need a loop that checks when it is time to turn off one of the servo out pins by comparing it to the count values. The problem is, as that loop is looping, you are not doing anything else on the micro-controller. And when the loop is done, you have very little time before you need to start it again, or else the servos will "let go, free spin" and you get jitter. This is why the roborealm works ok with software PWM, it has nothing else to do BUT PWM.

I hope this guides you to correcting your code. I'm sorry if you were looking for someone to fix it for you, but that's not really what we do around here. We are here to help you understand and learn, not fix it for you. (where's the fun in that?)


« Last Edit: January 19, 2010, 08:32:47 AM by Admin »

 


Get Your Ad Here

data_list