Memsic MX2125 Accelerometer/tilt to drive PWM motors
Botdocter
Posts: 271
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.
Post Edited (Botdocter) : 3/8/2010 1:10:47 AM GMT
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
Comments
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
Post Edited (Botdocter) : 3/8/2010 2:40:04 PM GMT
The PWM.start and the code that follows is never executed.
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.
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?
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
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.
I have a hexapod that uses the basic atom pro 28, together with an ssc32 servocontroller.
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
·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
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?
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
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%
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
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
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
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!
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?
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
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: (
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
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:
Will turn the motor one way
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
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
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!
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
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!
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