Cordic trig assembly code for PIC18f


Closed Thread
Results 1 to 40 of 55

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Smile TRIG for non-integer angles using a PIC16F684?

    Hey, nerds! I'm relatively new to PICs, and right now I'm using the PIC16F684.

    I'm trying to write a program for a mobile robot with two stepper motors for differential steering - to navigate it along a predefined path (or function). In order to do this, I need to be able to calculate trig values using Assembly code (yikes!). Not only that, but due to the physical geometry of my robot, I need to calculate the sine and cosine of multiples of 0.72 (non-integers).

    Will this TRIG.INC file work with non-integers? If not, is there a way to get around this (DP shifting)?

    Thanks for your help!
    Samuel Aaron Ward
    [email protected]
    (740) 357-8016 cell

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Hello Samuel,

    Can you explain the significance of the .72, or maybe what you are calculating. I put on my nerd glasses, but it's not helping. There might not enough resolution in the PIC16 cordic version, but then, I do not have a grasp of what you are measuring, so maybe it would be fine.

    The PIC16 cordic is here http://www.picbasic.co.uk/forum/showthread.php?t=7502

    Thanks,
    Walter
    http://www.scalerobotics.com

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    scalerobotics, the .72 probably represents 7.2 degrees per step for the motor... Stepper motors usually come in 1.8 and 7.2 degrees per step...

    Dave Purola,
    N8NTA

  4. #4
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Thanks Dave!

    In that case, the PIC16 should work for you Samuel. But it does need some modification to enable you to find the angle throughout 360 degrees. The PIC16 cordic only does 0 to 90 degrees. But the rest can be done pretty easily with PicBasic. I think it handles around 2000 iterations per second, but I may have been doing a few more equations when I measured that.

    Here is some code that works on a Pic16F877a:
    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : Walter Dunckel                                    *
    '*  Notice  : Copyright (c) 2009 Scale Robotics Inc.            *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/7/2009                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    DEFINE __16F877A 1				; list directive to define processor
    DEFINE OSC 20                    ' Define Oscillator Speed                    
    DEFINE LOADER_USED 0
    include "trig-pic16.inc"   
    
    ' Shutdown comparators
    ADCON1 = 7                        ; Turn of all A/D
    CMCON  = 7
    ' Set the port directions
    ' 0=output 1=input
    TRISA=%00000011                         ' Set PORTA  0-1-3 used for AD
    TRISB=%11011100                         ' Set PortB
    TRISC=%10000000  
    TRISD=%00000000                       ' Set PortC USART RX as an input
    porta.2 = 0 
    porta.3 = 0
    
    x           var     word
    y           var     word
    z           var     word
    i           var     word
    lon_dif     var     word
    lat_dif     var     word
    angle       var     word
    angle_deg   var     word
    distance    var     word
    j           var     word
    
    ;---------[change these to match your hardware]------------------------------
    DEFINE LCD_DREG PORTD             ; Set LCD Data port                B C   D
    DEFINE LCD_DBIT 0                 ; Set starting Data bit (0 or 4)   4 0
    DEFINE LCD_RSREG PORTA            ; Set LCD Register Select port     A A
    DEFINE LCD_RSBIT 3                ; Set LCD Register Select bit      3 2
    DEFINE LCD_EREG PORTA             ; Set LCD Enable port              A A
    DEFINE LCD_EBIT 1                 ; Set LCD Enable bit               1 5
    DEFINE LCD_BITS 4                 ; Set LCD bus size (4 or 8 bits)   4 4
    DEFINE LCD_LINES 2                ; Set number of lines on LCD       2 2
    '----------------------------------------------------------------------------
    clear
    lcdout $FE,1
    ' to convert degrees to radians Z_ * 256 / 90
    ' to convert radians to degrees Z_ * 90 / 256
    Start:
    x = 0
    y = 0
    z = 0  
    
    Z_ = 90                    'put angle in Z_ in degrees
    Z_ = (Z_ * 255)/90     'convert degrees to radians
    Call sincos                 'perform sin and cos on Z_ value
                                   'cos(Z_) = X_ :sin(Z_) = Y_
    
    lcdout $FE,1,"cos Z=",#X_                                          
    lcdout $FE,$C0,"sin Z=",#Y_ 
    end
    And the pic16 trig file: http://sites.picbasic.net/media/uhp2...trig_pic16.txt
    http://www.scalerobotics.com

  5. #5
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Nice work, but I am also challenged with maths.
    Will this allow me to do point rotation?

    Say I have an origin x=0 & y=0, and another point x=4, y=4.
    Can I find out the angle of the second coordinates where the first set is the origin,
    and then rotate the second point say 180 degrees around the origin ?
    Art.

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Uh-oh.. just including the line:
    include "trig-pic16.inc"
    causes this:

    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 213 : [235] Opcode Expected Instead of 'dt'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 216 : [235] Opcode Expected Instead of 'dt'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 219 : [235] Opcode Expected Instead of 'dt'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 222 : [235] Opcode Expected Instead of 'dt'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 237 : [226] Numeric Constant or Symbol Name Expected
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 239 : [226] Numeric Constant or Symbol Name Expected
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 269 : [235] Opcode Expected Instead of 'movfw'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 283 : [235] Opcode Expected Instead of 'movfw'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 301 : [235] Opcode Expected Instead of 'movfw'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 306 : [235] Opcode Expected Instead of 'movfw'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 316 : [235] Opcode Expected Instead of 'movfw'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 321 : [235] Opcode Expected Instead of 'movfw'
    Error C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 349 : [235] Opcode Expected Instead of 'movfw'
    Warn C:\PROGRA~1\MPLAB\PROJECT\SCROLL.ASM 934 : [102] Code Crosses Boundary @ 800h

    I'm using a 16F877 too.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Use MPASM for the assembler.
    <br>
    DT

  8. #8
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Art View Post
    Nice work, but I am also challenged with maths.
    Will this allow me to do point rotation?

    Say I have an origin x=0 & y=0, and another point x=4, y=4.
    Can I find out the angle of the second coordinates where the first set is the origin,
    and then rotate the second point say 180 degrees around the origin ?
    Art.
    Hi Art,

    You can definitely find the angle in the first part of your question. As far as rotation, I have to think a little more. Should be possible though.

    Are you rotating one of your LED displays? If so, are we talking full rotation, or just 90, 180, 270 degrees?

  9. #9
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Ideally, to potentially be able to do full rotation of a pixel.
    Yes, I have a matrix LED display in mind
    I wouldn't care whether or not it is equally divided into 360 degrees.
    I already have made routines that make it easy to draw to the screen.

    I can do this in C on the Sony PSP like this
    Code:
    xx = 240;
    yy = 30;
    x = xx - 240;
    y = yy - 136;
    
    theta = rotation;
    thetab = 360 - theta;
    thetab = (3.141592 / 180) * theta;
    
    xnew = cos(thetab)*x - sin(thetab)*y;
    ynew = sin(thetab)*x + cos(thetab)*y;
    
    xnew = xnew + 240;
    ynew = ynew + 136;
    Where x-240, y-136 are coordinates for the centre of the screen,
    x-240,y-30 is the arbitrary point I want to rotate (the one given is a point directly above the centre of the screen),
    rotation is the real angle 0-359 (0 can also be 360).
    xnew and ynew are the new (rotated) coordinates.
    I don't know if this works with microcontrollers even if you do use a C compiler.

Similar Threads

  1. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 22:31
  2. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 9th December 2008, 00:40
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 09:26
  4. 4 Chanel Dmx512 ready assembly code to PBP ?
    By syscoder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 22nd March 2007, 00:55
  5. Your Suggestions: Assembly code material?
    By dw_picbasic in forum General
    Replies: 1
    Last Post: - 2nd February 2007, 18:33

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts