Questions about gyroscopes as inputs and things for a school project.

Ruzzy

New Member
I've only used PICAXE on a VERY basic level and programming is the only part of it that I have serious past experience of (from before I used PICAXE). For a school project I want to make a robotic segway that will keep its balance when I try to push it over. To do this, I wanted to have the PICAXE control the voltages to the wheel's motors by judging it's current balance using a gyroscope(or two with an average value for accuracy).

Here are the things I'm worried about:
Can the PIC perform floating point calculation at a decent rate for this. Is it possible for a PIC to change voltage going to the motors and how. Is it possible to use a gyro to give values to a variable in the program?

Any light being shed on this would be much appreciated. Thanks in advance.
 

BeanieBots

Moderator
Wow, now there's a challenge even for an experienced person.
Good luck.

First off, the PICAXE can't do floating maths at all.
Secondly, it's a bit slow (even with integers) for that sort of task.

What sort of iteration rate would your algorythm need?
The PICAXE can do roughly one calculation every 250uS. Fast enough?

You can slow the whole system down by 'damping' whatever it is you want to balance. Picture the classic inverted pendulum with large piece of card on the 'ruler' that is being balanced. That allows a much slower algoryhtm to be used.

Any PICAXE which supports PWMout can control the speed/direction of a motor using an H-Bridge and PWM speed control.

A PICAXE can read (pulsin) the output from a "Hobby" gyro or a gyro sensor (via amplification) using ReadADC
 
Last edited:

Ruzzy

New Member
So I can't do maths with any value between 0 and 1? If that's the case, could I do the whole thing with integers and have the output voltage written in mV? And can you please link me to info on tjis ReadADC thing?
 

hippy

Ex-Staff (retired)
I think you may run into speed issues with a PICAXE but there are versions which will run at up to 20MHz, 5 times faster than the base versions, and another which should run 10 times or even faster planned. It may be fast enough but I think it would be a case of try it and see or determine the exact algorithm and predict what time it needs. You may be able to use multiple PICAXE's to share the workload around.

A good place to start would be a search engine and see what similar projects use for response rates and loop cycle timings. That will give a ball-park estimate for what you need to do and the time you have to do it in.

I recall fly-by-wire fighter jets used to have a 20ms cycle time but I have no idea how much work they need to do, or whether a 'segway' would need as fast or faster than that response. 20ms is a reasonable long time for a fast PICAXE, ~400 simple commands.

Regardless of what you use, defining its operating parameters will be important. As a school project it may be acceptable to have some wobble or a poorer performance than would be acceptable if it were actually carrying people. Self-correcting for a small knock will obviously be a goal but would you need it to recover from greater knocks ? Are wild travel motions back and forth acceptable to correct balance or are you looking for very tight minimal movement ?

Does the project have to deliver promised results, or is it acceptable as an exploratory project to see how well it can do, where learning and experimenting is more valuable than achieving the hoped for target goal ?

Is this expected to be a key educational, complicated and extensive, project or simply coursework in passing and it seemed like an interesting project to attempt ?

READADC is described in PICAXE Manual 2 ( select Help in the Programming Editor ).

The PICAXE has byte and word variables, the later can implicitly handle numbers between 0 and +65535 though you can visualise them in other ways.

As to whether an algorithm would work with integers or have the desired accuracy, that would very much depend on the algorithm itself.

There's the Micro-Mega FPU which can act as a floating point processor for a PICAXE which may be suitable if floating point is required. I have no idea off-hand what the processing speed of that or data throughput rates are.
 
Last edited:

Ruzzy

New Member
I want it to be capable of both small and large pushes to either side. This would be easily possible and much within my (and most people's) ability to have in code. Unfortunately, to get it just right, I would likely need decimal numbers in the algorithm. The graph of balance versus wheel speed ought to look like everyday parabola OR trig graph. I might get the desired effect from integers too but I would be lucky for that. This project doesn't need to be perfect, but I can have defects only with a logical explaination for such a problem in the written report. So is the PICAXE capable of decimals or can I use millivolts(microvolts would be GREAT) for the power output? By the way, this thing isn't expected to hold a person and probably won't be taller than a loaf of bread.

The calculation speed was described as 10'000 basic commands estimate according to an FAQ I read. If that's even remotely accurate, I should be able to tear up atleast 20 changes in speed in a second, and probably over a hundred. And if not, I could resort to an overclock.
 

BeanieBots

Moderator
You won't be able to have the output voltage written in mV.
Even if you could, that's not how to do it.

Don't forget, any micro controller (not just PICAXE) cannot drive a motor directly. You will some sort of motor driver. By far the easiest method of controlling a motor's speed from a digital signal is to use PWM (pulse width modulation) control. That is, switch the motor on and off very fast. The more "on" you give, the faster it goes. This can be done with a single output and a single transistor. To include direction control requires another output and an "H-Bridge" driver. You can BUY ready made H-Bridges.

How big is your device going to be?
What current will the motor require?
Do you have any idea how to do the maths? (ignoring the integer issue for now)?

Before you write the first line of code or buy the first part, you should have the problem almost solved first.

PWM motor speed control works as a percentage of full speed.
The PICAXE PWMout command has a resolution of 10-bits (1023 steps).
So, if it's a 3v motor, that would be equivalent to steps of 0.0029v.
For a 12v motor it would be steps of 0.012v
 
Last edited:

Ruzzy

New Member
Understood. For the maths part, I would use an algorithm that looks (theoretically) like "speed = (gyroangle * k +/- l)^2 +/- m" where k, l and m are unknown constants that will be set by trial and error. OR, by observation, should this not perform as well, use trigonometric ratios. Oh dear. Does PIC support that? I'm sorting out these things now and haven't purchased a single nail, and won't until I have this stuff sorted out.

Edit - that method of quick on/off should work well. And it would have 1024 steps if you included zero(totally off).
 
Last edited:

BeanieBots

Moderator
Unfortunately, PICAXE does not support trig functions. (very limited on 28X1).
The uFPU mentioned by Hippy does floats & trig but that would make the maths even slower. There might be some scope for lookups but if/then structured integer approximations will probably be enough to get things working as long as the whole system inertia is quite high.
 

Andrew Cowan

Senior Member
I've made a relativly simple one of these using a potentiometer, a weight, and two motors.

It uses the potentiometer and weight to measure angle, and drives back if it tips back, and forward if it tips forward. You need motors with quite a lot of power to straighten it up. I used a PICAXE runing at 16MHz with no problems. You couldn't tip it, though...

This link should help alot - it is where I got most of my info from. http://www.geology.smu.edu/~dpa-www/robo/nbot/


A
 

Ruzzy

New Member
Thanks a lot for the info, all.

Andrew, can I ask how the battery life worked out on nBot? And did a PICAXE still play some different role in it?

Edit - Ignore those questions, I misread your post. Thanks for the link!
 
Last edited:

Rickharris

Senior Member
I've only used PICAXE on a VERY basic level and programming is the only part of it that I have serious past experience of (from before I used PICAXE). For a school project I want to make a robotic segway that will keep its balance when I try to push it over. To do this, I wanted to have the PICAXE control the voltages to the wheel's motors by judging it's current balance using a gyroscope(or two with an average value for accuracy).

Here are the things I'm worried about:
Can the PIC perform floating point calculation at a decent rate for this. Is it possible for a PIC to change voltage going to the motors and how. Is it possible to use a gyro to give values to a variable in the program?

Any light being shed on this would be much appreciated. Thanks in advance.

http://www.geocities.com/davidvwilliamson/balance.html
 

Ruzzy

New Member
Thanks for the link.

Okay basically, if PICAXE can do multiplication AND division of integers, with or without remainders and that, this will be possible. If it takes 250 microseconds for each of those calculations then it should be fast enough at standard clock rate. So is PICAXE able to do those simple arithmetic tasks?
 

moxhamj

New Member
Absolutely. It can certainly do multiplication and division of integers.

An integer is 0 to 65535. The key is to use this full range as much as possible. So say you want to measure something, then divide it by a value:

You read a value 0-5V with readadc10, which gives a value 0-1023. You then multiply that value by 64 which uses up the full integer range. Then you divide by another integer. That way you minimise errors. Finally, you can scale the value to millivolts or whatever, but try to keep values using the full integer range through each calculation. And long formulas would be split into a number of lines of code. And watch for overrange on each of those steps.

Sometimes I use this technique even when floating point is available (eg good old MBASIC), because integer maths is a lot faster than floating point maths.

And you can always run it through a spreadsheet with cells representing each line of code, and then get a feel for how errors accumulate through each step.
 
Last edited:

hippy

Ex-Staff (retired)
if PICAXE can do multiplication AND division of integers, with or without remainders and that, this will be possible.
PICAXE can do 16-bit multiplication, division and remainder, and also larger than 16-bit if work is put into achieving that.

With the right program code, the PICAXE can manipulate arbitrary-sized floating point numbers but perhaps not at the speed required by this application.

If it takes 250 microseconds for each of those calculations then it should be fast enough at standard clock rate. So is PICAXE able to do those simple arithmetic tasks?
That 250us is a ballpark figure for simple commands at 4MHz operation. Mathematical operations may take longer and will become longer the more complicated the calculations are.

It is not possible to say how long any particular PICAXE command will take exactly as that depends on a number of factors; where the code is in memory, what operations are performed, whether using constants or variables and of what value. Previous discussion on PICAXE instruction timing can be found using Forum Search.
 

jglenn

Senior Member
There was a sim project in Nuts and Volts, or Servo magazine here in the USA.

It was a pretty big challenge. Have to have the feedback loops tuned properly.
Stability. There are pictures of our President Bush falling off a Segway. Not that it was the machines fault. No shoes involved.
 
Top