Sunday, 21 April 2013

Compass Fun And Games..

After i got an anonymous tip from the person i am calling Anonymous Genius #102, i have been playing with my project that has a compass in it. i have some weird results when i try and compensate for the unit being tilted. i have tried following a few tutorials but they all seem to give different results. The compass on its own on a flat surface gives nice 0-360 degrees and 0-6 radians.

I have my code that runs ok if i tilt it up and down (Pitch the unit) the accelerometer seems to compensate correctly, but when tilt it side to side (Roll the unit) the accelerometer seems to overpower the compass and the results vary wildly. it looks like the accelerometer is the only input.

My device is to be hand held and waved around (up/down, left/right). I need to know the following things.

heading (what direction the device is pointed)
Inclination (the angle to the ground)
how far the unit has moved ( 1 meters to the south 0.5 meters to the east and  3 meters away from the ground)

these bits of data will be used to display where the unit has moved. so if it was mounted to a robot it would be able to tell where in a room it was, what direction its pointing, and if i have enough processing power left over keep a map of where it is and has been.

my code is below if anyone can point out where i stuffed up (it has to be in there somewhere, i just cant see it) drop me a line and set me straight..

 there are a bunch of global variables that are defined else where in the project, this is the code that deals with the compass..

When Get_Reading(float,float) is called you need to supply the Pitch (Pr) and Roll (Rr) values in radians.

  // Reference the I2C Library
#include <Wire.h>
  // Reference the HMC5883L Compass Library
#include <HMC5883L.h>
  int error;
  HMC5883L compass;
  // Store our compass as a variable.
  void COMPASS_INIT(void)
     pinMode(DRDY, INPUT);
    Wire.begin(); // Start the I2C interface.
  compass = HMC5883L(); // Construct a new HMC5883 compass.
  //valid gauss values are: 0.88, 1.3, 1.9, 2.5, 4.0, 4.7, 5.6, 8.1
  error = compass.SetScale(2.5); // Set the scale of the compass.
   if(error != 0) // If there is an error, print it out.
        error = compass.SetScale(1.3); // Set the scale of the compass.
           if(error != 0) // If there is an error, print it out.
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
    if(error != 0) // If there is an error, print it out.

float Get_Heading(float Pr,float Rr)
  while (DRDY == 0);    // loops while compass is busy, i soldered a wire to the DRDY pin of the chip
  // I2c request for current heading 6 bytes to make INT X Y Z
  // do maths to get headings combine pitch and roll to compensate for how the device is being held
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  // Values are accessed like so:
  //  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // check to see if pitch or roll are not past 40' , the formula cant handle more than 40 degrees tilt
  if(Rr > 0.78 || Rr < -0.78 || Pr > 0.78 || Pr < -0.78)
    return headingDegrees;   // if unit is tilted too far return previous value

  float cosRoll = cos(Rr);
  float sinRoll = sin(Rr); 
  float cosPitch = cos(Pr);
  float sinPitch = sin(Pr);

  // The tilt compensation algorithem.
  Xh = scaled.XAxis * cosPitch + scaled.ZAxis * sinPitch;
  Yh = scaled.XAxis * sinRoll * sinPitch + scaled.YAxis * cosRoll - scaled.ZAxis * sinRoll * cosPitch;
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  // Tilt compensated result
  float heading = atan2(Yh,Xh);
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here:
  // sydney Magnetic declination: 12° 31' EAST positive inclination: -64° 16'
  //                            : 0.215024564 rads
  // Mine is: 2ÔøΩ 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.0215024564;
  heading += declinationAngle;
  // Correct for when signs are reversed.
  if(heading < 0.0)
    heading += 2.0*PI;
  // Check for wrap due to addition of declination.
  if(heading > 2.0*PI)
    heading -= 2.0*PI;
  // Convert radians to degrees for readability.
  headingRadians = heading;
  headingDegrees = headingRadians * 180.0/PI;

// dump values to the serial port for debugging.
  #ifdef DEBUG_head      
  if (Serial.available()==1)
  return headingDegrees;

UPDATE - Looking over my hardware for possible answers to the above problem, i noticed that the Accelerometer and compass IC's are not mounted in the same directions.. this could be the source of my problem.. basically I Bread boarded the two breakout boards so XY on the compass is YX on the Accelerometer, A bit of a facepalm moment... I will rotate the Compass (it has less pins to re-wire) and post an update with the results.. I probably could sort this out in software but its only 5 wires

Friday, 19 April 2013

A Compass that points me in the right direction... yay...

I Received an email the other day regarding one of my posts from 14th Sept 2012, regarding my troubles with a compass IC that i had planned to use on a project.. the person doesn't want to be named but i must thank them for their assistance.. for now they will be known as Anonymous Genius #102 ...Thanks..

It may look like a simple solution, the addition of 12 chars, but it had me stumped for a while..
to solve this what you need to do is, open up the HCM5883L.h find the lines below..

struct MagnetometerRaw
    int XAxis;
    int YAxis;
    int ZAxis;

and make it look like this :-

struct MagnetometerRaw
    int16_t XAxis;
    int16_t YAxis;
    int16_t ZAxis;

add the 16_t to the keyword int this will force the compiler to use a 16bit signed int. the problem is that gcc  (for the PIC32 uP) sees it as a 32bit int. this would be causing all sorts of troubles when you start shifting buts around expecting it to behave one way and it doing its own thing...

the HCM5883L.h file is stored in the resources folder, you only need to edit the one under the PIC32 folder. in OSX the path is..

now when i run my code i get 0-360'..... now to intergrate this code into the other half of the project code... fun times ahead.. 

Monday, 28 January 2013

Nozzle Fun and games..

Today was spent hanging out eating with the family, yay nephews and the niece running around screaming, it was a change from the drone of the printer or the mill :). After that was over with i went to my mates workshop and proceeded to make some new nozzles for the printer..

  First i took a length of 5/16ths brass rod that i had from a previous nozzle session and proceeded to center the piece in the lathe which resulted in a 7.8mm rod 30mm long, i then made a shoulder 6mm in diameter and 22.5mm long, i then cut that off at 28.5mm long.

I turned the nozzle around and pushed a 3.5mm drill bit into the piece to a depth of 25mm, i then took the nozzles to the mill and proceeded to setup to peck drill the 0.5mm hole to form the nozzle, this was a total bust I snapped two drills in the process, so i decided to attempt to drill the next one from the outside to the inside so i could see what was happening, turns out that this was not that hard a process, i placed the blank into the drill chuck and moved the bed with the vice mounted on it until the blank nozzle was touching the jaw of the vice, this took a little bit of back and forth on the handles to get it just right, using a set of feeler gauges to find the point where both corners of the V groove. i then tightened up the vice and released the chuck, then in went the 0.5mm drill, then some manual gcode to lower the head in 0.25mm steps, i noticed the amount of swarfe that was generated from a 0.25mm plunge of the drill was quite significant given the diameter of the drill i was using, i found that my previous gcode didn't clear the swarfe from the drill leading to breakages.

Using this method of drilling from the outside to the inside was actually quite easy, as long as the tip of the nozzle is formed dead flat the drill doesn't wander any noticeable distance, i had tried not to do it from this end because i was worried that i would end up with a nozzle that flared out due to the need to use a centering bit first ( on the lathe this was needed) with the bed of the mill being so nice and tight i didn't need to center drill the blank first.

One thing i noticed was that when you have a 0.5mm drill bit in the chuck of anything you gotta be very very careful, if you even think a bad move it will snap.. i broke a drill before i even started by issuing a bad gcode command, the command itself wasn't bad just the results. the spindle wasn't moving and the drill was a lot closer than i thought..

here you can see a partially completed nozzle installed and two placed in just for the photo. they still need the ptfe liner and the brass riser tubes and nozzle tips formed before they are ready to use.

The result of a half a days playing on the mill and lathe.. i have still to shape the end of the nozzle to a point, this will be done later once the riser tube and ptfe liner is installed.

 here you can see the blank nozzle with its 0.5mm hole this is in the retracted position.
here the iphone decided the flash was need, you can see the nozzle in the extended position. once i have the first channel of the extruder working nice i will duplicate the settings across to the other two channels, then it i will see if the spring loaded nozzle flaps/valves can be remade to suit the new nozzle length, i increased the length of the nozzle to see if i can get a more positive closing and opening action. the original nozzles and valves seems to work quite nice but haven't actually printed with them installed i was having a bit of trouble getting started again so i removed them to make cleaning of the nozzle area much easier, on that note i think i may run into some clogging issue of the valve area, in that if the nozzle doesn't snap shut i may end up with strings hanging and these could end up above the flaps and cause problems, with the longer nozzle length i should be able to avoid some of these problems.  they may not be a problem. one thing i did notice when i was playing with the nozzle retracting was the amount of pull the filament puts on the nozzle isn't that great, i have a plan to crowd in the end of the brass riser tube at the bottom. this sits about 5mm above the melt zone with the ptfe tube extending towards the opening of the nozzle. i am hoping this crowding of the brass tube will grip the plastic a bit more on the retract and pull the nozzle up more. with the longer nozzles i will also be able to put a bend on the leading edge of the valve as i did notice the nozzle was starting to wear due to the valve scraping the brass away and this will eventually lead the Z height issues.

once i form the nozzle tips i will have to install all three nozzles and mount a strip of emery paper to the printer bed and drag the tips of the nozzles until they are even in length when the are fully extended. this will ensure one nozzle doesn't pump out plastic at a different heights and cause layering issues.

While i was attempting to peck drill the 0.5mm hole i discovered that my mill had developed a loose Z Axis Rack gear. this is because its held in place with two countersunk screws about 5mm in diameter, they don't hold up well when you have the Z axis Quill clamp engaged and you try to crank the Z axis, the cast iron in the main post just caves to the stress. I devised a solution, i took some 19mm wide strips of steel 3mm thick, one is about 200mm long the other is about 60mm long, these were then drilled to match the other holes in the post. the top one was close enough to the top that i managed to place a nut on the back of it and some loctite to make sure it stays tight with out the fear of stripping this hole too. the bottom strip rests on a step in the rear post that out of sight, this setup ensures there is nowhere for the rack to move when too much force is put on the axis. this makes sure my backlash stays at 1.83mm in Z axis.

Sunday, 27 January 2013

WOOT I'm so happy i'm squirting... plastic all over the place

    I have spent the last 36 or so hours poking at my printer trying to get it to play nice..  Last night i was so close to getting nice results out of the beast...

    After printing and tweaking and printing and tweaking and printing some more, I just couldn't get it right. the first layer was too thick the middle layers were too thin and the top layers too thick, then i decided to check what i was working with, turns out what i thought was a 0.5mm nozzle was actually a 0.35mm nozzle.. after that things went pear shape, so i went to bed, that was about 5am..

   when i woke up i had another stab at it. looking at what i had done made no sense, the firmware changes i made were gone, or wrong... so i started fresh, looked at the output and started to make some progress. i found that i needed to install the Z min endstop, to get a bit of repeatability out of the printer, once this was done things started to get better, i got the printer to start printing nice, then they wold fail due to the print job lifting up off the print bed... once i adjusted the temp of the bed to get 110'c on the surface of the glass, and the fans to blow at the right time, things started to come together really well. well enough that i could get to chasing down some print quality.

    With Repraps and Repstraps the fun and games really start when you start calibrating, and chasing print quality. You could be starting the print too close to the bed, (is there backlash in you z axis), adjust your Z offset in the slicer to test this then adjust the hardware to zero this out. then you need to make sure you are putting out just the right amount of plastic. When things don't come out  as expected there are a variety of causes, are you moving your axis enough, or too much for a desired gcode command.  I got to the point where the side of my test cube were nice but the corners where the print head lifts up and down between layers were not really pretty, there were random blobs, not on every layer, this is because Slic3r has a feature to randomize the starting point of each layer. i got the blobs small but it wasn't as good as i wanted, then trouble started, i got something stuck in the nozzle, i tried the lazy way out and created more work for myself, i tried to clear the hole by drilling the hole out to 0.5mm but ended up snapping the drill in the nozzle and stuffed it..

     Tomorrow is A Long weekend  here in Australia so i am going to spend the day at my mates workshop and knock up another three new 0.5mm nozzles, if time is going well i will make a bunch of blanks up as spares then when the 0.25mm & 0.35mm drills arrive from ebay i will drill them out. to do this i might have to re-face the jaws on my dividing head, while making my hyena style bolt up i noticed that depending on where the job is inserted into the jaws it can wobble, this didn't affect my bolt as i used a tailstock steady on the job.

Here you can see the results of my 36hour printing session.... on the left lots of worms. the back row, some niceish blocks. and in the middle you can see a print with a missed step. and on the lower right the nicest print so far.. that crease in the top is from where the extruder clipped it while it was still soft when i was trying to move the print head out of the way..  this is not perfect it still has a few blobs here and there but its on the way to being nice.. cant wait to make some more nozzles..

I still have some ptfe tube and brass left over from the last time when i made the first lot. this time i will make them a little longer and see if that helps with some of the problems have been having. and this time i have the CNC mill working enough that i wont have to hand crank the drill bit in and out to peck drill the hole to prevent snapping.. its a pain as you have to pull the drill bit all the way out of the job to check you have not snapped it, a 0.5mm drill bit doesn't give you very much if any feedback, and if you snap a drill when blind drilling or are unsure, you can waste drill bits trying to drill out the hole that is now blocked by a drill. will have to make some sort of depth ring for the drill bits to make my life easier..

for now its a bit of TV and Bed for me..

Tuesday, 22 January 2013

Victory is Near... so near i can taste the plastic!!

Had a hard day at work trying to make some soothing sounds from a PIC micro and a mosfet driven speaker, feeding it a square wave at varying frequencies to make a bleep and a bloop sound for good and bad report. I was eager to get to the mill, as soon as i sat down at home i rang my mate to see if he has finished work for the day, and he was, so i scooted out there as fast as traffic would allow. I got there at about 6:30ish and proceeded to setup the mill to enable the Y axis as i am still waiting on the replacement stepper drivers, so i am plugging the A axis into a axis that is not required, this time i needed the Y axis, first time i needed to use it so i had to calibrate the axis and document the backlash.  after about and hour or so and nice hunk of chicken i had the axis' setup and ready to roll.

i whipped up some gcode to make the teeth that i need, forgetting my notebook that i scribbled the formula in last night i had to go off memory and plain forgot how many teeth i wanted and some how ended up only cutting 18 teeth on the first groove so i manually cnc'd the last two teeth.

o100 repeat [20]
G1 Y-2.6 F25
G1 Y2.6 F50
G1 A18 F1000
o100 endrepeat

this fed the cutter into the groove nice and slowly then retreats to clear the job and rotates 18' and repeats. simple code. i then manually cranked the X axis to line up with the next groove and let it rip and repeated this for the last groove. the teeth are a little off center to the grooves but that shouldn't affect anything as i pre-grooved the job.

so now i need to go to and buy me a 10 tooth gear to mesh with the worm drive and mount that. i will venture into hobbyco in the city to latch onto a new set of skateboard bearing ( i know i have four fresh ones here somewhere but meh) then i will be all set to start a print, i seem to have lost one of my nozzles, probably hiding with the bearings so i might order some 0.25mm & 0.35mm drills and make a new set or two, now i have the mill under control they wont take so long to make, well at least i wont have to crank it by hand to peck drill them out, last time it was a PITA, might have a better look in the work room.. once i have the hardware sorted i will have to find out what the best firmware will be to use, i haven't been in #reprap for a while..

here is a vid or two of the bolt being machined on my cnc X2 Sieg mill with Dividing head..


 here you can see the cutter making the teeth for the drive bolt for my 3d printer. its cutting 20 teeth.

 this is the cutter that i made from a piece of 5mm hss tool steel that i bought off the internet for about 1$au each, bought 10 bits in the buy it now sale, so $10 delivered, i have posted about a company that will make/does make, cutters that would have done the job but i am an impatient sort of person and wanted to try and see if i could do it, as it turns out its not that hard i used a 6inch angle grinder with a 1mm cutting disc to form the profile then turned it to an angle of about 30' in the tilting vice to cut the cutting edges/flutes.
 my iphone doesn't seem to like taking photos of things up close but the videos are not that bad..

the cutter seems to have survived enough that i will probably use it to make the new extruder with any improvements that i can think of. i would like to see if i could make this thing printable and post it to, that reminds me i should update my existing things i have posted to reflect the progress that has been made. 

Sunday, 20 January 2013

Progress on the printer..

Finally i have the mill working enough to attempt to make a Drive bolt for the triple extruder. as i blew up a few channels on the Stepper control board for the mill and am waiting for the replacements to arrive from They should be here just before the weekend :). I currently only have three channels/axis' working on the mill, XYZ, i was moving the mill and pinched the stepper wires and let out the magic smoke on the other two channels AB. i have ordered 10 replacements (not expecting problems but not wanting to get stuck) and it makes the fedex charges ($30aud) worth it.

Today was spent setting up the mill, plugging in the steppers, i dont need the Y axis to make the bolt i can make do with XZA. then YZA to cut teeth. i also documented the backlash in the mill and entered those into EMC2 and tuned the acceleration to avoid the missed steps. I haven't quite got my head around the speed settings and stopped once things were working good enough for now. finer tuning can wait for when i have all the axis running.

The other weekend i purchased a 2m length of 1/2in Brass Solid bar for about $40aud and cut off a short length of about 100mm. placed that in the lathe and machined a 6mm wide 8mm diameter step to fit a skateboard bearing that i had placed in a Steady i made for the dividing head. plan is to put a cone shape coupling so stepping the job piece is not required but that can wait and a shoulder will work for now. i then proceeded to write me some gcode. while i was doing the expected cut and paste job in a spreadsheet i got to thinking there has to be a better way, so i had a quick peek at what gcodes i had at my disposal and found the while loop and do while pages in the linuxcnc docs and rushed ahead and developed some code that would repeat the tasks required then when checking the syntax i found a neater command, repeat [n]] ; other commands ;endrepeat this doesn't require any external variables to be setup. soon after i had some code ready to run.


; 12.7 - 8 = 4.7/2 = 2.35
;2.35 / 0.25 = 9 & .1

o120 repeat [ 9 ]
        G1 Z-0.25 F35
        o125 repeat [362]
                G1 A1 F1000
        o125 endrepeat
o120 endrepeat

G1 Z-0.1 F35
        o126 repeat [362]
                G1 A1 F1000
        o126 endrepeat

G1 Z2.35 F50
G1 X30 F300

o130 repeat [ 9 ]
        G1 Z-0.25 F35
        o135 repeat [362]
                G1 X40 F300
                G1 X-40 F300
                G1 A1 F1000
        o135 endrepeat
o130 endrepeat

G1 Z-0.1 F35
o140 repeat [ 40 ]
        G1 X1 F300
        o136 repeat [362]
                G1 A360 F1000
                G1 A-360 F1000
        o136 endrepeat
o140 endrepeat

this code takes a 1/2 inch rod of brass and machines two shoulders for the bearings to run on.
have some more code that levels off the center area that actually grips the filament. this code is very similar to the above code in the way it uses repeat to loop.

after this i attempted to make the semi-circular grooves that should have lined up with the pinch rollers but i didn't quite get the gcode right, i forgot to lift the head up when moving from one groove to the start of the next and stuffed it up really bad. it has big fat grooves where it shouldn't be :(

So tomorrow I will try and get away from work early and scoot out to my mates shop where the mill is located and start on a new bolt. i have plenty of material and i hope i have all the kinks out of my code and should be able to make a bolt with out too much trouble.

here you see the grooves (that shouldn't be there) between the rings (that should be there) this is where i was missing a line of gcode

G1 Z1.5 F50

if i had that between the loops i would have been okay. as it turns out i noticed that the chassis of the extruder was not parallel, the sides tapered inwards and was throwing my measurements all of the shop, now i have massaged that out of the way i can get to making the drive bolt. without errors.

to make sure i get it right this time i will take the job out of the mill and place it in the extruder before i make the rings and mark exactly where the rings are to go then i will cut the teeth into them individually with the dividing head, i was trying to keep the bolt in the chuck of the dividing head for as long as possible to try and keep the run out to a minimum.

Armed with the knowledge that i learn today i should be able to get this bolt done and all three materials working on the printer, once this bolt is done i have all the mechanics done and then it time for firmware, or some creative g-coding.

before i go i have to say i set the mill up and left the factory to go get food and came back and the job was still ok, the cutter was still ok and there were no big holes in the chuck or the bed, my mate was there doing his thing and would have stopped it if he heard it lunch a tool or something but it survived, i am getting a little more confidence that this project will get finished and actually exceed my initial expectations, i am really happy with the repeatability of the axis' movement now i have the backlash under control :0

Sunday, 6 January 2013

Magnetic Bed Testing..

Just performed a quick and dirty test on the magnetic bed, while holding a smallish piece of steel to the bed and applying 12volts to each of the coils the piece of steel was held to the bed, enough that i could turn the bed upside down and it stayed stuck to the bed. then i tried moving the block laterally and it moved really easy :( so i thought how much power do i need to get that to stay stuck to the bed and not move sideways, so i turned up the power supply to 24volts and the coils drew 6.3amps. this held the bit of metal nice and tight :). I wouldn't mind  running the coils off 24volts but the amount of heat that was generated was beyond ridiculous.... I then thought i would test out the 10% duty Cycle test, and the results are promising, if i fire up the coil pack with 24volts i get good grip, then if i quickly turn the voltage down to 5volts the coils continue to hold the piece of metal nice and tight but after a few seconds the coils relax and the piece of metal is then allowed to move.. Time to make a PWM controller that can switch 24+volts at about 10amps and see what happens :)

Will post a further update once the controller is done and tested.

If i was to break the bed up into 3 sections of 20 hexagonal cells each section would have a effective resistance of  1/( (1/22) * 20)  =  1.1 ohms

Each section of bed would draw @12volts 12/1.1= 10.1 amps of current at 100% duty cycle
but this could be cut back to 2.5 amps once running @25% duty cycle, this could be done with a capacitor charge pump circuit of some sort or i could break each section down further into two this would give me a sectional current draw of 5.05amps @ 100% duty cycle and 1.25amps @25% duty cycle.

if was to run the coils at 24volts (like the good test showed is required) the sectional current draw would be doubled. with 3 sections each drawing 20amps @24volts ( and each sub section drawing 10amps) if i was to turn them all on at once i would need 60Amps of current at 24volts, this is unreasonable. but if i stagger fire each subsection @100% Duty cycle then cut back to 25% duty cycle i would need 10amps then 2.5amps per sub section.

the current profile would look like this
 @ 10% Duty Cycle
@ 25% Duty Cycle

ie each coil is hit with 10 amps and then its dropped back to 10Amps @ 25% Duty Cycle.
at 25% duty cycle the overlap turns out to be quite high.. i would need a PSU that spits out 25Amps @24 volts.. this is closer to reality but still a bit too far off i think.. i guess i will have to see what ebay has to say about the price of fish.. if i can find a 24V psu that is cheaper than further complicating the controller i wil do that other wise i will have to break down the coils into even smaller groups..

actually i have made an error.. the curve will look like this

the previous chart didn't take in to account the fact that we will be turning off cells after they have been driven @25% this makes things much more manageable. I can probably stagger out firing order a little more and make the curve a little flatter but this will do for now.. 15Amps @ 24Volts can be had really cheaply thanks to Ebay. just found a buy it now 24volts 20amp PSU Delivered for $66Au Bargain. this PSU allows me to fire two sub sections at once, basically this will allow for upto 33% duty cycle and if managed correctly 100% startup/inrush current can be achieved, the plan will be to turn each coil on for 90% duty cycle for the first pulse and then .1s after that, as the cycle steps thru the first time each coil is energized it will actually be on for 100% for one cycle, 90% + 10% then after that they will be turned on for 10% of the time rotating between the 6 coil subsections i can manage 16.6% duty cycle without overlap. if overlap is used i can get 33.2% duty cycle before my PSU will start to complain.

worst case is that i end up with a nice 20Amp PSU for my printer. I was running the steppers off 24volts but got to tidying things up and switched back to 12volts..