Menu

converting formulas

Help
2019-09-01
2019-09-01
  • stan cartwright

    stan cartwright - 2019-09-01

    I want my robot with a sr04 range finder to make some "decisions"
    I tried to clarify with a diagram.
    The idea is the bot sees an object within a pre defined range and stops then scans left if the object was right and visa versa until another object detected.
    Noting angle between first object detected and second should give the distance apart and knowing bot width would it fit.
    Thing is have I got the code right,braces and stuff in right logical order? It asm no errors.

    #chip mega328p, 16
    #option explicit
    #INCLUDE <TRIG2PLACES.H>
    #include <maths.h>              ;required maths.h
    
    dim dist1,dist2,angle,unknown as word
    dist1=60:dist2=60:angle=60
    unknown=sqrt ( (dist1*dist1)+(dist2*dist2)-(2*(dist1*dist2*cos (angle)) ) )
    
     

    Last edit: stan cartwright 2019-09-01
  • Anobium

    Anobium - 2019-09-01

    the braces may be correct, but, break the calc into smaller calcs.

    Start with the squaring, do two calcs. Then, do other stuff in smaller calcs into temp variables then bring it all together. The chances of code being debugged is

     
  • stan cartwright

    stan cartwright - 2019-09-01

    I don't do these sort of sums on a daily basis. x2 is x squared ie x*x.I couldn't find a square function.

     
  • Anobium

    Anobium - 2019-09-01

    Correct. A square.

    In these calcs consider. Overflow of the calc if you will get confused. So, consider the maximum calcalculation possible and size your variables to the maximum.

    if the angle is a constant. Then, do not do a cos() calc use a constant.

     
    • stan cartwright

      stan cartwright - 2019-09-01

      I can understand sqr and sqr root with same prime numbers so multiplying a number times itself is not its square. I don't know what errors to exect...lot more work to do.
      I was the syntax and order gcb works arithmetic formulas.

       
  • stan cartwright

    stan cartwright - 2019-09-01

    I suppose it would make less chance of compiler error and not slow it down much

    #chip mega328p, 16
    #option explicit
    #INCLUDE <TRIG2PLACES.H>
    #include <maths.h>              ;required maths.h
    
    dim dist1,dist2,angle,unknown,dist1sqrd,dist2sqrd as word
    dim dist12sqrdadded,dist1x2,dist1x2xcos,dist1x2xcosx2 as word
    dist1=60:dist2=60:angle=60
    dist1sqrd=dist1*dist1
    dist2sqrd=dist2*dist2
    dist12sqrdadded=dist1sqrd+dist2sqrd
    dist1x2=dist1*dist2
    dist1x2xcos=dist1x2*cos (angle)
    dist1x2xcosx2=2*dist1x2xcos
    
    unknown=sqrt (dist12sqrdadded-dist1x2xcosx2)
    
    ;unknown=sqrt ( (dist1*dist1)+(dist2*dist2)-(2*(dist1*dist2*cos (angle)) ) )
    

    I guess I should try it. if the angle between objects is 60deg and the distance to each is 20cm then the unknown would be 20cm..that's easy.
    If the objects were diffrent distances then the gap would would not be accurate...it would be wider.
    It was a thought I should apply to a chassis with stepper motors for smarter bot. I have one using a i2c port controller. could turn degrees better.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.