News:

Let's find out together what makes a PIC Tick!

Main Menu

ST7735 Library

Started by joesaliba, Jan 09, 2022, 11:35 AM

Previous topic - Next topic

top204

#60
I've been too busy to update my websites, but I am going to have to try and make some time, although I cannot just drop things because I have to eat and pay bills. :-)

Below is a set of procedures that calculate fast sin, cos and tan approximations. I created it last year because I was going to add it to my raycasting 3D maze program and see which one was the most accurate and fast, but time got in the way. They may be able to help you. Please remember, they were quickly created and not very tested, so they may need some tweaks. :-)

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
' Fast trig approximation procedures
' Written by Les Johnson for the Positron8 BASIC compiler
'
    Device = 18F25K20
    Declare Xtal = 64
    Declare Float_Display_Type = Fast
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600            ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTB.6           ' Setup the pin used for USART1 TX
'
' Create some variables
'
    Dim Global_fTrigTemp_ As Float
    Dim DwordOut As Dword
    Dim FloatOut As Float

'----------------------------------------------------------------------   
Main:

    FloatOut = FastSin1(0.1)
    HRSOutLn Dec5 FloatOut
   
'----------------------------------------------------------------------   
' Integer Exponentiation by squaring
' Input     :
' Output    :
' Notes     : Easily overloaded with larger pExp values
'
Proc iPow(pBase As Dword, pExp As Byte), Dword
    Result = 1
    While pExp <> 0
        If pExp.0 = 1 Then
            Result = Result * pBase
        EndIf
        pExp = pExp >> 1
        pBase = pBase * pBase
    Wend
EndProc 


' Code for fast approximation of cos, sin, tan and inverse sin, cos and tan

'----------------------------------------------------------------------
' A fast Radian Sin approximation
' Input     : pValue holds the value to calculate Sin from (0 to PI / 2)
' Output    : Returns the Radian Sin value
' Notes     : FastSin0 is faster but less accurate than FastSin1
'
Proc FastSin0(pValue As Float), Float
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * 0.00761
    Result = Result - 0.16605
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
    Result = Result * pValue
EndProc

'----------------------------------------------------------------------
' A fast Radian Sin approximation
' Input     : pValue holds the value to calculate Sin from (0 to PI / 2)
' Output    : Returns the Radian Sin value
' Notes     : FastSin0 is faster but less accurate than FastSin1
'
Proc FastSin1(pValue As Float), pValue
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * -0.0000000239
    Result = Result + 0.0000027526
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.000198409
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.0083333315
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.1666666664
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
    Result = Result * pValue
EndProc

'----------------------------------------------------------------------
' A fast Radian Cos approximation
' Input     : pValue holds the value to calculate Cos from (0 to PI / 2)
' Output    : Returns the Radian value
' Notes     : FastCos0 is faster but less accurate than FastCos1
'
Proc FastCos0(pValue As Float), pValue
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * 0.03705
    Result = Result - 0.4967
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
EndProc

'----------------------------------------------------------------------
' A fast Radian Cos approximation
' Input     : pValue holds the value to calculate Cos from (0 to PI / 2)
' Output    : Returns the Radian value
' Notes     : FastCos0 is faster but less accurate than FastCos1
'
Proc FastCos1(pValue As Float), pValue
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * -0.0000002605
    Result = Result + 0.0000247609
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.0013888397
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.0416666418
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.4999999963
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
EndProc

'----------------------------------------------------------------------
' A fast Radian Tan approximation
' Input     : pValue holds the value to calculate Tan from (0 to PI / 4)
' Output    : Returns the Radian value
' Notes     : FastTan0 is faster but less accurate than FastTan1
'
Proc FastTan0(pValue As Float), Float
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * 0.2033
    Result = Result + 0.31755
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
    Result = Result * pValue
EndProc

'----------------------------------------------------------------------
' A fast Radian Tan approximation
' Input     : pValue holds the value to calculate Tan from (0 to PI / 4)
' Output    : Returns the Radian value
' Notes     : FastTan0 is faster but less accurate than FastTan1
'
Proc FastTan1(pValue As Float), Float
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * 0.0095168091
    Result = Result + 0.002900525
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.0245650893
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.0533740603
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.1333923995
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.3333314036
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
    Result = Result * pValue
EndProc

'----------------------------------------------------------------------
' A fast Radian ASin approximation
' Input     : pValue holds the value to calculate ASin from (0 to 1)
' Output    : Returns the Radian value
' Notes     : None
'
Proc FastASin(pValue As Float), Float
    Global_fTrigTemp_ = Sqr(1.0 - pValue)
   
    Result = Global_fTrigTemp_ * -0.0187293
    Result = Result + 0.0742610
    Result = Result * pValue
    Result = Result - 0.2121144
    Result = Result * pValue
    Result = Result + 1.5707288
    Result = 1.570796326 - (Global_fTrigTemp_ * Result)
EndProc

'----------------------------------------------------------------------
' A fast Radian ACos approximation
' Input     : pValue holds the value to calculate ACos from (0 to 1)
' Output    : Returns the Radian value
' Notes     : None
'
Proc FastACos(pValue As Float), Float
    Global_fTrigTemp_ = Sqr(1.0 - pValue)
   
    Result = Global_fTrigTemp_ * -0.0187293
    Result = Result + 0.0742610
    Result = Result * pValue
    Result = Result - 0.2121144
    Result = Result * pValue
    Result = Result + 1.5707288
    Result = Result * Global_fTrigTemp_
EndProc

'----------------------------------------------------------------------
' A fast Radian ATan approximation
' Input     : pValue holds the value to calculate ATan from (-1 to 1)
' Output    : Returns the Radian value
' Notes     : FastATan0 is faster but less accurate than FastATan1
'
Proc FastATan0(pValue As Float), Float
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * 0.0208351
    Result = Result - 0.085133
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.180141
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.3302995
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.999866
    Result = Result * pValue
EndProc

'----------------------------------------------------------------------
' A fast Radian ATan approximation
' Input     : pValue holds the value to calculate ATan from (-1 to 1)
' Output    : Returns the Radian value
' Notes     : FastATan0 is faster but less accurate than FastATan1
'
Proc FastATan1(pValue As Float), Float
    Global_fTrigTemp_ = pValue * pValue
   
    Result = Global_fTrigTemp_ * 0.0028662257
    Result = Result - 0.0161657367
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.0429096138
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.0752896400
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.1065626393
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.1420889944
    Result = Result * Global_fTrigTemp_
    Result = Result + 0.1999355085
    Result = Result * Global_fTrigTemp_
    Result = Result - 0.3333314528
    Result = Result * Global_fTrigTemp_
    Result = Result + 1.0
    Result = Result * pValue
EndProc

Pepe

I have tried the procedures and they do not work, apparently you need
more accuracy

Pepe

#62
Joesaliba could you speed up the graph?

Could you try this code on your hardware?

Dompie

Quote from: Pepe on Mar 16, 2022, 02:43 PMI have tried the procedures and they do not work, apparently you need
more accuracy
top204 said:
Quote from: top204 on Mar 16, 2022, 02:17 PM..... approximations.....
so you can't expect great accuracy.
You may need to consider moving to a PIC24 or ds33.

Johan

Pepe

I think that the spi is a bit slow method compared to the parallel connection

Pepe

Joesaliva
 
I have converted your code to use the Atomix library but drawing lines with negative values ​​still does not work

joesaliba

#66
Pepe,

Quote from: Pepe on Mar 16, 2022, 07:53 PMI have converted your code to use the Atomix library but drawing lines with negative values ​​still does not work

Yes I have seen the other post.

Is the sprite.hex for a 18F26K22?

Maybe later today, I will try all suggestions. Cannot find some free time these two days.

@Dompie I have thought of that, but to use higher frequency. Problem for me is to acquire the PIC24 / DS33.

Regards

Joe

Pepe


joesaliba

Pepe,

With the sprite.hex is only a grid?

Regards

Joe

Pepe

Thanks for testing the code, it should have shown a square that moves within the grid.

joesaliba

Quote from: Pepe on Mar 19, 2022, 11:57 AMThanks for testing the code, it should have shown a square that moves within the grid.

I can see only the grid, no square that moves within the grid

Joe

Pepe

#71
The Atomix universal library now it works well and much faster.

Pepe

#72
The simulation in proteus should not be the same as in the real hardware so you only see a grid.
This should have been shown moving

joesaliba

Wow, that is a great improvement.

Only thing is that the word Roll, `R' has some pixels missing from the top.

I tried open the .bas file but I think you included the original one.

Joe

Pepe

How can I send you the source of your code?

joesaliba

salibaj2 at gmail dot com

Pepe

Joesaliba
let me know if you have received it correctly

Pepe

Speed ​​comparison between libraries