Fighting with computers

Computers are not always friendly.

Sunday, March 15, 2015

One Arduino controlling two brushless DC motors

After some changes I have been able to get it working reliably with two motors. But one of the changes I have made is to use a new type of motors that I reckon are better suited to the task. Instead of using brushed motors that are cheaper, I have found these other brushless motors that have several advantages: 
  1. Being brushless there is no other wear than the bearings
  2. They have built-in driving electronics, so they can be controlled with the Arduino digital outputs, that simplifies things and reduces interface cost. 
  3. Motors have built-in encoder disk too, but the one they carry is just 100 lines per revolution (a bit poor in my opinion) that can turn into 400 "steps" per revolution with the 4x decoding of the program. The lower number of lines per revolution puts less stress on the Arduino interrupt code, which may be one of the reasons it works ok now.
  4. I have moved all the encoder signals to pins that are monitored with the interrupt on pin change and I have reserved the two external interrupts for the STEP input signals for each motor. 
Though I am doing my tests with an Arduino UNO, it has to work the same with the Pro Mini (as far as it uses a '328 chip too). 

The motors I am using seem to be manufactured for Ricoh, but I have failed so far to obtain any decent technical information. But they are sold as 20W brushless motors with built-in driver electronics and quadrature encoder.



While they cost around $15 each, these motors have a brilliant behavior, even when powered at 12V (they are rated 24V).  With power to spare I have to reduce the PID maximum output if I do not want my toothed pulleys to skip in the highest acceleration moves of my tests. The video below shows X and Y axis motion while printing a part controlled with Marlin firmware running on the blue Mega board while the red Arduino UNO board runs this code.

Some other interesting detail is that motor electronics will work at 3.3V too, in case you are driving it from a 3V3 processor. But remember output shaft is 6mm and not 5mm which is a bit of a pain.

Saturday, March 07, 2015

Moshidraw: pain on every laser cut

I have been using a [cheap] laser cutter machine. You can see which one on the following video.

 

Till now it kind of works but what is really annoying is the software you have to use to be able to cut and engrave. It is called Moshidraw and for reasons I cannot really understand it comes with a USB dongle for protection (it will not run without it).  Not in a million years I would like to copy such a program.

This is what I have learned so far that causes trouble:

  • If you DXF file contains curves, the operation will fail randomly (suddenly the cut will take an unexpected route and it will ruin your stock material).
  • If you import a DXF and fail to select the part, the cut will did not work properly.
  • If you have several parts in DXF, it may fail at any point (mostly when it is almost finished and all your stock material will be ruined). Best results and lower risk if you do multiple parts one at a time.
  • There not seem to be a way to force inside cuts first, so some operations will fail as the outside is cut before the inside so the part will fall and inside cut did not happen (only solution I have found is to reduce the power, doing multiple passes and keeping some of the contour to be cut till.
  • Sometimes it will cut a line in the middle of the design for no reason. If you are lucky the Optimize Route option will avoid that to happen but that is not a sure bet.
  • It will never remember you want to do a outline cut or that you do not want a mirror image of the cut.  At least it will remember the offset you fixed.
  • Do not attempt to repeat a cut without closing and opening again the cut dialog or the second one could fail. (Don't you love it?).
Other than that and the ridiculously small area of the stock material holder it is a fine machine if you have the patient not to destroy it every time you found out one of the many evil sides of the software/firmware that controls it.

It is easy to see why many people will completely replace the controller board right away.

Sunday, February 22, 2015

3D Tool path generation

I needed for a project to create a parallel finishing tool path similar what many CAM software does. There is nothing wrong with existing CAM software but what fun would be to just buy and use the software.

The basic idea is that you can use a swipe plane across the mesh representing your volume and get the top part of each slice to be the tool path. A similar idea is used for 3D printing, but there the swipe plane is an horizontal one and object is built from the bottom up.

The problem is that you cannot use the slice you just obtain from the mesh as milling bit (or extruded filament if 3D printing) has a non-null dimension. Because of that, just following the contour of the object will create a smaller result than the model.

For 2.5D milling you can do a waterline finishing, doing flat passes on XY-plane. Thus a simple polygon offsetting operation can help you get the part dimensions right. But for 3D milling, the tool compensation becomes more difficult.

The solution I opted for was to perform a mesh offsetting. Three different ideas can be used for the operation:

  1. Facets can be offset following the normal direction.  Mesh becomes full of gaps
  2. Edges can be offset following the a combination of the two normals of the two triangles sharing that edge.
  3. Vertices can be offset too. The question is that what is the better way of combining the normals of the three triangles each vertex shares. 
A quick look at the vertex normals calculation available in Meshlab gives four different methods. Before committing to code anything, a quick test would be welcome. 

I have done a simple test, that proved useful: after calculating vertex normals, I can export the file from Meshlab. Using PLY format instead of STL allows me to output each vertex's normal. If now I do some math with a script I can do the offset and test the result. 

Most time was lost due to a particular "feature" of awk in OSX. Once this was fixed I was able to offset the vertices of the mesh and prepare the correct tool path.

Friday, February 13, 2015

Barcode listings with data

A few weeks ago I mentioned a new tool I created for (allegedly) making my life easier when delivering marked exams to my students. Then I covered the process that happens after the exam. But one key element it was not mentioned is what it needs to do in advance.

For my system I needed a set of labels. My prototype used Code-128 barcode that can handle ASCII code to encode each student email using barcode command-line program for Linux. The only problem with that was the lack of additional information being added to the label, such as the student name.

Looking around I found a very handy tool by AVERY company, that helps you creating labels on-line for all their range of labels. What is even better is the tool will allow you to upload an excel file with data columns, like names and addresses to create a custom listing you designed and export it as PDF (they offer to print it for you to and deliver the labels to your office too).

After some tweaks and, the most difficult part, discovering the reference of my European A4 labels on their site, I get a set of working labels but this time using 2D QR codes that are a bit more reliable than 1D barcodes.

But I was left with a bit of an itch that I should be looking for a way of solving the problem entirely with my own software instead of using an online application, as importing a new list of students required a bit of touching here and there.

Not knowing what could do the job more easily, I turned to the ZXing library I am already using for decoding and I saw there is an encoder too. And given that creating a PDF output seemed to work well for AVERY I decided to do the same, this time using Processing, as it tends to make my life easier than when using Java.


The function above will create a QR code as an image you can later place inside a PDF page following the pattern you want. Going from BufferedImage to PImage took me like one hour to get right, as it seems Processing has been going through different solutions to that in the past, most of them no longer working.

So now I do have a tool that creates the labels from a CSV files with students name and addresses. I am looking forward to use these tools in the coming months.

Saturday, February 07, 2015

Maple Mini looking good

This week I received a real bargain I wanted to test as a better candidate to be able to control several DC motors in closed loop.

I have already tested the STM Nucleo 401 board and I get one 32 bit, signed, hardware encoder input on timer 2 and three more unsigned, 16-bit hardware encoder inputs on other timers. Unfortunately timer 5 was taken by the mbed library I was using. Nucleo is a solid platform and using the cloud IDE actually is not big deal for me.

But now I have got a Maple Mini for $4 and I have to tell you I am totally sold. It is so much faster compared to an ATMega Arduino that I am positive it can do the work for controlling two DC motors closed-loop and most likely it can do four or more.

The board can handle up to 16 external interrupts, enough for 8 encoders, and it can produce 12 PWM 16-bit resolution outputs (a far cry from 8 bit PWM from Arduino, though I am not sure that will have any impact on the system performance) plus plenty of memory (128K flash, 20K RAM), serial ports and other nice touches.

Like any other Cortex-3 it runs off 3.3V power, which I thought it will be a pain as encoder circuit is rated at 5V, but a quick test revealed encoder seems to be working happily when powered at 3.3V too. That saves me some trouble with interface and gives me more freedom as not all the input pins are 5V tolerant.

Though the Maple family has been around for quite a while (2009) I have never used it before. And my first experience has been a blast too, because the Arduino IDE clone is what they use, so it all seems pretty familiar for long-time Arduino users.

I guess that sooner or later I will need to venture in some of these detailed user manuals for setting some feature on, but for the moment the sample programs and online help are being good enough.

Thursday, January 29, 2015

A better mousetrap so to speak

Once in a while I need to mark exams. Exams do not tend to please students and marking exams is rarely a task anyone enjoys doing. But I cannot change that.

What we do later is what I was trying to change for a while: For some years I have been using Scribd as a platform to make available the marked exams to my students. As I did not have a better tool, I started by doing the scan of the big pile of exams to later make it available as a document they can browse on the web (but not download).

Making all the exams available to all the students is a liability that created me no trouble so far, but I can see privacy concerns plus fear of putting too much information in the hands of the students. To address the privacy concerns I did not made the document visible or public, but private so only those with the secret URL could browse it. On top of that, marked exams were kept available for a couple of weeks before being deleted.

The idea of a student being able to review the whole marking process may not be entirely wrong, as it seems a great exercise of transparency, but it makes most of my colleagues uncomfortable.

Of course I could scan each exam on a different file and later send a copy to each student. But while this approach may be better, it takes a lot of work.

So what I did today was to put together a nice set of tools which help me get a working solution:

  1. ZXing project provides tools for you to create barcodes but mostly for decoding them. It is Java based and it works nicely.
  2. barcode is a linux command-line tool that can easily create a set of barcodes by reading from a textfile (containing names or whatever other info you want to encode).
  3. ImageMagick is a fantastic command-line set of tools for image manipulation
  4. Postfix (or sendmail) can be used to send email alerts to the students (unfortunately my ubuntu version is old and I cannot install new stuff anymore, time to upgrade to next LTS, so I had to make a hack in Java for sending email using SMTP and Sockets). 
What I did was quite a straightforward process:
  1. Once student is turning in the exam, a barcode with his/her email is sticked to the first page of the exam. Several pages of barcodes were created using "barcode" program from the list of emails of the students. Each barcode the text version of the email too for human identification. Code 128 was used.
  2. The big pile of exams is scanned at grayscale 300dpi as a sing PDF file.
  3. PDF file is processed, extracting each page as a bitmap file.
  4. Each bitmap file is processed by ZXing with the option --try_harder so the code is read no matter were on the page it was stuck. 
  5. Once the last page of a lot is detected (as the new one has an email barcode) the sequence of pages of that exam were stored in a special folder on a web server, with a name derived from email value. 
  6. An email containing links to each one of the scanned images of the marked exam stored on a web server are sent to the student (directory listing is not possible in that server, only valid URL are shown).
This way each student gets only his/her exam and they can see what they did wrong or ask for any clarification they may need. On the web server side, the URLs are created in a way the students cannot guess their or others filenames. Being a web server we get feedback of whether a students have checked or not their exams. 

The whole thing process one page per second so my 80 pages exam was done in two minutes, alerts emails included. Now I need like five years to recover the invested effort in creating a tool for saving me time :-)

Monday, January 26, 2015

Sometimes you get lucky, sometimes not

I have wasted a fair amount of time today with a problem I solved the way I recommend to others: use a library. I needed to do a polygon offsetting in a program and I used what has worked for me in the past, the Java Topology Suite.

I did my thing and it almost worked perfectly:

However, I noticed a minor artifact once in a while. After some time I found that offset would sometimes did not work well as a weird line would appear crossing (line between to small circles) my beloved pumpkin instead or remaining on the outside (horizontal crossing was expected though).
I as not trying to do a full offset but only of the part whose normals is between 0 and 180 degrees, as this project is related to 3d milling. 

After a few hours of testing it all came down to a defect on the library (as far as I can tell) because with the same input data, but a smaller offset value, the operation was succesful as you can see below:

I was unable to find a proper forum to ask other users about this problem, so I will keep on using this library when the offset values needed are low but I will need to find a better choice for the future. If you know of a good choice I could use in Java I will like to know.

At any rate, here you have the data points in case you want to have a look.