Shop OBEX P1 Docs P2 Docs Learn Events
Memsic MX2125 Accelerometer/tilt to drive PWM motors — Parallax Forums

Memsic MX2125 Accelerometer/tilt to drive PWM motors

BotdocterBotdocter Posts: 271
edited 2010-03-20 15:18 in Propeller 1
I'm trying to get the motors to turn in relation to the output of the accelerometer. I can't seem to get it to work. It gives me a dutycycle error, and i'm stuck. i posted the code below.

Can anyone help me out? I want to use this to make my bot balance.


CON     ''General Constants for Propeller Setup
  _CLKMODE = XTAL1 + PLL16X
  _XINFREQ = 5_000_000

  MMx = 17
  MMy = 18
  
OBJ     ''Setup Object references that make this demo work
    Ser         : "FullDuplexSerial"
    MM2125      : "memsic2125"
    PWM         : "PWM_32_v2"


PUB Main_Program | a,b

    Ser.start(31, 30, 0, 9600)      '' Initialize serial communication to the PC

    MM2125.start(MMx, MMy)               '' Initialize Mx2125

    repeat
      a := MM2125.Mx            
      b := MM2125.My

      ser.dec(a)
      ser.tx(9)
      ser.dec(b)
      ser.tx(13)
      
PUB DEMO_Example | DutyCycle
''-------- This Block Starts the PWM Object ----------

    PWM.Start                   '' Initialize PWM cog

    ''-------- This Block creates a speed up/down Motor test on Pin24 -----------
    repeat
      repeat DutyCycle from 0 to 100
        PWM.Duty(24,DutyCycle,(a/100))       '' Ramp Duty cycle up from 0 to 100               
        repeat 10000
      repeat 1000000                     '' Hold at 100% for a little bit
        
      repeat DutyCycle from 100 to 0
        PWM.Duty(26,DutyCycle,(b/100))       '' Ramp Duty cycle down from 100 to 0
        repeat 10000
      repeat 1000000                     '' Hold at 0% for a little bit

Post Edited (Botdocter) : 3/8/2010 1:10:47 AM GMT
«1

Comments

  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-08 02:36
    In the code you show the PWM code will never be run. The main_program function will be run and then go in to the repeat loop where it will update a and b.

    You need to put PWM.start in main_program and adjust the duty using PWM.Duty in the loop where the angles are measured.

    I have no idea what a duty cycle error is.

    Graham
  • BotdocterBotdocter Posts: 271
    edited 2010-03-08 14:35
    Can you please show me alittle example of the explanation below? I don't really understand what you mean.

    Adjust the duty using PWM.Duty in the loop where the angles are measured.
    

    Post Edited (Botdocter) : 3/8/2010 2:40:04 PM GMT
  • kwinnkwinn Posts: 8,697
    edited 2010-03-08 15:11
    Your code is only executing this part of the program. It does the Ser.Start and MM2125.start once then sits in the repeat loop ever after.
    The PWM.start and the code that follows is never executed.

    PUB Main_Program | a,b
    
        Ser.start(31, 30, 0, 9600)      '' Initialize serial communication to the PC
    
        MM2125.start(MMx, MMy)               '' Initialize Mx2125
    
        repeat
          a := MM2125.Mx            
          b := MM2125.My
    
          ser.dec(a)
          ser.tx(9)
          ser.dec(b)
          ser.tx(13)
    
    
  • kwinnkwinn Posts: 8,697
    edited 2010-03-08 16:43
    This should do what you want. It ramps the motor on pin a up to 100%, pauses briefly, then ramps back down to 0%, then repeats it for the motor on pin b. Once that is done it reads the memsic and outputs a duty cycle on a and b proportional to the reading of x and y.

    You will probably have to change the value of "maxmemsic" from the 255 to whatever the maximum value the memsic driver can output. I do not have a memsic to test with so I am not sure what it would be. You have a memsic and a calibrated 1G available so finding that calibration value should not be too hard.
  • BotdocterBotdocter Posts: 271
    edited 2010-03-08 18:39
    Thanx! I will try the code tonight.

    At least now i have a basic balancing code to work with. I couldn't find anything.

    Again thanks!

    Btw: do you know where the 'Tout' pin is for on the accelerometer?
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-08 18:45
    Botdocter said...
    Can you please show me alittle example of the explanation below? I don't really understand what you mean.

    Adjust the duty using PWM.Duty in the loop where the angles are measured.
    


    Exactly what Kwin does in his code: PWM.Duty(apin,(a*100/maxmemsic), period)

    PWM.Duty sets the duty, a is loaded with the angle so now the duty is proportional to the angle.

    Have you done much other propeller coding?

    Graham
  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2010-03-08 18:55
    From the datasheet·linked on the parallax store:

    TOUT – This pin is the buffered output of the temperature
    sensor.· The analog voltage at TOUT is an indication of the
    die temperature.· This voltage is useful as a differential
    measurement of temperature from ambient and not as an
    absolute measurement of temperature.··

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out the Propeller Wiki·and contribute if you can.
  • kwinnkwinn Posts: 8,697
    edited 2010-03-08 19:25
    @Graham, was your question aimed at Botdoctor or me?
  • BotdocterBotdocter Posts: 271
    edited 2010-03-08 20:23
    In any case, i did not use the propeller before.

    I have a hexapod that uses the basic atom pro 28, together with an ssc32 servocontroller.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-08 20:35
    kwinn, I was showing that your code does what I was suggesting in my first post and I was asking Botdocter if he had programmed much with the propeller before, I know you have.

    Botdocter,

    I think you should definitely practise doing some more basic propeller programming, that way you will be able to glue together the objects that are available to you more easily and you can repeat the success you have had with your hexapod with your balance bot. If you don't understand the basics there is no way you will be able to pull it off, I asked you if you had used the propeller much because it was clear from your code that you did not really understand what it was doing. The more you understand the easier it will be.

    Graham
  • hover1hover1 Posts: 1,929
    edited 2010-03-08 20:41
    Balancing robots can be quite a change. I would suggest you get the book:

    ·http://www.parallax.com/Store/Books/Propeller/tabid/171/CategoryID/45/List/0/Level/a/ProductID/637/Default.aspx?SortField=ProductName%2cProductName

    In Chapter 6, Hanno describes the problems and successes he had while developing his balancing robot.

    You should also read up on PID and Fuzzy Logic. This is key in the control of the robot.

    Jim
  • BotdocterBotdocter Posts: 271
    edited 2010-03-08 22:36
    @kwinn:

    the code worked perfect!

    Only now i have to find this out;

    the output from the sensor is in balance (horizontal), the value is up and about 400000. When i tilt back 90 degrees it is 300000, and to the front 500000 max.

    How can i translate this in forward and backward motor turning?
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-08 22:53
    How are you driving your motors?
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-08 22:59
    Hover1,

    I bought the book and I have read that chapter and to be perfectly honest I didn't find it very helpful (yep sometime soon I also want to make a robot balance). Despite having some understanding of the concepts involved (kalman/fuzzylogic/PID) I couldn't understand the descriptions very well (the fuzzy logic diagram was meaningless), the code was also not very well commented IMHO. I'd still recommend the book but I was disappointed by that chapter. That said I do understand the challenge fitting complex subjects in to a small space.

    Graham
  • kwinnkwinn Posts: 8,697
    edited 2010-03-08 23:16
    How you do that would depend on what kind of motors you have and how you are driving them. Provide as much detail as possible.

    In general if you want to go from 0% to 100% PWM using the range of readings you get from the memsic you could do the following calculation:

    Subtract the minimum reading from the maximum reading to get the range - 500,000 - 300,000 = 200,000 - this is the maximum range value

    Subtract the minimum value from your current reading (lets say current reading is 450,000) - 450,000 - 300,000 = 150,000 - this is current value

    Now your pulse width value would be (150,000 / 200,000) * 100 = 75 - so your pulse width modulation would be 75%
  • hover1hover1 Posts: 1,929
    edited 2010-03-08 23:58
    Sorry Graham,
    The reply was directed to BotDoctor.
    I am trying to get the parts that Hanno used in the book. I would like to recreate it based on Chapter 6. It will take about 2 months based on my schedule, (packing and moving 1000 miles, unpacking). Of course the computer is the last to go. [noparse]:)[/noparse]
    Jim
    Graham Stabler said...
    Hover1,

    I bought the book and I have read that chapter and to be perfectly honest I didn't find it very helpful (yep sometime soon I also want to make a robot balance). Despite having some understanding of the concepts involved (kalman/fuzzylogic/PID) I couldn't understand the descriptions very well (the fuzzy logic diagram was meaningless), the code was also not very well commented IMHO. I'd still recommend the book but I was disappointed by that chapter. That said I do understand the challenge fitting complex subjects in to a small space.

    Graham
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-09 00:06
    I realised that I just wanted to say that it might not be much help unless he just copies and pastes to the code.

    Graham
  • BotdocterBotdocter Posts: 271
    edited 2010-03-09 00:29
    I use geared DC motors similair to the stingray.

    Driven by the internal H-bridge from the Propeller robot controll board (msr1). Also used on the stingray.

    Post Edited (Botdocter) : 3/9/2010 2:12:46 AM GMT
  • hover1hover1 Posts: 1,929
    edited 2010-03-09 00:56
    That's right.

    I just went through that with someone. Copy...paste..why doesn't it work !?!?!?

    Understanding of the concept and physics first, understanding of the microcontroller and software to program it, and then understanding·how to put it all together.

    Not something I would try and put together for a weekend project.

    Jim


    Graham Stabler said...
    I realised that I just wanted to say that it might not be much help unless he just copies and pastes to the code.

    Graham
  • kwinnkwinn Posts: 8,697
    edited 2010-03-09 03:43
    I have made the changes to the program based on the memsic values you gave me so the motors should go from 0 to 100% speed now. I have also added some comments to help you understand the program.
  • BotdocterBotdocter Posts: 271
    edited 2010-03-09 04:14
    This code woks very nice. Much smoother than the former version. Thank you so much.

    I still can't seem to figure out how to turn the motors the other way when the 400_000 bariere is broken.
    Now they only turn one way.

    Shouldn't i use a pulse width like 1500 instead of the percentage. you probably know better then me, but when i used servos, 1500 is standing still in the middle. 900 left turning 2000 right turning
    I found some nice code in the dancebot code too. It only uses another sensor. and i don't know what to use and what not to. i attached the main file with the interestng code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • kwinnkwinn Posts: 8,697
    edited 2010-03-09 04:30
    I think you may have left out some critical information. Are these continuous rotation servo motors? Please post or PM ALL the details including all the part numbers, manufacturer the driver chip/board, etc.

    Is the motor supposed to go in one direction with memsic inputs from 300,000 to 400,000 and the other way for 400,000 to 500,000?

    Is there a pin on the motor controller to reverse direction?
  • BotdocterBotdocter Posts: 271
    edited 2010-03-09 05:13
    i use this prop board with H-bridge: www.parallax.com/Store/Microcontrollers/PropellerDevelopmentBoards/tabid/514/CategoryID/73/List/0/Level/a/ProductID/584/Default.aspx?SortField=ProductName%2cProductName
    It is possible to run the motor in the opposite direction. I did it. I just don't know if that is done with a certain pin.

    And these are my motors

    227544_BB_00_FB.EPS.jpg
    Features:

    Motor idling speed 6000 rpm
    Motor load speed 4650 rpm
    Motor torque 35 g/cm
    Motor idling current consumption approx. 80 mA (without gearbox)
    Motor current consumption under load approx. 300 mA
    Motor diameter 35 mm
    Gearbox diameter 37 mm
    Efficiency 66 %.

    Technical data

    Dimensions: (
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-09 10:28
    The first thing, if your memsic gives 400_000 when the bot is vertical then you should look at the difference between the measured value and 400_000

    difference = measured_tilt - 400_000

    If your measured tilt was 400_001 then the result would be one. If your measured tilt was 400_399 then the difference would be -1.

    You can use the absolute value (make it positive) to set the duty cycle to the motors and you can use the sign of the difference to set the direction of the motors. You can use an if statement

    if diff < 0
      send the pwm for motor in one direction
    else
      send the pwm for motor in the other direction
    
    



    On page 4, table 1 of the robot controller it shows how to control the motors. Considering just the left motor for a second then you would send PWM to pin 24 for reverse while holding pin 25 low and you would send PWM to pin 25 for forward while making pin 24 low.

    To make this a little easier:

    PWM.Duty(apin, a, period) 
    PWM.Duty(apin+1, 0, period)
    
    



    Will turn the motor one way

    PWM.Duty(apin, 0, period) 
    PWM.Duty(apin+1, a, period)
    
    



    Will make it turn the other.

    But please spend sometime learning the objects you are using, it will help so much, just trying to build a balancing bot straight away will lead to Kwinn doing most of the programming and you will miss out on lots of fun and learning.

    Graham
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-09 10:36
    Botdocter said...

    Shouldn't i use a pulse width like 1500 instead of the percentage. you probably know better then me, but when i used servos, 1500 is standing still in the middle. 900 left turning 2000 right turning

    A servo has special circuitry to understand the pulses that were being sent, in that case the PWM sends information, the servo looks at the difference between the pulse length sent and 1500us, if it is greater it turns one way if smaller the other, the deflection is proportional to the difference of if it is continuous rotation the speed is.

    With an h-bridge you are in control of the four switches that make up the bridge, these control the direction the current passes through the motor, the PWM this time controls the amount of power put through the motor. The PWM has a regular frequency and during the time period it will turn on for a certain percentage of the time period. If it turns on for 100% then all of the supply power passes to the motor, if it turns on for 0% of the time then none of the power goes to the motor. If it is on for half the time and off for half the time then half of the power goes to the motor, so it lets you control the power delivered to the motor.

    Graham
  • BotdocterBotdocter Posts: 271
    edited 2010-03-09 17:22
    thank you for the explanation!

    I do a lot with the code. I only ask when i really can't find any info about it on the web. I am dislectic and have difficulties with reading large amounds of words. Reading books is impossible for me. On the other hand, reading the code seems to be much easyer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • BotdocterBotdocter Posts: 271
    edited 2010-03-09 19:43
    this is very clear and i won't have much trouble implementing it. Thank you so much!

    Can you tell me how to make the angle/pwm co-relation tighter?
    I mean, i want it to move faster with less angle. For example; the motor has to be at 100% with an angle of 45 degrees instead of the 180 degrees it uses now.
    I tried changing different values but couldn't find the right one i guess.
    Graham Stabler said...
    The first thing, if your memsic gives 400_000 when the bot is vertical then you should look at the difference between the measured value and 400_000

    difference = measured_tilt - 400_000

    If your measured tilt was 400_001 then the result would be one. If your measured tilt was 400_399 then the difference would be -1.

    You can use the absolute value (make it positive) to set the duty cycle to the motors and you can use the sign of the difference to set the direction of the motors. You can use an if statement

    if diff < 0
      send the pwm for motor in one direction
    else
      send the pwm for motor in the other direction
    
    



    On page 4, table 1 of the robot controller it shows how to control the motors. Considering just the left motor for a second then you would send PWM to pin 24 for reverse while holding pin 25 low and you would send PWM to pin 25 for forward while making pin 24 low.

    To make this a little easier:

    PWM.Duty(apin, a, period) 
    PWM.Duty(apin+1, 0, period)
    
    



    Will turn the motor one way

    PWM.Duty(apin, 0, period) 
    PWM.Duty(apin+1, a, period)
    
    



    Will make it turn the other.

    But please spend sometime learning the objects you are using, it will help so much, just trying to build a balancing bot straight away will lead to Kwinn doing most of the programming and you will miss out on lots of fun and learning.

    Graham
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • kwinnkwinn Posts: 8,697
    edited 2010-03-09 19:50
    Botdocter, after reading your reply to my post, as well as the later posts by Graham and yourself I now see what you are trying to do. The program I sent should only require a few changes to do what you want. Graham has covered the essentials, so if you need any more help let me know.
  • BotdocterBotdocter Posts: 271
    edited 2010-03-09 20:30
    I do i do... Hahah

    im breaking my head over these 4 lines.
    Can you tell me where i should fit them in?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • kwinnkwinn Posts: 8,697
    edited 2010-03-09 23:18
    Here is the revised program. After looking at the robot control board data sheet I took a slightly different approach from what Graham suggested. It was easier to work into my existing code this way and allowed me to send the commands to both motors as close to the same time as possible.

    It also outputs F or R for direction to the serial port along with the pulse width percentage. Let me know how it works.

    Post Edited (kwinn) : 3/10/2010 2:13:31 PM GMT
Sign In or Register to comment.