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 support@ccsinfo.com

GSM control of two servo motors
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
samuel



Joined: 04 May 2016
Posts: 14

View user's profile Send private message

GSM control of two servo motors
PostPosted: Sat May 07, 2016 10:21 am     Reply with quote

Hello to everyone in the forum!
I am currently working on a project to control two servo motors using SMS.
I am using pic16f877a.
I would please need some directives.
thanks
temtronic



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

View user's profile Send private message

PostPosted: Sat May 07, 2016 4:34 pm     Reply with quote

OK, some quick ideas...

1) do not use the 16F877A. It's virtually obsolete and newer PICs are 100% pin compatible, have more memory, peripherals, speed and are CHEAPER !

2) break down the project into small provable sections.

a) assuming 'servos' means a generic Radio Control 'servo'. Get the PIC to control ONE of them. Easy, tons of code here and there.
b) Now get the PIC to control the 2nd servo. Real easy, clone code from 'a' !

SMS is another matter. You'll have to decide upon an SMS 'module'. Now since 99% of all 'modules' are 3 volt device you should (must) choose a 3 volt PIC. Either use a PICxxLFyyyy or one that IS rated for 3 volt operation. Simply read the 'electrical specs' in the datasheets. There's always an easy to read chart of frequency vs voltage. It's just one of the 500 pages that you NEED to read.
If you have to use a 5 volt PIC be sure to use correct 'logic level' translations for all the PIC <-> SMS interfacing. Get ONE wrong and poof, 'magic smoke' kills the SMS device or 'it just doesn't work'.

You'll also need to design some sort of 'command interpreter' again 'assuming' the PIC receives an SMS message, it'll have to decode and take appropriate action. If it got a string of say '+++S1M14' that could mean to Move Servo #1 to position 14.

There are 100's of 'minor' details like this that must be worked out.

Just do NOT think that a simulator like Proteus will actually work for you. Read sticky PIC101. Get real hardware and get real experience in the real world.


Jay
samuel



Joined: 04 May 2016
Posts: 14

View user's profile Send private message

PostPosted: Fri May 13, 2016 4:51 am     Reply with quote

Hello! I am trying to write a function to control two servo upon receiving a message from the handset using SMS. The function should receive the angle and rotate the servo to that angle.
I am really new to this programming please i will need your out. This is a code of what i which to accomplish. Please correct me and elaborate better on this, i wish to learn.
Thanks
Code:

void Initalise_pwm() {
 
  PORTC = 0x0;                   // set PORTC to $FF
  TRISC = 0;                     // designate PORTC pins as output
  PWM1_Init(5000);                // initialize PWM1 module
}

void rotate_pwm( int angle) {
  initialise_pwm();
 
 
  PWM1_Start();                   // start PWM1
  PWM2_Start();                   // start PWM1
 
  while (1) {                                  // endless loop
    if (angle==0)  { 
      pwm1_set_duty_percent(20);    //on for 20% of the period

 
    if (angle=90)                       
      pwm_set_duty_percent(50);   // on for 50 percent

   
     
    if (angle==180) {             
      pwm_set_duty(100);        // on through out the periond
    }
    Delay_ms(200);               // slow down change pace a little
  }
}
temtronic



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

View user's profile Send private message

PostPosted: Fri May 13, 2016 5:03 am     Reply with quote

That 'chunk of code' is not CCS C and no where even complete.
I suggest you look at the servo example CCS supplies in the 'examples' folder.
First though , get a basic 1Hz LED program to work
2nd, get a basic 'Hello PC' program to run.
3rd, get the CCS servo program to run.
4th, get an SMS program to run.
5th, combine all of the above and you'll almost have your project done.
This should take you 1-2 months of 'burning the candle' (work long hours).
Programming PICs is not as easy as learning to fly an airplane but CCS has made it fairly easy with all the examples, the FAQs and this forum!

Jay
samuel



Joined: 04 May 2016
Posts: 14

View user's profile Send private message

PostPosted: Mon May 16, 2016 7:12 am     Reply with quote

thank you very much temtronic for your reply. really i admit i still have much to do.
thanks again for taking time to guide me on what to do. i will welcome many more directives from you
temtronic



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

View user's profile Send private message

PostPosted: Mon May 16, 2016 8:58 am     Reply with quote

As all of use 'old guys' know, you have to start small, get ONE piece of the puzzle working, build upon your mistakes. While most here were taught, I never was. While I know maybe 15-20 different computer languages the C I know has come from CCS PCM C starting in V2.543 ish. It is very powerful but it's the syntax that trips me up ! Also having a half dead finger means I can't touch type, sigh...so I have lots of 'typos'.

One trick I have learned is to cut code/build/test. To make changes COPY source to a new file. NOW make changes, build/test. Every time more than a couple changes are made, I make a NEW source file (test001.c, test002.c, etc.) This allows me to go back 1,2,40 versions to where things 'used to work'! PCs have huge hard drives so space isn't a problem. When your proram works fine THEN you can erase the previous versions but keep say the last 2 or 3 (just in case !).

Jay
samuel



Joined: 04 May 2016
Posts: 14

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 4:43 am     Reply with quote

Please look at this code and tell me what to do.
I am doing simulations of this on proteus. I need to control the rotation of two servos to go forth and back 180 degrees at opposite direction. However this code turns the servos 90 degrees from position of reference. But after it gets to the 90 degrees it does not reverse its direction.
Please i need help!
Code:

#include <16F877.H>
#fuses  NOWDT, NOPROTECT
#use delay(clock=50000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#USE PWM(OUTPUT=PIN_C1, OUTPUT=PIN_C2, FREQUENCY=50Hz)
#define NUM_PULSES  10

//======================================
void main()
{
int16 count;
int8 period=249;

setup_ccp1(CCP_PWM); //configures CCP1 as a PWM
setup_ccp2(CCP_PWM); //configures CCP2 as a PWM
setup_timer_2(T2_DIV_BY_1, period, 1); //T2_DIV_BY_1 IS THE PRESCALER, PERIOD OF OUR PM

count = 0;

while(TRUE)
   {
   set_pwm1_duty(249);  //turn right
   set_pwm2_duty(110); //turn left
   delay_ms(2000);
   set_pwm1_duty(110);  //turn right
   set_pwm2_duty(249); //turn left
   }
}
temtronic



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

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 5:11 am     Reply with quote

OK, I do NOT ususlay help anyone with 'simulations', especially Proteus/ISIS, as they are BROKEN, DO NOT WORK PROPERLY, ETC. but...
this line of code ...
#use delay(clock=50000)
I have a probem with !

It says you're using a 50KHZ xtal and 2 caps as the 'clock' for the PIC. My 'gut' feeling this is probably a mistyping. 50KHz xtals are ,well rare, I suppose you might have meant to type 5000000 or 5M ,meaning 5MHz. though even that value is 'rare' or uncommon.
If you have a choice of xtals use 4MHz as it gives the PIC a 1us instruction time making it 'easy' math to calculate timings like PWM, etc. There's also charts in the datasheet already done for you !

Also you do need to get a real PIC and parts and build the project.You also need use of an oscilloscope to SEE what's happening. My old 20MHz analog scope has paid for itself many times in the past 3 decades! It's not new or digital, and I'm sure they're inexpensive these days! There's NO better teacher than the 'Real World', especially since Proteus is NOT a good simulator!

Jay
samuel



Joined: 04 May 2016
Posts: 14

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 8:13 am     Reply with quote

Thank you for reply, please this is my worry a servo works at 50 Hz and so taking the 8MHz requires the PR2 register to be 39999. And from what i have read the PRE register is an 8 bit implying 256 max. Also the PRESCALER can scale upto 16 (2499 which still exceeds the 256).
Please i need help.
jeremiah



Joined: 20 Jul 2010
Posts: 1329

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 9:08 am     Reply with quote

In that scenario, instead of using the hardware PWM, I would just setup the timer to be divide by 32 or 64 (using the prescaler and postscaler) and use an interrupt service routine for the timer to control the I/O line. Keep a counter going in the timer isr and when it has enough counts, toggle the I/O line.

It doesn't have to usec accurate at 50Hz periods.
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Fri Jun 03, 2016 10:39 am     Reply with quote

if this was my project - i would use a PIC with the NCO option such as 16f1509 or 16f1719 . The NCO would generate the PWM base frequency with NO PIC overhead and then have 4 channels of 12 bit resolution OUTPUT using the Mitsubishi IC M66242P/FP....i would NEVER EVER commit the whole PIC to what IMHO is a hardware function.

http://pdf.datasheetcatalog.com/datasheet/MitsubishiElectricCorporation/mXyzuwuy.pdf
Ttelmah



Joined: 11 Mar 2010
Posts: 19370

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 11:01 am     Reply with quote

samuel wrote:
Thank you for reply, please this is my worry a servo works at 50 Hz and so taking the 8MHz requires the PR2 register to be 39999. And from what i have read the PRE register is an 8 bit implying 256 max. Also the PRESCALER can scale upto 16 (2499 which still exceeds the 256).
Please i need help.


Actually most servo's don't 'work at 50Hz'. All they care about is the 'on' period of the pulse to them. 50Hz, comes from the older radio receivers, which sent a pulse 'train' carrying all the servo pulses 'one after the other', with a longer 'marker gap' to show the end of the train, and then started again. The receiver detected the long gap, and reset a counter. Then the first received pulse was sent to the first servo, the second to the second etc.. The total 'frame', gave a typical frame time of about 20 to 25mSec, so 40 to 50Hz.
Old servo's, if they didn't see a pulse for a long time, would 'creep', so you can't take the repeat time below perhaps 5Hz. Modern servo's though don't do this, and will stay where they were set, without pulses for several seconds or more.
Then modern digital receivers, to give much faster responses, will send the pulses faster, and servos are now required to support pulse repeat rates up to 400Hz. It is the pulse width that sets the angle. 1 to 2mSec.

So it is actually perfectly legal to use much faster pulse trains than 50Hz.

Use a PIC like the 16F1578. This gives a 16bit PWM versus the normal 10bit, and this has up to an /128 prescaler. So you can program a 50Hz repeat if you want or go for a faster repeat, and it is all done for you in the hardware, with zero CPU overhead. Makes the job easier, and the result better (much more accurate PWM).

Save yourself work.
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Fri Jun 03, 2016 1:55 pm     Reply with quote

So this is NOT any kind of "pure play" PWM at all!!

This is about pulse -duration hobby servos ???

If that's the case and you are using modern digital hobby servos - I did a project for a scientific instrument using such and
found that basically if i sent the servo ONE program position pulse-

no refresh was ever needed. The persistent belief that it must be constantly refreshed dates to ancient history in the RC area, when the position was held by analog means and a capacitor which would discharge-- and hence had to be "refreshed" frequently to be stable.
So let me cut to the chase.

WHO MAKES YOUR SERVO and what is the part number please ??
Ttelmah



Joined: 11 Mar 2010
Posts: 19370

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 2:48 pm     Reply with quote

As I said:

"Modern servo's though don't do this, and will stay where they were set, without pulses for several seconds or more. "

It is very much an error (probably perpetuated in things like Proteus....), to think that you have to keep sending pulses.

ESC's will generally still drift a little if not given a pulse for a minute or more, so it is ideal to just send a refresh once every few seconds to 'be sure'.
gjs_rsdi



Joined: 06 Feb 2006
Posts: 468
Location: Bali

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 03, 2016 3:39 pm     Reply with quote

Ttelmah is right, asmboy is right.
But in the market you still have a lot of analog servos, cheaper than the digital and some people prefer using them as they believe are less prone to EMI/RFI.
See: http://hitecrcd.com/files/2013_HRU_ServoMat_2.pdf
Those servos needs pulse update every 20ms. If not, they are losing from the rated torque.
Regarding PWM accuracy, many want 10 bit or 16 bit.
In my opinion and experience, 8 bit is enough for a model aircraft or drone.
If the servo moves +/- 45 degree at 8 bit you have 0.35 degree per bit.
Usually you are reducing it mechanically to +/- 25 degree or even less so
is 0.175 degree per bit. I can't see 0.175 degree visually, but my eyes are not so good Smile
At 10 bit resolution +/- 45 degree will be 0.0875 degree.
Regarding generating the period and pulse, use a timer set to interrupt every 20 ms and write a small routine to generate the pulses (as jeremiah has written)

Best wishes
Joe
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