CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

NEED HELP with multiple servo motor
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BLDD



Joined: 24 May 2015
Posts: 8

View user's profile Send private message

NEED HELP with multiple servo motor
PostPosted: Sat May 30, 2015 3:16 pm     Reply with quote

Hi, i'm new here and new in the ccs world!
I'm doing to my mechatronic's school project an Animatronic Hand using 5 servo motors. To the position of each servo i use a glove with a flex sensor for each finger and then read the sensor signal with my adc.
But it's not my problem at the moment, i can control a servo with my finger perfectly, but i just can control one at once ... when i try to add some reads of the other adc channels in my program everything goes wrong :/ I know its a coordinate / delays program because i should respect the 20ms low output to control.
I just don't have idea how to do it. I read a lot about this, should i do the servo control with timer interrupts???

Code:

#include <main.h>
 int16 value=0;
 int16 servodelay=0;



void main()
{
   setup_adc_ports(AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF);

   while(TRUE)

   {
   set_adc_channel(0);
   delay_us(20);
     value = read_adc();
   
     servodelay = servodelay*.8+(value*2.8+250)*.2;
   
    output_high(PIN_b1);
    delay_us(servodelay);
    output_low(PIN_b1);
    delay_ms(20);   
   }

}


i really don't know how i can do it for the 5 servo motors, i need to control all at once without losing reading refresh time :/


Thank you for reading and help if you can pls (ps: I'm using pic16f877A)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 30, 2015 4:05 pm     Reply with quote

You're missing the setup_adc() line.

Also, you need to post your PIC and the PIC's oscillator frequency.

Also, the line below implies that you have external Vref voltages
connected to your PIC's Vref pins. Do you ? What voltages are they ?
Quote:
setup_adc_ports(AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF);
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat May 30, 2015 5:43 pm     Reply with quote

and... this line...

servodelay = servodelay*.8+(value*2.8+250)*.2;

won't execute as you expect.

you declared servodelay as a 16 bit integer yet use floating point math....


Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 30, 2015 6:22 pm     Reply with quote

Actually the math will work OK. CCS will promote the int16 to a float
before doing the math. Because he puts the result into an int16, the
fractional part of the floating point result will be truncated. But that's
not a big concern.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 31, 2015 5:13 am     Reply with quote

interesting as I'd thought it might not work that way and you'd have to cast to get correct result.

it'll be slow though compared to integer math and that _might_ affect performance.


Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sun May 31, 2015 10:17 am     Reply with quote

Yes.

It'd work, because there is a float in every component of the maths. If you parse along the line as the compiler would, at the first operation, there is a float on the right, so float maths will be used. Then the result is a float, so the same is true at the next operation. Similarly inside the bracket, the multiplication will be float, then since this result is float, the addition will also be float, giving a float result. etc. etc...

As you say, very slow though.
I must admit I'd probably be looking to see if I could change the scaling of the values generated (change the Vref....), so that nice binary values can be used, like /4 etc., and integer maths throughout...
BLDD



Joined: 24 May 2015
Posts: 8

View user's profile Send private message

PostPosted: Sun May 31, 2015 3:37 pm     Reply with quote

PCM programmer wrote:
You're missing the setup_adc() line.

Also, you need to post your PIC and the PIC's oscillator frequency.

Also, the line below implies that you have external Vref voltages
connected to your PIC's Vref pins. Do you ? What voltages are they ?
Quote:
setup_adc_ports(AN0_AN1_AN4_AN5_AN6_AN7_VREF_VREF);


i use a 20MHz oscillator, yes, i use 3.6V min and 4.2V max adc ref.

like i said that code works to move the servo like i want, i just can't apply this to all the 5 servos.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 31, 2015 4:28 pm     Reply with quote

Quote:
i use 3.6V min and 4.2V max adc ref.

According to the 16F877A data sheet, in this section on page 194
http://ww1.microchip.com/downloads/en/DeviceDoc/39582C.pdf
TABLE 17-14: A/D CONVERTER CHARACTERISTICS
Parameter A20 shows that the difference between Vref+ and Vref- must
be a minimum of 2.0 volts. You only have 0.6 volts (4.2 - 3.6 = 0.6).
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun May 31, 2015 4:38 pm     Reply with quote

it seems you are using hobby servos of the pulse width control type for fixed position servos and updating them with adc reads.

To make this decently "smooth" in operation you need to know what resolution of both input and output are required.
Do you know for a fact that your servos can accurately resolve 1 millisecond?

If so - then positioning on a stop to stop basis - it could require 13 or 14 bits adc resolution to match the degree of control possible with the servo.

Also a key point : you will N E V E R make working code using the delay_xx()
for more than one servo -
since you can't take a new ADC reading while stuck waiting for the next bit of useful execution.
or anything else for that matter........

I actually had to do JUST what you are attempting for an opthamological
research device and it is very doable.
BUT first you need to learn to use TIMER functions and a "state machine" to
interleave taking adc readings with timing pulses of different lengths , synchronously.
where the stop and starts are not needing to align in any way.

I'd suggest picking a master clock frequency that makes it easy to set up a system timer for continuuous 1 or 2 millisecond rollover and then poll its
I-flag at the 'top' of your tight-loop state machine.

May i assume you are a student?
If so that is the first step on a very ambitious learning curve.

here is a hint: a master clock frequency of 16.384mhz will be more useful for you.
BLDD



Joined: 24 May 2015
Posts: 8

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 3:19 pm     Reply with quote

Hello,
I'll upload in youtube a video with my servo working Very Happy
What you recommend to me? switch my pic device and do the project with the adc/pwm hardware or its possible to do with pic16f877A only using software?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Jun 01, 2015 4:23 pm     Reply with quote

Quote:

my servo working Very Happy


then i guess you are all set.
Just stick with ONE servo and you don't have to change anything......
BUT IF YOU WANT more than one at the same time - your code can not be adapted to do what you want - let alone be "smooth" in operation.
DELAY_MS() is not useful for more than one very primitive channel.
What you want to do - is not trivial and requires really good understanding of what needs to happen in the time domain to make it work.
what you have chosen is not a beginner project, IMHO......
as the code you posted makes abundantly clear.

to make it smooth - multiple servo control pins need to be programmed synchronously for overlapped timing of individual servos - ideally interleaving the ADC reads inside an iterative monitoring of individual, simultaneous servo command pulses. You need a PIC i/o pin per servo -each run within a state machine as i said earlier. doable but NOT simple to program.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 3:33 am     Reply with quote

I guess it is possible to control multiple hobby style RC servos from a PIC16F877A but it's not a project for a starting programmer.
Controlling one servo is easy as you have shown in the posted code, but for multiple servos you are hitting processing power limitations with this old processor.

One requirement you didn't mention is how many different servo positions do you want to be able to select?

Problem in creating software PWM for multiple channels is that you have to split the timing and logic into two separate functional blocks and this creates a _lot_ of overhead. The overhead reduces the number of selectable servo positions.
For example see this project in the Code Library. It uses an 48MHz PIC18 to create 8 servo outputs but for each servo can only address 64 positions. Changing the number of channels has no effect on the number of addressable positions.

I don't know what your school project's purpose is and what budget / time you have. Here are some options:
1) Copy your existing design for each motor.
2) Copy your existing design for each motor, but replace the processors by a cheaper PIC10 or PIC12.
3) Do not use software driven PWM but select a PIC model with hardware PWM for each servo. Beware of the PWM resolution. Models with 10-bit PWM will give you only about 51 addressable positions so better choose a PIC with 16-bit PWM resolution. The PIC16F1786 for example has 11 PWM channels with 16-bit resolution. Costs $2.54 when buying one piece from Digikey.com. This is the best technical solution but perhaps not feasible when the chip can not be ordered in your country.
4) Use the project from the Code Library. Note that the number of addressable positions depends on the clock speed. At 48MHz it gives 64 positions. Your PIC16F877a can be clocked up to 25MHz. Accept 32 positions or use another processor.
5) Be brave and try to squeeze multiple software PWM channels into your existing PIC16F877a design. The referenced Code Library project uses interrupts for timing. Interrupts have high overhead. Polling the timer flag instead will create more processing time. Implementation will take more time than the above options, has limitations and gives lower addressable positions but you will learn a lot and can use your existing hardware. We can help you with this but we expect you to put in some effort too.
BLDD



Joined: 24 May 2015
Posts: 8

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 11:33 am     Reply with quote

ckielstra wrote:
We can help you with this but we expect you to put in some effort too.


Very thanks, and sorry for my bad english (I'm from Portugal)
I am here not only because of my problem but also because I want to learn and evolve all my abilities!
Honestly, in my school i do not learn to program in CCS, i just started researching and using the CCS because it lets me use the Vref to my adc.

So, should i try to adapt the program in the code library to use in my project?

I chose this project because my teacher said it would be quite easy, and it was .... while I tested only one servo.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 3:36 pm     Reply with quote

Don't worry about your English. Many people here, including me, are not native English speakers.

Now we understand more of your context it is time for you to tell us more about the requirements for your project.
1) I already asked about the number of positions you want to address. How many positions you require minimum?
2) You have seen that more positions asks for more processing power. The PIC16F877A you are using is very old and not recommended for new designs. Newer models are now being used which are way more powerful, cheaper and consume less current. Is it possible to use another processor?
3) Choosing a chip with hardware PWM will make the program a lot easier but you'll learn less. What do you choose?
4) You are now using the ADC for input. Will there be other inputs or communication as well? (RS-232, buttons, display?)
BLDD



Joined: 24 May 2015
Posts: 8

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 1:26 am     Reply with quote

ckielstra wrote:

1) I already asked about the number of positions you want to address. How many positions you require minimum?
2) You have seen that more positions asks for more processing power. The PIC16F877A you are using is very old and not recommended for new designs. Newer models are now being used which are way more powerful, cheaper and consume less current. Is it possible to use another processor?
3) Choosing a chip with hardware PWM will make the program a lot easier but you'll learn less. What do you choose?
4) You are now using the ADC for input. Will there be other inputs or communication as well? (RS-232, buttons, display?)


1) I want the maximum of possible positions , as this will increase my accuracy, and i can catch more moves. but 32 positions is fine (180º/32=5,6º) if i can run it faster

2) Yes i can, i have possibility to use a dsPIC but with this dsPIC i have the number of adc and pwm what i need. To the PIC16F877A i made my own board with the inputs/outputs welded

3) What you recommend? i want to learn but i have 1 month and 10 days, it is possible to do?

4) The only input i use is the ADC (except RS-232, i can't use this function because i made my board to connect with pc via usb)

Say if you need more details, what can i do?

congrats
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group