The Wooden Menace; Robotic Arm

Questions and Comments on All Projects
ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

The Wooden Menace; Robotic Arm

Postby ThePyroElectro » Mon Nov 12, 2007 10:24 pm

The Wooden Menace Project Write-up

The Wooden Menace is a robotic arm made from hobby servos
& (you guessed it) wood! It can be autonomously or remotely controlled.



Questions & Comments?

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

HELP

Postby Engineer41 » Mon Mar 17, 2008 6:01 am

Hello everyone I need help in understanding a part of a code.



int servo0 = 0xF63B; // Servo 0
int servo1 = 0xF077; // Servo 1


these two statements just initialize the variables with certain values, I do not see how this gives the delay to the servo motors with C????


Help! I need this for my final year project! Pleaseeeeeeeee


Thank you all

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Mon Mar 17, 2008 2:22 pm

Hello everyone...


I was wondering if someone could explain to me the initialization of servo1 and servo2, because I do not see how a delay is provided so that the servo moves in a certain position. I understand the interrupts and timers, how they are generated and preloaded, rescaled that is ok. I just really don't get how the servos get their pulse length (delay)...based on what????


Thank you

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Mon Mar 17, 2008 5:15 pm

Hey Engineer41,

To answer your first question:

Code: Select all

int servo0 = 0xF63B; // Servo 0
int servo1 = 0xF077; // Servo 1


These two lines of code initialize the variables that hold the amount that the two servos should delay while the PWM signal is +5v (aka logic 1). The values put into them initiatially are just randomly chosen as they are just changed later on when the program operates.

The actual delay calculation....

If you look at the timer initialization it has a 1:2 prescalar. This means the timer counts twice as fast than normal.

Known values:

System Clock Frequency - 20MHz
Instruction Frequency - 20MHz/4 = 5 MHz

Max Timer Value (16 bit) - 0xFFFF
Set Timer Value - 0xF63B (servo 0) & 0xF077 (servo 1)
Prescalar - 2

Image

This formula is actually in the data sheet. Take a look at it if you need a better understanding of what is going on. Otherwise it's pretty straight forward.

The 5th post down here: http://forums.pyroelectro.com/viewtopic ... 3&start=15 explains this process more clearly

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Mon Mar 17, 2008 8:02 pm

Hello!


Thank you so much for answering! I am in such a mess right now with my final year project :( . I have a demonstration in two weeks and I am having trouble with my code .... Thank you so much for the explanation, I really appreciate it! I have one more question where I am totally lost, well two questions.


1. Where in the program do you bring PORT low?? If you have 5 servos and you want to bring the port high when timer 1 is set, and then bring it low at overflow, where exactly in the code do you do it?

2. I also get errors when I initialize the two timer1 registers with a different value in each switch statement :( . I am using PIC16F877A and I just initialize it like so:

TMR1H:TMR1L = some value; (0xf892 for example)

and then I do the same thing in the next switch statement and I get the same error, that I reinitialized the timer....
:?:



Thank you so much for your help! You are saving my grade :( I am at the point of desperation! :(

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Tue Mar 18, 2008 1:28 am

Hey Engineer41,

If you're using my code (or modifications) in your senior project be kind enough to mention it in your references in your project write up. :D

:arrow: (1) The servo PWM's switch in this statement:

Code: Select all

            switch(count){
         case 1:     PORTA = 0x08; // First Stage
                   WriteTimer1( servo3 );
                  break;
         case 2:      PORTA = 0x04; // Left Gripper
                  WriteTimer1( servo1 );
                  break;
         case 3:      PORTA = 0x00;
                  PORTC = 0x02; // Swivel/Rotate
                  WriteTimer1( servo4 );
                  break;
         case 4:      PORTC = 0x00;
                  PORTA = 0x02; //Right Gripper
                  WriteTimer1( servo0 );
                  break;
         case 5:      PORTA = 0x00;
                  PORTC = 0x01; // Second Stage
                  WriteTimer1( servo2 );
                  break;
         case 6:      PORTC = 0x00; //Future Servo?
                  WriteTimer1( 0 );
                  break;
            }


Case 1 turns PortA Pin 4 On & Resets the timer1.
Case 2 turns PortA Pin 4 Off, PortA Pin 3 on & Resets the timer1.
Case 3 turns PortA Pin 3 Off, PortC Pin 2 on & Resets the timer1.
Case 4 turns PoirtC Pin 2 Off, PortA Pin 1 on & Resets the timer1.
etc...
etc...

:arrow: (2) It's a good idea not to initialize stuff within the switch statement. That will give compiler errors. Do all your initializations at the top of the program like I did. Modify them in the switch statement like I do. If this doesn't answer your second question feel free to copy and paste some code & the mplab error. Please not your entire program. :shock:

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Tue Mar 18, 2008 1:36 am

Hello! THank you so much for answering again!


I am using the idea of using interrupts and switch statements that you did in your code, I will certainly put your code as a reference!

I am not using MPLAB and therefore I do not have functions such as writetimer1()....I can only give new values to registers....I am using MikroC...I will try again and tell you what happens.


Thank you once again for your explanation!!!

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

I am not understanding something

Postby Engineer41 » Thu Mar 20, 2008 1:42 am

Hello,


I am defining the code in this way, your code ...I am pasting here a part:

if(INTCON.TMR0IF) //check if TMR0 interrupt flag is set
{
//WriteTimer0( 0x3CAF );
TMR1L = 0x77;
TMR1H = 0xFC;

count = 0;
INTCON.TMR0IF = 0; //clear TMR0 flag
}
if(PIR1.TMR1IF == 1 && PIE1.TMR1IE == 1) //if set controls the first servo
{
count++;
switch(count){
case 1: PORTD = 0x01; // First Stage
//WriteTimer1( servo0 );
break;
case 2: PORTD = 0x02; // Servo 1
TMR1H:TMR1L = servo1;
break;
case 3: PORTD = 0x00;
//PORTC = 0x02; // Swivel/Rotate
TMR1H:TMR1L = servo2;
//WriteTimer1( servo2 );


I keep receiving an error that TMR1H label has been redefined:(


I don't understand why, because its the only way to initialize the timer1 in PIC16F877A ....

Its not compiling because of this ...anyone? Please help :(


Thank you all

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Thu Mar 20, 2008 2:10 am

I have a question...which might fix my problem...how do you I assign to a variable the first byte of a hex number in this form '0xf98E' ??? If I can do that I can fix the problem, cause I need to reassign the value of Timer1...


Thank you!

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

I am not understanding something

Postby Engineer41 » Thu Mar 20, 2008 2:11 am

Hello,


I am defining the code in this way, your code ...I am pasting here a part:

if(INTCON.TMR0IF) //check if TMR0 interrupt flag is set
{
//WriteTimer0( 0x3CAF );
TMR1L = 0x77;
TMR1H = 0xFC;

count = 0;
INTCON.TMR0IF = 0; //clear TMR0 flag
}
if(PIR1.TMR1IF == 1 && PIE1.TMR1IE == 1) //if set controls the first servo
{
count++;
switch(count){
case 1: PORTD = 0x01; // First Stage
//WriteTimer1( servo0 );
break;
case 2: PORTD = 0x02; // Servo 1
TMR1H:TMR1L = servo1;
break;
case 3: PORTD = 0x00;
//PORTC = 0x02; // Swivel/Rotate
TMR1H:TMR1L = servo2;
//WriteTimer1( servo2 );


I keep receiving an error that TMR1H label has been redefined:(


I don't understand why, because its the only way to initialize the timer1 in PIC16F877A ....

Its not compiling because of this ...anyone? Please help :(


Thank you all

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Thu Mar 20, 2008 2:51 pm

Hey,

I don't have much time to look over your code but my initial thought is either that you can't define TMR1H:TMR1L = servoX; inside the switch. Or you have to define the TMR1H and TMR1L registers seperately. Try to do it without the switch statement, like with if statements and see if you get the same error.

Anyway, I'll have more time later. :)

~Good luck :wink:

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Fri Mar 21, 2008 10:50 pm

Hello!


Thank you for repliying! I can't define them together, I already figure it :D...but I need to change the timers I am using with PIC16F877A...


I have a small question, is the instruction cycle the same? If my chip is 20Mhz frequency, do I perform the calculations in the same manner?


Thank you!

ThePyroElectro
PyroElectro Admin
Posts: 1181
Joined: Mon Nov 12, 2007 9:24 pm
Location: Earth
Contact:

Postby ThePyroElectro » Sun Mar 23, 2008 5:08 pm

:arrow: It looks like the PIC16F877A has a two stage pipeline which means that the actual instruction frequency would be -->
20 MHz / 2

You can double check this by making a simple delay circuit to blink an led. Make the code so it should blink once a second then just see if it does. After you got that done then you're ready to move on. :wink:

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Tue Mar 25, 2008 9:31 pm

Hello,


Does that mean all the calculations are to be made with doing the following?:

20MHz/2= 10Mhz = 10,000,000??

But my timers are only 8 bits and 16 bits...I'd have to use prescaler of 256 then:( don't I?

Engineer41
Newbie Pyro
Posts: 15
Joined: Mon Mar 17, 2008 5:58 am

Postby Engineer41 » Tue Mar 25, 2008 9:39 pm

Its a 200 ns instruction cycle...


Return to “Projects”

Who is online

Users browsing this forum: No registered users and 0 guests