Controlling Esc's for quadrotor

Madtom

Member
I am trying to control 2 brush-less motors with an 18m2+

I think the inputs may be out of the range required but they are limited to that so it may just be the mixing part
also think my limiters may not be working

I cant see why this wont work it is just getting an 18m2+ to mix two inputs for a motor output.
the first motor is always going full speed and the second gives me bad signal beeps (which I suspect is just telling me the throttle is to low)
any suggestions here's the code

View attachment t1 q v1 chp t.bas
 

MPep

Senior Member
I think the problem is in this bit of code
Code:
let b1 = b7 - b5 + 150  'normal mixing to make throttle increase when tilting left
let b2 = b7 + b5 - 150    'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way
Imagine, if you will, that B7 = 100, and B5 = 200. In the first statement you'll end up with a negative number (which PICAXE doesn't handle)!!!
You'll first need to evaluate which of the 2 is larger then chose an appropriate equation.

You'd need to add something like this:
Code:
if b7 >=b5 then goto One:
if b7 <=b5 then goto Two:


One:    
let b1 = b7 - b5 + 150  'normal mixing to make throttle increase when tilting left
goto CarryOn              

Two:
let b1 = b5 - b7 + 150  'Variables are reversed here!!
goto CarryOn

let b2 = b7 + b5 - 150    'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way
'Addition of course doesn't matter!!!  :-)
 
Last edited:

jedynakiewicz

Senior Member
The syntax for pulsin is (manual 2 page 160):
PULSIN pin, state, wordvariable

and continues; "It is normal to use a word variable with this command."

You appear to not to be using a word variable. Also, the word variable receives the result between 1 and 65535. Are you sure that you pulse happens to be of the appropriate length for your calculation?
 

MPep

Senior Member
Seeing as RC system signals are generally (someone is bound to correct me here!) between 1.0ms and 2.0ms a word variable will be fine. I hadn't even spotted that one! Oops. ;)
 

Madtom

Member
@Mpep

I think you've miss interpreted what I am trying to do

with your if statements if I send the control to turn left(increasing the amount of pulse's) it will increase but it will also increase if I turn right.

the let b2 statement is for another motor.

switching the variables gets the same number as reversing the + - without sending it into a negative. so the problem of it going negative I will try it

so the new code will look like this

init:
servo b.7,b1
servo b.6,b2

main:
pulsin c.6,1,b7 'throttle
pulsin c.7,1,b6 'yaw <----ignore this it does nothing at the moment
pulsin c.0,1,b5 'lean
if b7 < 100 then
let b7 = 100
endif
if b7 > 200 then
let b7 = 200
endif
if b5 < 100 then
let b5 = 100
endif
if b5 > 200 then
let b5 = 200
endif

let b1 = b5 + b7 - 150 'normal mixing to make throttle increase when tilting left
let b2 = b7 + b5 - 150 'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way

if b1 < 100 then
let b1 = 100 'limiters to keep esc in same throttle range as controller
endif
if b1 > 200 then
let b1 = 200
endif
if b2 < 100 then
let b2 = 100
endif
if b2 > 200 then
let b2 = 200
endif

servopos b.7,b1
servopos b.6,b2

goto main



@jedy

it will always be contained between 75 and 225 as they are the servo limits. i know mine goes from 104 to 213 or something very close to that so I don't need the 8 bit word variable just the 4 bit byte variable
 

Madtom

Member
Still getting the same result 1 maxed the other on nothing

Maybe it can only do one at a time?

Does the second servopos command overwrite th first
 

SAborn

Senior Member
i know mine goes from 104 to 213 or something very close to that so I don't need the 8 bit word variable just the 4 bit byte variable
Is that correct?
Not sure why you think your code is correct and ask for help and then not take the advice.
The manual quotes.......

Syntax:
PULSIN pin, state, wordvariable

Also these lines of code are the same as b1 = b2 or b2 = b1, think you have a bug there.

let b1 = b5 + b7 - 150 'normal mixing to make throttle increase when tilting left
let b2 = b7 + b5 - 150 'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way

When i went to school 1+2 = 3 and 2+1 = 3 it makes no difference which is added to what in which order the answer is the same.
 

MPep

Senior Member
If I have mis-interpretted your code then fine, all I was pointing out is the fact that you do not appear to have taken into account the fact that you can end up with, mathematically, a negative number first, then try to add 150. The poor PICAXE will not know what to do with this.

As SABorn has pointed out, in your new code you are ending up with the same thing!!

the 8 bit word variable just the 4 bit byte variable
Completely incorrect.
A WORD variable is 16bits, a BYTE variable is 8bits.

EDIT: 4bits are referred to as a NIBBLE
 
Last edited:

srnet

Senior Member
it will always be contained between 75 and 225 as they are the servo limits. i know mine goes from 104 to 213 or something very close to that so I don't need the 8 bit word variable just the 4 bit byte variable
If you use a byte variable to measure the pulse (instead of the specified word variable), what happens if the RC pulse is a glitch and is longer than 255 ?
 

Madtom

Member
Also these lines of code are the same as b1 = b2 or b2 = b1, think you have a bug there.

let b1 = b5 + b7 - 150 'normal mixing to make throttle increase when tilting left
let b2 = b7 + b5 - 150 'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way

When i went to school 1+2 = 3 and 2+1 = 3 it makes no difference which is added to what in which order the answer is the same.
That's what I thought but I still get the same results one on full the other on nothing so maybe it's not my equation but the picaxe can't handle two at a time

Back to my original code which went it into the negatives couldn't I jus stick the + 150 at the start(instead of the end) to pull it away from zero?

Sorry I see what you mean if I go 150(b5) + 150(b7) It exeed that limit and the variable ticks over to the 0 my limiter code then kicks in keeping It at 100

But wouldn't the -150 bring it back
But it would bring it back to 205 which is still max throttle

And the opposite for the other command (I can change the order to avoid it becoming a negative)
So my byte variables are the problem
so like this for the mixing

W1 = w3 + w4 - 150
W2 = w3 + 150 - w4

So if we let w3 = 120 and w4 160
W1 = 130
W2 = 110

So that's 10 in each direction without causing a negative

Didn't mean to overlook the word variables just didn't think there was a need for them
Thanks
 

Madtom

Member
@srnet
If it glitches longer than 255 the byte will trickle over to zero again causing my motors to turn off

But am going to use a word variable know as my equations exceeded the byte's capacity
 

inglewoodpete

Senior Member
Some code simplification

(Every little bit counts)

Have a look at the MIN and MAX operators in Manual 2 (Commands)

Code:
if b1 > 200 then
   let b1 = 200
endif
if b2 < 100 then
   let b2 = 100
endif
...can be replaced with...
Code:
b1 = b1 MAX 200
b2 = b2 MIN 100
 

Madtom

Member
every little bit counts indeed
that is a lot easier than writing all those if statements
and will most likely reduce cycle time

I tried the word variables and it worked straight away but my throttle changes increase one of the motors and reduce the other

any suggestions my code looks like this now

Code:
init:
	servo b.7,100
	servo b.6,100
	
main:
	pulsin c.6,1,w6 	'throttle
	pulsin c.7,1,w4 	'yaw  <----ignore this it does nothing at the moment
	pulsin c.0,1,w5	'lean

	
let w1 = w5 + w6 - 150  'normal mixing to make throttle increase when tilting left
let w2 = w5 + 150 - w6	'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way

w1 = w1 max 200
w1 = w1 min 100
w2 = w2 max 200
w2 = w2 min 100


	servopos b.7,w1
	servopos b.6,w2
	
goto main
 

Madtom

Member
it works :D

very happy just had to swap w5 and w6 in the mixing commands here is the final code

Code:
init:
	servo b.7,100
	servo b.6,100
	
main:
	pulsin c.7,1,w6 	'throttle
	pulsin c.6,1,w4 	'yaw  <----ignore this it does nothing at the moment
	pulsin b.1,1,w5	'lean

	
let w1 = w6 + w5 - 150        'normal mixing to make throttle increase when tilting left
let w2 = w6 + 150 - w5  	'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way

w1 = w1 max 200		'recommend changing these to limit it further as at the moment mine flips real easy
w1 = w1 min 100
w2 = w2 max 200
w2 = w2 min 100


	servopos b.7,w1
	servopos b.6,w2
	
goto main
THANKS for all your help lots appreciated reaction time is slow though should i clock my picaxe to 16mhz that will affect the pulsin but not the servo
 

srnet

Senior Member
it works :D
THANKS for all your help lots appreciated reaction time is slow though should i clock my picaxe to 16mhz that will affect the pulsin but not the servo
It might be slow because, depending on your radio, you might be only reading the servo pulses at 1/3 the normal repetition rate.
 

Madtom

Member
my radio is a 2.4ghz digital radio so I don't think it is that after a few seconds reaction time seem's to speed up maybe its just the radio establishing a connection at the same time

turned the power off left it for 5 mins then came back and now only one motor for each chip works may be wiring but I don't think so maybe it just hasn't got the processing power to handle 2 at a time.which is why I thought of clocking. it says in manual 2 that servopos and servo commands will still work correctly at 16mhz

On M2 and X1 parts servo will only function at 4MHz or 16MHz.
but the pulsin command has this table under it which I dont quite understand

Effect of Increased Clock Speed:
4MHz 10us unit 0.65536s timeout
8MHz 5us unit 0.32768s timeout
16MHz 2.5us unit 0.16384s timeout
32MHz 1.25us unit 0.08192s timeout
64MHz 0.625us unit 0.04096s timeout
so what would I have to do to still get it to read the pulses correctly
thanks again
 

srnet

Senior Member
Does your quad have a conventional (specialist) mixer and gyros ?

Are you trying to build a PICAXE based quad mixer ?
 

srnet

Senior Member
I am trying to make a picaxe mixer

gyro is the next stage but i have to get all motors working first
I would suggest that a PICAXE based quad mixer will never work.

To deal with the gyros and stability routines you need something that is a great deal faster than a PICAXE.
 

Madtom

Member
I would suggest that a PICAXE based quad mixer will never work.

To deal with the gyros and stability routines you need something that is a great deal faster than a PICAXE.
I disagree
although you most likely know more about this than I do .
I still believe it is achievable and I have some chips lying around anyway.
if it turns out to be unachievable I can go grab an arduino or something.
although I don't know much of that code

stabilisation algorithms are pretty basic once you understand them and I was planing on having 3 chips

1 for Gyro input (Analog) which will send a servo signal to each of the others (so its almost like having another channel)
1 for Left/Right motors
1 for Front/Back motors

maybe I should just get an 08m2 for each motor
and still have an 18m2+ for gyro

it seems to work sometimes so maybe I have some loose wires or something

thanks for everyone's help
its much appreciated:D
 

Madtom

Member
I have got three going which mean one chip is faulty or I have a wiring issue ( I think its wiring)

Checked the signal and ground lines they are still connected thought.

Using the same outputs as the other chip and I know it is signal as it is always the motor connected to port 3 (I have labelled them for convenience it is b7 on chip 2 which is an 18m2+)

Don't have a schematic of my board but it's practically direct out to the servos and common the ground and 5v input
Have 330ohm resistors in the signal out lines
Replaced the one on that line to see if it was the problem. It wasnt. also did a continuity/beep test with the multimeter and my connections seem fine

Maybe I should switch the chips over to see if it's the chip ( I will have to reprogram them for the right slot ofcourse)

Also the steering is extremely sensitive should I put in divide w5 by 2 like this

(in the next post)
 

Madtom

Member
Code:
init:
	servo b.7,100
	servo b.6,100
	
main:
	pulsin c.6,1,w6 	'throttle
	pulsin c.7,1,w4 	'yaw  <----ignore this it does nothing at the moment
	pulsin c.0,1,w5	'lean

let w7 = w5 /2    'reduce sensitivity
	
let w1 = w6 + w7 - 150  'normal mixing to make throttle increase when tilting left
let w2 = w6 + 150 - w7	'reverse mixing to make throttle decrease when tilting left and opposite when tilting the other way

w1 = w1 max 200
w1 = w1 min 100
w2 = w2 max 200
w2 = w2 min 100


	servopos b.7,w1
	servopos b.6,w2
	
goto main
 

jedynakiewicz

Senior Member
it seems to work sometimes so maybe I have some loose wires or something
Have you got a properly decoupled and stable power supply for the PICAXE chips? Servo systems can be so terribly noisy and really spike up the chips. Perhaps you might indicate your circuit diagram for the power supply to the servos and PICAXE. I think the best way is to run the servos on 6v and use a low dropout regulator such as an LP2950CZ-5 for the PICAXE. This regulator is designed to be power-efficient and to work on batteries. Add a diode-fed capacitor to maintain the current when the servos crank up. Decouple properly and you should be okay. Your expression "maybe I have some loose wires or something..." triggered my thoughts in this direction. Suggested circuit diagram attached.
PICAXE servo power supply.png
 

g6ejd

Senior Member
I would suggest that a PICAXE based quad mixer will never work.

To deal with the gyros and stability routines you need something that is a great deal faster than a PICAXE.
Having done Quad Copter work, I agree with @SRNET, you need a lot more in your CPU time budget than a PICAXE can deliver to deal with the controls to maintain your flight dynamics, it might appear to work on the bench, but get it out in the air/wind and things are very different. I only know of Arduino solutions and I'm sure if a PICAXE was suitable it woudl have been done by now - so good luck and if you pull it off, then I offer you a 'Well Done' in-advance of that day.

The source code for a quadcopter Ardunio is avaiable, so worth a look to see how they are doing it.
 

Madtom

Member
For one of the not working I had a loose wire, I have fixed that and now I have 3 working but for some reason the forth won't.
I just get bad signal beeps from the esc
I have checked the curcuit with a multimeter and I have decoupling capacitors.
Plus both would be playing up if there was spikes from the motors
Also it is only on b.7 of the second chip
 

g6ejd

Senior Member
The number and duration of beeps will tell you what the ESC is finding error in. Do you have the ESC data sheet. Generally any more than three beeps means an error like supply voltage, temperature, pulse width too short or too long, etc.
Should start with 3-beeps to siginfiy all OK, or maybe one long beep. If continuous beeps then your starting your pulse width too high, etc. There are plenty of data sheets around to establish what the beeps mean.
 

g6ejd

Senior Member
Irregular throttle signal, or you have exceeded the minimum throttle setting on start-up. Try moving your ESC around t osee if the problem follows the ESC, if it does then it's been programmed for a higher throttle position and needs to be reset back to 1.0mS for minimum throttle. Easy on a radio system, not so easy on a PICAXE. The ESC data sheet explains how.
 

Madtom

Member
The problem does not follow the esc it is always on b7 of the second chip
And I have programmed the max/min throttle to the esc's through the picaxe

I'm starting to think its my code as if I reprogram the other chip for this ones spot I have the same problem.
Have checked the connections with a multimeter and they seem fine

Thanks
 

srnet

Senior Member
For a challenging project like this an Oscilloscope would help a lot.

Glitches in the output servos pulses, either rogue pulses or significant variations in width can be difficult or impossible to spot otherwise.
 

hippy

Ex-Staff (retired)
Connect the beeping ESC also to the B.6 that is driving the other to make sure it's not the ESC module, connect both to B.7 to check both beep and that it is a B.7 related issue.

Perhaps try outputs B.5 and B.6 or some other combination rather than B.6 and B.7. It might not solve things, and shouldn't be any different, but it might give further clues as to what's happening or why it's not working.

When there isn't test equipment to help show what's wrong, and leaving it as is isn't likely to make it magically start working, there's not a lot to lose in trying something else. You might by chance get it working and along the way discover information that points to what is going wrong.
 

Madtom

Member
It has been 3 days and I have gotten nowhere with it.
it seems to be that whatever chip is plugged into output 2 on my RX has the problem and then it always effects b.7 of that chip
but.
I moved the cable from b.7 to b.4 and the problem shifted to b.6 .
so I moved the line out on b.6 to b.5 and got no difference but then moved it to b.3 and the problem went back to the other one
so it seems the highest pin number is effected

I am very confused by this.
I had a look at the pin schematic in manual 1 but couldn't see a reason

thanks for your help.

also is it possible word statements are mixing together as they are so close in number. (I haven't used them much before. its probably stupid question)
 

Madtom

Member
Ok it may be conflicting commands but wouldn't the other chip be effected to

Appendix 4 - Possible Conflicting Commands
Internal Interrupt Driven Event Tasks
Task: Internal Interrupt: Command:
Background serial receive Serial interrupt hsersetup
Background I2C slave mode I2C interrupt hi2csetup
Timer Timer 1 interrupt settimer
Servo Timer 1 & 2 interrupts servo
Timer 3 Timer 3 interrupt tmr3setup
Hardware pin interrupt Hardware pin interrupt hintsetup
Comparator Comparator interrupt compsetup
I don't get how one of the motors on this chip work but the other doesn't if the wiring is fine and so is the code
 

srnet

Senior Member
my remaining pins aren't connected to anything. is this a problem?
It can be.

Unused pins should not be left floating.

Either use resistors as described, or if you are real sure they are not connected to anything, set them low or high as outputs.
 

Madtom

Member
18M2 C.3 = fixed output C.4, C.5 = fixed input

might have to use the resistors I have some 330's lying around and some 3k3's will they do?

To the 5v or ground ? (does it actually matter)
 
Top