I made no changes to the initial ramp up - ramp down of the two motors so since it worked before it should still work. If both motors do not ramp up and down (a first then b) let me know.
I did find an error in the code for the b motor which I have also corrected. It is now identical to the a code except for using the b pins and the b variable instead of a.
This code works great!!!!!! except for 1 little bug.
When i tilt forward it goes forward from 100% speed to 0% speed
When i tilt Back it goes back from 0% to 100%
I have been searching for this all night and can't find it...
And then i need to adjust the speed/angle ratio. It should be up and about 100% speed at an angle of 30 degrees. But should be adjustable right? Can you tell me what parameter to change?
Yes that problem was in there the entire time, hahah. I mensioned it a few posts before too.
It's both motors. A and B. They both go forwards from 100% to 0% and backward from 0% to 100%
i didn't change anything about the last code except this;
''set up direction and pulse width for motor b
bd := 0 ''set b direction value to forward
b := MM2125.Mx ''get memsic y reading
I think I am just having a hard time understanding your explanations. Sorry about that. I have changed the program so both motors are set to the same speed. Let me know if I am right or wrong with the following guesses.
1 - It seems that you want to control both motor speeds with the "Mx" reading so I am guessing you want it to go straight forwards or backwards based on what way you tilt the memsic x axis, with level being stopped.
2 - You want to control the turning by the tilt of the "My" axis.
3 - Right now (after you change "My" to "Mx") it works for reverse but in the forward direction it runs at high speed when the memsic is close to level and slows down the more you tilt it.
@Graham, I did not realize until today that botdocter was building a balancing bot. I thought he was going to use the memsic as a hand held speed control of some type and control the speed by tilting it. I thought the My could be used as a signal to control the speed difference between the two wheels and thus control the steering.
Ok thanx to Kurt who wrote the code, i have a basic balancing code. (attached to this post)
Now i need to implement a PID or Kalman filter. I found a .spin for a basic PID setup but the explainations aren't very clear to me. Could someone maybe explain what i need to change to get it to work with my Memsic2125 acc.?
Also the basic PID file is attached to this post.
A PID controller does one job and the Kalman filter another. Perhaps we could start by helping you understand what they do and then on how it is coded and then on how to put it into your code.
The PID controller's job is to look at the difference between the current tilt angle and what you want the tilt angle to be and drive the motors to try and reduce this difference. The difference is called the error.
When the error is zero then the system (your bot) is doing what you want! Measured tilt = wanted tilt.
At the moment you have implemented a P controller, this means that the drive to the motors is Proportional to the error. This just means that the drive is equal to the error multiplied by some number. This number is called the proportional gain.
The I stands for Integral, integral means added up. If your bot was tilting close to your desired angle but not quite on it then the error would be small, this means the P part of your controller would not do much (multiply something by a small number and you get a small number). But what if you add up the error over time? You could add up the error and then multiply that by another number (the integral gain) and add that to what is sent to the motors. This means the I part will ensure the small error is reduced. This error is called the steady state error because it is normally seen when the controller has settled down and is following some fixed value.
The D part stands for Differential and this means rate of change. In other words it is all about how quickly the error is changing, sometimes you really need to act fast if you want to control something that is changing quickly. The D part is just this rate of change of the error multiplied by another number. You guessed it, the integral gain. This is also sent to the motor but notice that once the bot is stable the error will be quite constant so the D part will not do very much.
That is the basic idea of the PID controller, take another look at the code and see if it makes more sense.
The Kalman filter's job is to combine estimates of the tilt from different sensors (such as accelerometer, gyro etc) into one measurement of the tilt that is better than any one of them. You have probably noticed that the accelerometer detects vibrations and other accelerations as well as tilt. A gyro is less sensitive to these errors but the tilt angle it can produce tends to drift. Combine them and you get a much better value for tilt.
To get your bot balancing in a stable manner you can probably just use the tilt sensor but when you want your bot to move around then a gyro or similar will be helpful and at that point the Kalman filter will be helpful.
Please read what I have written and then take a look at the PID code and then say specifically what does not make sense and we can work on it. You may also want to study the code that has been written for you so you really understand it fully and again ask specific questions.
My biggest problem is that i have only the accelerometer with 2 outputs. Called mmx and mmy i just didn't know wich variables i have to rename to those values. But like you said, i will have a carefull look to see if i can manage now with your explanation.
Kwinn, Thanks, I'm a university researcher and someone who struggled at school then finally got my act together to PhD level and now still struggle at school [noparse]:)[/noparse]
Botdocter, hopefully the answer to your second post will answer the first:
Take a look at the start method:
PUB Start(Current_Addr, Set_Addr, Gain, Integral_Time, Output_Addr)
''Starts PID controller. Starts a new cog to run in.
''Current_Addr = Address of Long Variable holding actual position
''Set_Addr = Address of Long Variable holding set point
''Gain = PID Algorithm Gain, ie: large gain = large changes faster, though less precise overall
''Integral_Time = PID Algorithm Integral_Time
''Output_Addr = Address of Long Variable wich holds output of PID algorithm
So he explains what each variable is (you should read tilt when he says position) but perhaps the problem is also this address business.
In your main program you will have variables which define the current tilt, the tilt you are aiming for (set point), and the output that needs to be sent to the motors. When you launch this method the PID loop is put into another cog so you need to tell it where these variables are in hub ram so it can find them. You do this by passing the address, just like you need my address to find me. If the name of a variable is current_tilt then its address is @current_tilt.
So an example call to the PID start routine might be:
pid.Start(@current_tilt, @wanted_tilt, Gain, Integral_Time, @Output_to_motors)
In this object there is just one gain which is not really right but makes it easier to tweak, the intergral_time variable is how long you do your adding up for the integral. You can fiddle with gain and integral time and see how they change things.
You say you have mmx and mmy but only one of these changes when the robot tilts, it is that one that you should use as your current_tilt. I think looking at Kwinn's code you can do:
I keep having problems implementing the code. It gives me all kind of errors so i attached the files that i use. The main file and the PID file. It must have something to do with the way i tried to implement the code but i can't find out how i 'should' do it.
The variables you are sending to the PID loop need to be defined in the VAR section (VAR = Variables) not the CON section (CON = constant, not changing).
Also you should only use the @ on the variables that are supposed to be sent as addresses, the ones ending in _Addr.
It would help if you read what the errors were, your error was "Expected a variable" which might have made you ask, why isn't MMy a variable? Constants don't vary so they are not variables [noparse]:)[/noparse] If you don't understand the error then let us know what it is.
Graham
Post Edited (Graham Stabler) : 3/15/2010 9:56:45 AM GMT
Thank you. I will try again. I did try to understand the errors. But as said before. While trying to get the code to work i got multiple errors for different things. I just tried to get it to compile.
Anyway, i hope i get it to work with your explaination.
A little tip when you are getting multiple errors as you try things is to simplify the program, comment out things that are not needed or even make a simple version of the program just for testing, that often helps to narrow down the problem. Because I did not have the PWM object and was too lazy to download it I just commented out the PWM stuff which let me look at your error (your first error of several probably).
Most of the time solving the problem is easy, the hard part is finding it. There are many tricks you can use, always try to think of ways you can test your programs that will reveal the problem. That is the art of debugging.
Even after all the reading i have done and after a lot of tries, i came to the conclusion that i am not able to get it right.
I need some help with setting up a PID filter for my balance bot.
I tried to make it work and left the files like i thought it would be good. I hope someone can download the attached files and can put the code on the right spot for me..
Comments
One wheel is going forward from 0 to 100 and back to 0 ==>to -100 to -0
I cant find the problem...
another thing is that the other wheel doesn't do anything. It's powered but stays at 0
Any ideas?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
I did find an error in the code for the b motor which I have also corrected. It is now identical to the a code except for using the b pins and the b variable instead of a.
Post Edited (kwinn) : 3/10/2010 2:12:52 PM GMT
I changed it every time, but could that be the problem?
In a way i had the feeling that was disturbing the A motor. It hitches instead of turning.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
When i tilt forward it goes forward from 100% speed to 0% speed
When i tilt Back it goes back from 0% to 100%
I have been searching for this all night and can't find it...
And then i need to adjust the speed/angle ratio. It should be up and about 100% speed at an angle of 30 degrees. But should be adjustable right? Can you tell me what parameter to change?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Post Edited (Botdocter) : 3/10/2010 5:51:03 AM GMT
It's both motors. A and B. They both go forwards from 100% to 0% and backward from 0% to 100%
i didn't change anything about the last code except this;
It used to be MM2125.My
BTW: The spin up goes well..
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Post Edited (Botdocter) : 3/10/2010 6:10:21 AM GMT
1 - It seems that you want to control both motor speeds with the "Mx" reading so I am guessing you want it to go straight forwards or backwards based on what way you tilt the memsic x axis, with level being stopped.
2 - You want to control the turning by the tilt of the "My" axis.
3 - Right now (after you change "My" to "Mx") it works for reverse but in the forward direction it runs at high speed when the memsic is close to level and slows down the more you tilt it.
Post Edited (kwinn) : 3/10/2010 2:15:18 PM GMT
2 correct
3 correct
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Graham
Graham
Now i need to implement a PID or Kalman filter. I found a .spin for a basic PID setup but the explainations aren't very clear to me. Could someone maybe explain what i need to change to get it to work with my Memsic2125 acc.?
Also the basic PID file is attached to this post.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
The PID controller's job is to look at the difference between the current tilt angle and what you want the tilt angle to be and drive the motors to try and reduce this difference. The difference is called the error.
When the error is zero then the system (your bot) is doing what you want! Measured tilt = wanted tilt.
At the moment you have implemented a P controller, this means that the drive to the motors is Proportional to the error. This just means that the drive is equal to the error multiplied by some number. This number is called the proportional gain.
The I stands for Integral, integral means added up. If your bot was tilting close to your desired angle but not quite on it then the error would be small, this means the P part of your controller would not do much (multiply something by a small number and you get a small number). But what if you add up the error over time? You could add up the error and then multiply that by another number (the integral gain) and add that to what is sent to the motors. This means the I part will ensure the small error is reduced. This error is called the steady state error because it is normally seen when the controller has settled down and is following some fixed value.
The D part stands for Differential and this means rate of change. In other words it is all about how quickly the error is changing, sometimes you really need to act fast if you want to control something that is changing quickly. The D part is just this rate of change of the error multiplied by another number. You guessed it, the integral gain. This is also sent to the motor but notice that once the bot is stable the error will be quite constant so the D part will not do very much.
That is the basic idea of the PID controller, take another look at the code and see if it makes more sense.
The Kalman filter's job is to combine estimates of the tilt from different sensors (such as accelerometer, gyro etc) into one measurement of the tilt that is better than any one of them. You have probably noticed that the accelerometer detects vibrations and other accelerations as well as tilt. A gyro is less sensitive to these errors but the tilt angle it can produce tends to drift. Combine them and you get a much better value for tilt.
To get your bot balancing in a stable manner you can probably just use the tilt sensor but when you want your bot to move around then a gyro or similar will be helpful and at that point the Kalman filter will be helpful.
Please read what I have written and then take a look at the PID code and then say specifically what does not make sense and we can work on it. You may also want to study the code that has been written for you so you really understand it fully and again ask specific questions.
Graham
My biggest problem is that i have only the accelerometer with 2 outputs. Called mmx and mmy i just didn't know wich variables i have to rename to those values. But like you said, i will have a carefull look to see if i can manage now with your explanation.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Though still some things aren't clear. I don't know what the Adress thingy's mean and where do i get the gain from?
This is the code i need help with.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Botdocter, hopefully the answer to your second post will answer the first:
Take a look at the start method:
So he explains what each variable is (you should read tilt when he says position) but perhaps the problem is also this address business.
In your main program you will have variables which define the current tilt, the tilt you are aiming for (set point), and the output that needs to be sent to the motors. When you launch this method the PID loop is put into another cog so you need to tell it where these variables are in hub ram so it can find them. You do this by passing the address, just like you need my address to find me. If the name of a variable is current_tilt then its address is @current_tilt.
So an example call to the PID start routine might be:
In this object there is just one gain which is not really right but makes it easier to tweak, the intergral_time variable is how long you do your adding up for the integral. You can fiddle with gain and integral time and see how they change things.
You say you have mmx and mmy but only one of these changes when the robot tilts, it is that one that you should use as your current_tilt. I think looking at Kwinn's code you can do:
a := MM2125.My
and then
pid.Start(@a, etc etc
Hope this helps
Graham
I think i will collect all info that i get and make it a topic for newbies.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Please have a look. Any help is welcome!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Also you should only use the @ on the variables that are supposed to be sent as addresses, the ones ending in _Addr.
It would help if you read what the errors were, your error was "Expected a variable" which might have made you ask, why isn't MMy a variable? Constants don't vary so they are not variables [noparse]:)[/noparse] If you don't understand the error then let us know what it is.
Graham
Post Edited (Graham Stabler) : 3/15/2010 9:56:45 AM GMT
Anyway, i hope i get it to work with your explaination.
Thanks again!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!
Most of the time solving the problem is easy, the hard part is finding it. There are many tricks you can use, always try to think of ways you can test your programs that will reveal the problem. That is the art of debugging.
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!
I need some help with setting up a PID filter for my balance bot.
I tried to make it work and left the files like i thought it would be good. I hope someone can download the attached files and can put the code on the right spot for me..
Thanx in advance.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!