| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| REMOTE  CONTROL Wıth a pic MCU | 
			 
			
				 Posted: Sat Dec 31, 2022 7:44 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Guess I have a snake robot that is composed of seven links, six motors.
 
     I want to remote control each of the motors separately. How many ways do I can make it? What is the easiest and cheapest way to apply it? | 
			 
		  | 
	
	
		  | 
	
	
		
			temtronic
 
 
  Joined: 01 Jul 2010 Posts: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat Dec 31, 2022 10:00 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Probably the cheapest would be to use 6 Hobby RC  servos for the 'joints'. Then any 18 pin PIC would control them (1 pin per servo), 2 or 3 for some sort of 'remote control' interface to a 'master' computer, aka a PC ??That leaves 2 for ICSP, and a few for LEDs or heat/light  sensors. Each body 'segment' would contain batteries for one servo, the 'head' segment containing the PIC and it's power supply.
 
The actual 'slithering' motion is  fairly easy to code. You'll also need to have to decide on the 'remote control' interface as well as the 'master' command code. | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sun Jan 01, 2023 11:52 pm     | 
				     | 
			 
			
				
  | 
			 
			
				RC servos represent the best small motors and gearboxes you can get 
 
for anything near the price. Buy slightly more expensive ones and these
 
will often have stronger final output gears and give better reliability
 
and accuracy.
 
Generally you can send one of these a single pulse, and the pulse width
 
determines where it will move to. The normal range is 1 to 2mSec to cover
 
the full range of movement. The pulse is normally repeated at an interval
 
to maintain position. The old traditional RC systems used 25Hz, and modern
 
systems go up to 400Hz. In a basic PIC system, the key is that you can 
 
use just a single CCP, send a pulse for the first servo, then send a pulse
 
to the second, third, fourth etc., and then go back and start with the
 
first again. Use a CCP interrupt to control all this. 
 
The control code then just has to work out the numbers to send for the
 
movements required, and change the values being fed to the CCP code
 
controlling the positions. 
 
Though they are Chinese, Dilwe do some of the best small servos around.
 
Buy a couple, and have a play with driving these. They are simple to
 
control and work very well. | 
			 
		  | 
	
	
		  | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Timer1 50Hz  2ms signal | 
			 
			
				 Posted: Thu Jan 12, 2023 1:35 pm     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | Code: | 	 		  #include <trial.h>
 
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
 
 
#use fast_io(b)
 
 
int i=0;
 
#int_timer1
 
void  timer1_kesme()
 
{
 
  set_timer1(65036);
 
  i++;
 
  if(i==1)
 
  {
 
  output_high(pin_b0);
 
 
  }
 
  
 
  
 
  
 
  if(i==2){
 
  output_low(pin_b0);
 
 
  }
 
  if(i==10)
 
  
 
  {i=0;}
 
//!  if(i==20){
 
//!  
 
//!  output_low(pin_b0);
 
//!  i=0;
 
//!  }
 
 
}
 
 
void main()
 
{
 
   setup_psp(PSP_DISABLED); 
 
   
 
   setup_timer_2(T2_DISABLED,0,1);
 
   setup_adc_ports(NO_ANALOGS);
 
   setup_adc(ADC_OFF);
 
   setup_CCP1(CCP_OFF);
 
   setup_CCP2(CCP_OFF);
 
   
 
   set_tris_b(0x00);
 
   output_b(0x00);
 
   
 
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
 
   set_timer1(65036);
 
   enable_interrupts(int_timer1);
 
   enable_interrupts(GLOBAL);
 
   
 
   while(1);
 
  
 
 
} | 	  
 
 
I tried to control sg90 servo motor using timer1, but did not work.I used different battery supply for motor.It requires 50Hz  1-2ms pulse.If I have not done any mistake, I approximately have these values.Any help? | 
			 
		  | 
	
	
		  | 
	
	
		
			gaugeguy
 
 
  Joined: 05 Apr 2011 Posts: 350
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jan 12, 2023 2:14 pm     | 
				     | 
			 
			
				
  | 
			 
			
				| What crystal are you using?  You are currently setting timer 1 to interrupt every 2000 instruction cycles. | 
			 
		  | 
	
	
		  | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jan 12, 2023 2:40 pm     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | gaugeguy wrote: | 	 		  | What crystal are you using?  You are currently setting timer 1 to interrupt every 2000 instruction cycles. | 	  
 
4MHz | 
			 
		  | 
	
	
		  | 
	
	
		
			temtronic
 
 
  Joined: 01 Jul 2010 Posts: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jan 12, 2023 3:04 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Which PIC ??
 
the file 'trial.h' probably contains this vital information and really for us to help we need to know what 'trial.h' contains.
 
I suspect it's 2 or 3 lines of code and should be listed in 'main()' and not in an 'included' file.
 
 
Typically a PIC like say the 16F84, a 4MHz xtal will result in 1 line of code being execute every us. | 
			 
		  | 
	
	
		  | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jan 12, 2023 3:10 pm     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | temtronic wrote: | 	 		  Which PIC ??
 
the file 'trial.h' probably contains this vital information and really for us to help we need to know what 'trial.h' contains.
 
I suspect it's 2 or 3 lines of code and should be listed in 'main()' and not in an 'included' file.
 
 
Typically a PIC like say the 16F84, a 4MHz xtal will result in 1 line of code being execute every us. | 	  
 
 
 	  | Code: | 	 		  #include <16F877A.h>
 
#device ADC=16
 
 
#FUSES NOWDT                    //No Watch Dog Timer
 
#FUSES NOBROWNOUT               //No brownout reset
 
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
 
 
#use delay(crystal=4MHz)
 
 | 	  
 
Sorry for that | 
			 
		  | 
	
	
		  | 
	
	
		
			temtronic
 
 
  Joined: 01 Jul 2010 Posts: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jan 12, 2023 3:59 pm     | 
				     | 
			 
			
				
  | 
			 
			
				hmm...
 
2nd line  of your header...
 
 
#device adc=16
 
 
The 877A only has a 10 bit ADC, not 16 bit ADC.
 
 
It does do the 1us per instruction though for a 4MHz clock(except for jumps and gotos of course....
 
 
You really should put those 4 lines into your program, otherwise you and others have to guess what clock speed you're running, though 'XT' is a clue to 4MHz or less...still doesn't SAY what the speed is. | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jan 12, 2023 7:35 pm     | 
				     | 
			 
			
				
  | 
			 
			
				| The CCS IDE creates those two files. It has done this like forever. | 
			 
		  | 
	
	
		  | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Jan 13, 2023 6:17 am     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | temtronic wrote: | 	 		  hmm...
 
2nd line  of your header...
 
 
#device adc=16
 
 
The 877A only has a 10 bit ADC, not 16 bit ADC.
 
 
It does do the 1us per instruction though for a 4MHz clock(except for jumps and gotos of course....
 
 
You really should put those 4 lines into your program, otherwise you and others have to guess what clock speed you're running, though 'XT' is a clue to 4MHz or less...still doesn't SAY what the speed is. | 	  
 
 	  | Code: | 	 		  #include <trial.h>
 
#device ADC=10
 
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
 
#use delay(crystal=4MHz)
 
#use fast_io(b)
 
 
int i=0;
 
#int_timer1
 
void  timer1_kesme()
 
{
 
  set_timer1(65036);
 
  i++;
 
  if(i==1)
 
  {
 
  output_high(pin_b0);
 
 
  }
 
  
 
  
 
  
 
  if(i==2){
 
  output_low(pin_b0);
 
 
  }
 
  if(i==10)
 
  
 
  {i=0;}
 
 
 
}
 
 
void main()
 
{
 
   setup_psp(PSP_DISABLED); 
 
   
 
   setup_timer_2(T2_DISABLED,0,1);
 
   setup_adc_ports(NO_ANALOGS);
 
   setup_adc(ADC_OFF);
 
   setup_CCP1(CCP_OFF);
 
   setup_CCP2(CCP_OFF);
 
   
 
   set_tris_b(0x00);
 
   output_b(0x00);
 
   
 
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
 
   set_timer1(65036);
 
   enable_interrupts(int_timer1);
 
   enable_interrupts(GLOBAL);
 
   
 
   while(1);
 
  
 
 
}
 
 | 	  
 
 
I completely cleaned all lines except pic name in header file, and wrote them onto program.Now I tried again and motor does not move. | 
			 
		  | 
	
	
		  | 
	
	
		
			gaugeguy
 
 
  Joined: 05 Apr 2011 Posts: 350
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Jan 13, 2023 6:56 am     | 
				     | 
			 
			
				
  | 
			 
			
				| Have you verified the signal coming out with a scope?  Do you know your chip is running? | 
			 
		  | 
	
	
		  | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Jan 13, 2023 7:45 am     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | gaugeguy wrote: | 	 		  | Have you verified the signal coming out with a scope?  Do you know your chip is running? | 	  
 
 
Unfortunately, I do not have any scope for tracking the signal, but I know it is running. | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Jan 13, 2023 8:07 am     | 
				     | 
			 
			
				
  | 
			 
			
				As posted, the code will send a 2mSec pulse. Now this is the maximum,
 
so the servo will stay right at one end of it's travel. It'll only move if
 
it has been set to a different position before this. 
 
Also obvious things. The ground of the servo supply needs to connect to
 
the PIC ground. | 
			 
		  | 
	
	
		  | 
	
	
		
			Khansokhua
 
 
  Joined: 06 Nov 2021 Posts: 92
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Jan 13, 2023 11:24 am     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | Ttelmah wrote: | 	 		  As posted, the code will send a 2mSec pulse. Now this is the maximum,
 
so the servo will stay right at one end of it's travel. It'll only move if
 
it has been set to a different position before this. 
 
Also obvious things. The ground of the servo supply needs to connect to
 
the PIC ground. | 	  
 
 
So, thank you. Now it works but it is not reliable at all. Sometimes even do not run. I tried to send just one pulse did not work. How can I reach more smooth and more accurate control using this motor? | 
			 
		  | 
	
	
		  | 
	
	
		 |