Simple Maths Going Wrong


Closed Thread
Results 1 to 40 of 44

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default Vector Averaging

    Darrel,

    Thanks again for your investigation. The display 'InstruMental' is really good.

    I guess the issue of:

    1. Adding one new vector and then running the 'Averaging Tree' with 15 older vectors. Or...

    2. Waiting till 16 new vectors are available and then running the 'Averaging Tree'

    Is one of latency. And the selection should be based on:

    1. The importance of immediate results.
    2. The time interval between the availability of new vectors.
    3. The need for the MCU to do other things.

    I have attached some code that I used to send the averages to my PC with the screen presentation laid out so one can easily see, and check, the averages. The vectors come from a RANDOM function that loads up the PIC EEPROM. RS232 at 19,200 Baud on pin C.6. Loops continuosely.

    Two questions for you please:

    1. How does one post PBP code here so that it looks neat/tidy. My code looks OK on my PC but when I post it here it is a mess?

    2. Is your 'InstruMental' writtern in Visual Basic? I want to learn this and recently got the book 'VISUAL BASIC FOR ELECTRONICS ENGINEERING APPLICATIONS' by Vincent Himpe. For me (An ex-electronics engineer) it's no good - starts off too advanced. Any advice on a good 'entry level' book?

    Regards Bill Legge

    Code:
    ' -----[ Title ]-------------------------------------------------
    '
    ' File...... WVL 877 VERTOR AVERAGE
    ' Purpose... Experiment with PIC16F877A 
    ' Date...... July 2009
    '
    ' -----[ Program Description ]---------------------------
    '
    ' 1. Devices used:  PIC16F877A on EasyPIC5
    '
    ' ----[ Includes / Defines ]-----------------------------------------
    '
    define OSC 20               ' Define Xtal as 20 MHz.  Use "HS" for programming
    '
    include "modedefs.bas"      ' Include serout defines
    '
    ' -----[ Initialization ]--------------------------------------------
    '
    ADCON1	= %00001110			' A0 is analog, remainder digital
    CMCON	= %00000111			' Comparators OFF
    INTCON	= $80				' Turns off interrupts.  See PBP page 187
    TRISA	= %00000001			' A0 is A/D input
    TRISB=0 : TRISC=0 : TRISD=0 : TRISE=0                   
    PORTA=0 : PORTB=0 : PORTC=0 : PORTD=0 : PORTE=0
    '
    ' ----[ Variables ]--------------------------------------------------
    '
    X			VAR BYTE		' First vector READ
    Y			VAR byte		' Second vector READ
    Vector		var	byte[31]	' Stores 16 A/D readings of wind direction and averages
    Difference	var	byte		' Difference between vectors, always +
    Average		var	word		' Average of two vectors
    J			var	byte		' EEPROM memory address
    K			var	word
    Memory		var	byte		' Contents of EEPROM address J
    TxPin		var	PORTC.6
    Heartbeat	var	PORTC.0
    '
    '-----[ Main ]-------------------------------------------------------	                           
    '
    Main:
    
    	for J = 0 to 15									' Create 16 random numbers 1 to 255
    		random K
    		write J, K.lowbyte							' Load EEPROM with random numbers
    	next J		
    	
    	for J = 0 to 28 step 2							' Read pairs of vectors
    		read J,  X
    		read J+1,Y
    		if Y>X then swap X,Y						' X is always the larger variable
    				
    		Difference	= X-Y		
    		Average	= (Difference/2) + Y				' Half difference and add to smaller vector
    		 	
    		IF DIFFERENCE>128 then					
    		Average=Average+128							' Reverse direction of vector
    		if Average>255 then Average=Average-255		' Whole revolution? Subtract 255 if necessary
    		endif
    	
    	    write J/2 + 16,Average		
    	next J
    	
    	for J = 0 to 30									' Send to PC at 19,200 Baud
    		read J, Vector[J]
    	next J
    	serout2 TxPin,32,["VECTOR ADDITION",13,13] 
    			
    	serout2 TxPin,32,[dec3 Vector[0],9,dec3 Vector[16],9,dec3 Vector[24],9,dec3 Vector[28],9,dec3 Vector[30],13]
    	serout2 TxPin,32,[dec3 Vector[1],13]
    	serout2 TxPin,32,[dec3 Vector[2],9,dec3 Vector[17],13]
    	serout2 TxPin,32,[dec3 Vector[3],13]
    	serout2 TxPin,32,[dec3 Vector[4],9,dec3 Vector[18],9,dec3 Vector[25],13]
    	serout2 TxPin,32,[dec3 Vector[5],13]
    	serout2 TxPin,32,[dec3 Vector[6],9,dec3 Vector[19],13]
    	serout2 TxPin,32,[dec3 Vector[7],13]
    	serout2 TxPin,32,[dec3 Vector[8],9,dec3 Vector[20],9,dec3 Vector[26],9,dec3 Vector[29],13]
    	serout2 TxPin,32,[dec3 Vector[9],13]
    	serout2 TxPin,32,[dec3 Vector[10],9,dec3 Vector[21],13]
    	serout2 TxPin,32,[dec3 Vector[11],13]
    	serout2 TxPin,32,[dec3 Vector[12],9,dec3 Vector[22],9,dec3 Vector[27],13]
    	serout2 TxPin,32,[dec3 Vector[13],13]
    	serout2 TxPin,32,[dec3 Vector[14],9,dec3 Vector[23],13]
    	serout2 TxPin,32,[dec3 Vector[15],13,13,13]
    
    	goto Main	
    		
    END

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Very nice animated simulation by DT. What program are you using to create this?

    Now, in DT's posted program taking an average with "S=(S*(AvgCount-1)+Temp)/AvgCount" takes some time to converge. With sudden changes in wind direction, which is usually the case, a high sampling rate is needed to keep up with this algorithm. This is a common issue when averaging several readings.

    Robert

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


    Did you find this post helpful? Yes | No

    Default

    Bill,

    After more testing, I can say that the problem I was seeing was due to my averaging the circular buffer after each sample.

    By taking 16 samples (at high speed) then averaging, the vane can never go more than 128 unless it's crossing zero and everything works fine. If the samples are slow enough that the vane can go more than 128, the problem shows up again.

    I'm sticking with SIN/COS/ATN.<hr>

    The program I'm using is DT_InstruMental. Something I've been working on for awhile that makes it easy to display info from the PIC graphically.

    It's still in it's infancy, but the only way to figure out what I need to do with it, is to use it. ... So I'm using it.

    Here's one of my favorites so far ...

    <object id='stUkhdQ01IR11XRl9VWV1aVF5T' width='345' height='308' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name='movie' value='http://www.screentoaster.com/swf/STPlayer.swf'/><param name='allowFullScreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='flashvars' value='video=stUkhdQ01IR11XRl9VWV1aVF5T'/></object>
    DT

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default

    It is what I 've been dreaming!

    Can I have it?

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default

    NO! You can't have my KEG of beer.

    Oh, you meant the program ...

    I hope that everyone will have it someday, some kind of shareware/crippleware/demo, don't know yet.
    But right now, you would hate me for the grief you would endure.

    It really needs to be more user friendly, which is why I'm trying to use it as much as possible, so I can figure out the best way to go.

    Before anyone gets it .. It must BE a dream, not a nighmare.
    <br>
    DT

  6. #6
    Join Date
    Nov 2007
    Location
    South-West of Australia. A small town called Denmark. 'Where the forest meets the sea.'
    Posts
    136


    Did you find this post helpful? Yes | No

    Default Vectors

    DT,

    I used StampPlot from Selmaware for a PID motor control demonstration last year, thoughts on it ~ may be of some interest in your development?

    1. Fantastic that I could produce a PC interface without learning Visual Basic.

    2. I think it was free, or I paid a very small amount of $ for the improved graphic design function.

    3. The complexity of the commands was OK once you had an example to modify ~ working with the User Guide alone was, in some cases, very difficult.

    4. The supplied controls, whilst functionally good, were visually poor ~ I imagined they were modelled on instruments in a Soviet 1950 power station.

    5. I would have been prepared to pay more for a better product that had the benefit of lots of polishing.

    6. However, excellent value for the very low cost ~ a labour of love for the developer.

    Regards Bill Legge

    PS Any recomendations for a book on learn Visual Basic?

  7. #7
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Bill,

    Take a look at this software
    http://www.abacom-online.de/uk/html/...ab-expert.html

    I haven't used it yet but it looks like it would do the job. Has anybody used it?

    Robert

  8. #8
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default StampPlot with USB interface to PIC 18F4550??

    Quote Originally Posted by Bill Legge View Post
    DT,

    I used StampPlot from Selmaware for a PID motor control demonstration last year, thoughts on it ~ may be of some interest in your development?

    1. Fantastic that I could produce a PC interface without learning Visual Basic.

    2. I think it was free, or I paid a very small amount of $ for the improved graphic design function.

    3. The complexity of the commands was OK once you had an example to modify ~ working with the User Guide alone was, in some cases, very difficult.

    4. The supplied controls, whilst functionally good, were visually poor ~ I imagined they were modelled on instruments in a Soviet 1950 power station.

    5. I would have been prepared to pay more for a better product that had the benefit of lots of polishing.

    6. However, excellent value for the very low cost ~ a labour of love for the developer.

    Regards Bill Legge
    Bill, I also am trying to get an application going without having to use VB6. I need to interface a laptop computer to an 18F4550 based microcontroller via USB to read the data stored by the 18F4550 in its EEPROM over a 31 day period [93 bytes = 31 daily measurements x (date value, Data_LSB, Data_MSB) ] . I need the data to be inserted on the PC side into an Excel spreadsheet for plotting and analysis purposes by the user. It appears that StampPlot might be a candidate to do this, but it appears it really only works with BasicStamp and RS232 or serial interfaces and not USB. As a user of StampPlot can you advise me if I can adapt it to use for USB and PICBASICPro without a huge learning curve? My controller is done and my project has a short fuse so can't spend a lot of learning time to get the PC side and USB interface going Would really appreciate your advice!~
    Last edited by jellis00; - 27th May 2010 at 00:28. Reason: quote fix

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    NO! You can't have my KEG of beer.
    Of course not! If I drink half of glass thenfor the rest of the day I 'll keep singing. And trust me, I am really bad singer!
    About the software, I have no problem with the way you are going to distribute it, as long as I do not have to learn Visual $omething.

    Ioannis

  10. #10
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default DT_InstruMental

    Sorry Internet Explorer only.
    It displays fine on Firefox 3.0.12 here.
    Bo

Similar Threads

  1. Simple RF remote control code
    By Bruce in forum Code Examples
    Replies: 13
    Last Post: - 22nd January 2014, 10:45
  2. Simple Blinking LED - WTF!!
    By johnnylynx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st February 2010, 06:19
  3. Simple LCD code not working!...WHY?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th November 2009, 19:48
  4. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 01:11
  5. Doing Simple Math - getting the wrong answer
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2005, 14:27

Members who have read this thread : 0

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