Shop OBEX P1 Docs P2 Docs Learn Events
Voltage Measurement — Parallax Forums

Voltage Measurement

plau45plau45 Posts: 109
edited 2014-08-23 18:08 in Learn with BlocklyProp
I know how to measure voltage and I also than changed it into the -100 to 100 range, but I need to figure out how to alter the equation ud = 200*(udV-2.5)/5 so that it would scale from 0 to 1800. I went ahead and multiplied the whole thing and got a scaling from -1800 to 1800 but can't seem to center at 900. I have gotten it to 900 but when I moved my joystick it only changes the measurement by at most 2 numbers. When I use the scale from -1800 to 1800 I get it so I start at 0 and can go all the way to either 1800 or -1800 with the joystick. I need to figure this out so that I can move my servo both ways just not one. I may figure it out before you guys get back to me. Thanks.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-08-22 21:54
    What processor, board and language are you using?

    Also if you post your code by placing it between code tags [noparse]
    and
    
    [/noparse] it will show up correctly in the forum.

    If "ud = 200*(udV - 2.5) / 5" is used for a range of -100 to 100 then you'd use "ud = 360 * udV" to scale it to 0 to 1800.
  • plau45plau45 Posts: 109
    edited 2014-08-23 07:00
    #include "adcDCpropab.h"                            // Include adcDCpropab#include "simpletools.h"                            // Include simpletool
    #include "servo.h"
    
    
    
    
    int main()                                          // Main function
    {
      pause(1000);                                      // Wait 1 s for Terminal app
      adc_init(21, 20, 19, 18);                         // CS=21, SCL=20, DO=19, DI=18
    
    
      float lrV, udV;                                   // Voltage variables
      int ud, lr;
    
    
      while(1)                                          // Loop repeats indefinitely
      {
        servo_angle(16, ud);
    
    
        udV = adc_volts(2);                             // Check A/D 2                
        lrV = adc_volts(3);                             // Check A/D 3
    
    
        ud = (200*(udV-2.5)/5*20)+900;
        lr = (200*(lrV-2.5)/5*20)+900;
    
    
        putChar(HOME);                                  // Cursor -> top-left "home"
        print("Up/Down = %d %c\n", ud, CLREOL);    // Display voltage
        print("Left/Right = %d %c\n", lr, CLREOL); // Display voltage
    
    
        pause(100);                                     // Wait 1/10 s
      }  
    }
    
    I added part to this original SmpleIDE tutorial like part of the equation that equals ud and such. The *20 in that equation is to make it 1980 to -1980 instead of the original 99 to -99. I also am using a standard parallax servo with the activity board. The code is written in C.
  • plau45plau45 Posts: 109
    edited 2014-08-23 08:29
    I just did the 360 and it wouldn't drive the servo so I went to this equation. (720*udV)/2 and it gets the same output bu allows me to drive servo. Thanks for the help on this.
  • twm47099twm47099 Posts: 867
    edited 2014-08-23 08:30
    I'm not sure if I am reading your question correctly, but if I wanted to take a voltage value of 0 to 5v and change that into a servo control value of 0 to 1800 centered at 900, I would use:

    ud = 1800*udV/5
    


    Is that what you need?
    I hope I didn't misinterpret your question.

    Actually I should have asked, what is the voltage range you get from the joysticks? Is it 0 to 5v?
    Also be sure you have fully charged batteries. I almost went crazy a couple of times (at least twice a year) when I try to debug servo programs where the servo only moves a small amount and stops. It is usually because my batteries run low and the program works well without power to the servos, but when the servo is hooked up, the current draw, is too much and the prop resets. Using new batteries debugs it fine.

    Tom
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-08-23 09:38
    twm47099 wrote: »
    I'm not sure if I am reading your question correctly, but if I wanted to take a voltage value of 0 to 5v and change that into a servo control value of 0 to 1800 centered at 900, I would use:

    ud = 1800*udV/5
    


    Is that what you need?

    Am I missing some subtlety of C?

    How is "ud = 1800 * udV / 5" different than "ud = 360 * udV"?

    Don't the two equations give the same output?

    It seems like one would be better off combining the multiplication and division terms together to make the calculations in the Propeller faster.
  • twm47099twm47099 Posts: 867
    edited 2014-08-23 10:26
    Duane Degn wrote: »
    Am I missing some subtlety of C?

    How is "ud = 1800 * udV / 5" different than "ud = 360 * udV"?

    Don't the two equations give the same output?

    It seems like one would be better off combining the multiplication and division terms together to make the calculations in the Propeller faster.

    Duane,
    Your'e right. I was just showing it as a proportion. When I actually use it in C I would use one constant that was equal to calculated value of the other constants as you have done (unless it wasn't time critical and I was illustrating generally how to do something).

    Tom
  • plau45plau45 Posts: 109
    edited 2014-08-23 12:38
    You Guys answered my question. Thanks
  • plau45plau45 Posts: 109
    edited 2014-08-23 18:08
    New question. I'm using te same code but I added servo_angle(17, lr); under the servo_angle(16, ud); and hooked up duel servos to the respective ports but this will not allow me to use the servo connected to pin 17. The Servo is connected correctly it just won't move even though the value that is displayed under lr does.
Sign In or Register to comment.