Fighting with computers

Computers are not always friendly.

Tuesday, November 25, 2014

Loading TinyG2 in the Arduino DUE

I have been intrigued for a while with TinyG controller and their S-shaped speed
curves. It is an
evolution from simpler trapezoidal speed patterns used by GRBL software and by most 3D printer firmwares out there too.

However, TinyG (besides a South Korean music group) is not designed to work on a regular Arduino but over more powerful Atmel processors, the XMega series. While the software is open source, it does not help if you do not have the right processor. But lately I am giving a course where I have borrowed an Arduino DUE, which happens to be supported by TinyG project too.

Uploading the code has been a bit of challenge, partly due to my attempt of using a USB cable with broken data wires (I did not know that as I use it for charging my cellphone). But even with a good cable, the upload process, at least using OSX was a bit of a challenge. It all comes down to a not entirely cooked script from their github.

As I am not familiar with ARM development conventions, I see there is a binary file that I can download, but it is a .elf file. Apparently, the uploader program, bossac, cannot handle the .elf file for reasons I do not understand. It needs a binary file instead, and that can be obtained with an obscure command (at least for me): arm-none-eabi-objcopy -O binary tinyg.elf  tinyg.bin

Once I have got the binary file, I can upload the code happily with command bossac  -e -w -v -b tinyg.bin -R

However, the built-in script from the git, DueFromOSX.sh failed miserably on my computer.

As usual, my downloading of Arduino IDE that was required, 1.5.8 version, was not successful at first as the Java 7 version is apparently not the one that I have installed but Java 6. Nothing that could not be fixed downloading the right one. But I was surprised how tricky the process has been as a whole.

Unfortunately I have run out of time for testing the DUE with grbl shield on my ShapeOko to see if there is a big difference from its older brother GRBL. Maybe another future post.

Update

For Linux, I did not have much luck with the direct usage of bossac command, however I was puzzled at how Arduino IDE will work flawlessly in Linux while still it was using bossac. So I realized the culprit was the need of a previous setting the port to 1200 bps to reset the board and clear the memory, this is the command that worked for me to upload code to the Arduino DUE from Linux command line:
stty -F /dev/ttyACM0 1200; sleep 1;  bossac --port=ttyACM0 -U false -e -w -v -b TinyG2.bin -R

Thursday, November 13, 2014

Another one bitten by the bug

While I have been doing electronics since I was a kid I never owned an oscilloscope before. At a local meeting of Arduino users a colleague mentioned me there was a very good offer of a four channel sampling oscilloscope. Not that I need to use it very often but I was curious enough to see how instruments have evolved lately I decided it was about time to have my own scope so it was kind of a birthday gift I bought to myself (if that makes any sense).

First thing I have measured, just because it's right next to me is the the ringing of my 3D printer's hotend PID regulator output. It has been mentioned voltages can get pretty high over there in RAMPS boards and boy, that is true. At around a 50-volt spike is produced when the load is switched off. And the real value can only be appreciated if you freeze the sampled signal and expand widely the time axis, as the spike duration is below 1us. I guess total energy is so small the MOSFET is not really damaged (as it has been sitting there for three years now without stop working). Values in the order of micro Joules seems to be safe as the STB55NF06L is avalanche tested and the single pulse avalanche energy is 300mJ.

This second image shows the moment the heated bed of my printer is switched off. It shows how the highest peak is actually triggering the avalanche mode in the MOSFET (avalanche voltage is rated 60V).

Friday, October 10, 2014

VFD control with Arduino using RS485 link

Variable Frequency Drives (VFD) allow the control of spindles so speed can accurately be controlled and a detailed acceleration profile for the spindle and reverse rotation can all be handled. In essence a VFD is an three phase inverter for three-phase AC motor.

I am using a popular (I mean cheap) Chinese VFD and though the reference manual is not great, I could see there is a built-in RS-485 port. I usually control the start, stop and speed selection using the keyboard on the unit but I thought it will be more useful if I could control everything from the same Arduino is doing our CNC table control.

Some cheap RS-485 off eBay and some lines of code later, I can start, stop and change the speed from an Arduino. What a cool thing to have!




Some configuration of your VFD are needed before you can use it like that.  You need to set PD163=1 (I am using address 1 in the code). PD=164=1 (for setting serial to 9600bps) and PD165=0 (for using ASCII and 8N1 character format). On top of that, you need to set PD001=2 (for commands being provided through the communications port). 

Please note that doing that may interfere with your existing controls using external terminals, so be careful as that might make your emergency or external stop buttons to stop working. The built-in stop button still will stop the spindle but you need to power-cycle the VFD for accepting new start commands over the serial port.


Thursday, October 09, 2014

First steps with ESP8266 wifi module

Some weeks ago it was mentioned in hackaday.com a new wifi module was available at a very tempting price (I paid $10 for two ESP8266 units shipped). It is a full SoC system which firmware can be updated.

My main interest was to use it with Arduino projects and, perhaps, as a way to use my 3D printers wirelessly.

The module offers, among other choices (I2C, SPI) a serial connection, running at 115200bps. Getting it to work has been a bit weird, as documentation was not very accurate and I was not paying much attention either.

Care has to be taken with power supply as the unit is to be powered at 3.3V and using 5V will probably kill it. Likely it seems that I/O are 5V-tolerant (or so they seem till now).

My initial setup has been to use an Arduino UNO as a power supply for the 3.3V and another 3V USB to Serial adapter with one FTDI chip. This way I was able to interact with the board using a serial terminal software (Coolterm). Unfortunately my initial setup was a no go.

I initially connected GND, 3V3, TX and RX pins. I was lucky a friend of mine did the same dance before and figured out that at least an additional pin needs to be HIGH too, which is CH_PD pin that acts as an enable signal, without it the board is mute and deaf.

Once you connect this last pin to VCC (remember 3.3V) the module is brought to life, and after some garbage in the serial port you can read the word "ready". Once you get there things will start to get interesting.

I have found the best was to interact with the module is using an Arduino with at least two serial ports. Whether you use a Leonardo or a Mega, you can get an extra hardware port to communicate with the module and the other for interacting with the user via the serial terminal.

You can do the basic stuff if you load the Blink example on your Arduino UNO (so you are positive Arduino serial port is not used) and you connect the TX and RX pins of the module to pins 1 and 0 of Arduino. But this setup is only for you to use Arduino's IDE Serial Terminal function.

If you want to start coding for Arduino to do some useful stuff you need to be able to both reprogram the Arduino (which you cannot with the previous setup) plus accesss to or being accessed from the Internet, a second serial port is needed. One that can work at 115200bps. Unfortunately SoftwareSerial can only work up to 19200bps, so it is out of the question.

The picture above shows how I am using Serial3 on Arduino Mega as the communication channel with the module, while Serial is kept for the user interface (with the serial monitor of Arduino IDE).

I have created a simple bridging code between the two ports as you can see below:

// simple terminal on Serial3 <--> Serial for Arduino Mega 
void setup() { Serial3.begin(115200); Serial.begin(115200); } 

// copy from and to each port 
void loop() { 
 while(Serial3.available()) Serial.write(Serial3.read()); 
 while(Serial.available()) Serial3.write(Serial.read()); 
}

Now open the Serial Monitor and start testing the different commands. Try first just "AT" to see if you get an "OK" response. If you don't, they there is some connection problem (maybe TX and RX pins are connected the other way around?).


Basic commands

Once you get your module to answer your commands, you may want to try AT+GMR to check which version is your firmware. Mine is 00160901. 

You can reset the board with AT+RST command.

But the magic starts once you try AT+CWLAP command, that will provide you a list of available wifi access points in the area.

Next you want the board to join a certain access point (most of us want the board to act as a STA or wifi client that will connect to an access point). That behavior is selected with the AT+CWMODE=1. You will need to reset your board after making this selection.

To connect to your home/office access point you issue the command AT+CWJAP="yourSSID","yourWifiPassword"

The good news is that from now, every time you power the board it will try to connect to the same wifi network. You can see if it was successful by checking your router's DHCP client list. Alternatively you can check your module IP address with command AT+CIFSR. Once you know your board's IP address you can try pinging it from your computer. Do not be surprised if you get delays over 100ms sometimes. 

If have made it so far, then your are ready for some fun uses.

TCP client and server modes

The module can handle up to four connections at a time. The card can be set to allow connections with the command AT+CIPMUX=1

A TCP connection can be started, for example to a web server by AT+CIPSTART=0,"TCP","www.yourwebsite.com",80

Once you get the OK response, you should get the text "Linked" next showing you the connection has been established. 

Now it is when things get a bit odd. Whatever you send to the serial connection of the module IS NOT transmitted through this new connection but just interpreted as new commands by the module board. If you want to transmit something you need to use the command AT+CIPSEND=0,50 to send in this example 50 bytes (change the number by one of your choice). After this command you get a prompt ">" so you can type your 50 character that will be transmitted through the TCP connection you just created.

If at any time data is received through any open connection, it will be signaled by a line of text over the serial interface "+IPD,0,1024:....". Number 0 is the identifier of the connection data is coming from. 1024 is the number of bytes that come afterward, not including the colon that acts as a marker. Next all the 1024 bytes will appear over the serial line.

If you receive several TCP segments, each one of the them will be signaled as the one above, stating the correct number of bytes of each one.

The TCP connection is meanwhile kept open. If you want to close it at any time, use the command AT+CIPCLOSE=0 (0 is the connection id number, that may change in your case). No matter who closes the connection, once the connection is closed you will get a line with the message "Unlink" warning you about a connection being closed. 

You can check the status of your connections with command AT+CIPSTATUS

The board can also enable a local TCP server with command AT+CIPSERVER=0,8888 (where 0 is the connection id and 8888 is the port number). Once created, you can connect from your computer using telnet or netcat to your board's port 8888. Again any data the board receives from the connection will be announced with the +IPD,0,bytes header. And you can send data to the client with AT+CIPSEND command as you did when using the TCP client connection.

Contrary to the documentation, I could not make any connection if AT+CIPMUX=0. That is supposed to allow only one connection but it did allow any in my case.

Multiple connections to the TCP server are possible (I guess up to four, but I have not tested it). 

Power supply issues

Sometimes I have got the board die on me with a message that suggested a watchdog timer reseted the board due to a malfunction. This seemed to happen a lot while using the board at the office (clone computer) but it did stop happening when trying the board at home with an old-ish iMac. My guess is that the Arduino UNO and or my office's computer USB max current was quite close the peak current needed by the board. However, with the Mega connected to the iMac I have not seed the message all evening. 

To be on the safe side, get yourself a 3.3volt power supply that can provide up to 400mA and you will be ok. Along the ride, there have been moments when the board did not answer me right away for reasons unknown, for example not responding for a while to AT+CWLAP command (for listing access points). 

I know there are more modern versions of the firmware and the newer ones do include a way for the board to self upgrade once it get online (cloud upgrade) which is kind of cool. Once I learn whether it is worth or not I might upgrade.

Not exactly what I expected

Unfortunately, using the board with a 3D printer running Marlin software on an Arduino Mega will require some filtering of the data sent back an forth. As received data is prepended by the text "+IPD,id,bytes:" and in order to send data you have to invoke command "AT+CIPSEND=..". All this makes the communication not very transparent, at least not as transparent as when you use a Bluetooth module. But it is still doable with some coding.

Finally, what I did not like either is the link throughput that was below 80kbps in my tests, where the module is not in the same room as the wifi router. 

However, for data logging on the cloud or IoT projects based on Arduino I think this board is a great tool as it allows IoT integration at a very affordable price.  





Thursday, September 25, 2014

The ever shrinking Arduino

The las version of Arduino I bought is the micro. Around the size of the Arduino Pro Mini but
featuring a processor that handles the USB port too and costing $7.50. As I bought mine from an Asian online seller off eBay I decided to give a try before providing feedback.

Last week I have got trouble with two items I bought on Aliexpress: a set of Kossel linear rails that have rough spots along the rail and a set of hardened chromed smooth rods that are very smooth but bent too. In both cases they first impression was they were ok and I give them the heads up so they could get my money. Unfortunately, on closer inspection I discovered the problems mentioned above and I am still in the process of sorting it out with the sellers.

This time I wanted to be quite sure everything worked as expected ... but it did not. I could happily upload new sketches once I selected the Arduino Micro as the target board. But I could not get Blink program to work. After checking Arduino docs about the Micro it was clear pin 13 was actually the LED pin on it. But I had not one but two leds staying on all the time (besides the power pin).

A closer inspection revealed my unit was another variant called Pro Micro. It turned out this board does not have the led at pin 13. The two leds availables are somehow weird, as one of them is connected to RX_PIN that happens to be pin number 17 but the other led is connected to TX_PIN which, oddly enough, is not associated with a pin number, but two macros: TXLED1; will set it off and TXLED0; will turn that led on.

You can get the Blink program for that board from Sparkfun. And a micro USB cable is needed (similar to the one most Android smartphones phones use).

I am happy to report that at least this device works ok :-)

Thursday, September 04, 2014

Stupidity-R-US

Today, after a long wait, I made my mind to shopping for a brand-new MacBook Air. I went to the Apple store in my city before the summer but I did not like what I was told: They will make me a nice discount for being a university professor but only if I was billed personally. I wanted the machine to be invoiced to my university that would slash the discount seriously. It did not matter that my university was a leading public university in the country. I left the shop shocked and empty handed.

After checking with some colleagues it seems that is Apple's usual policy, though nobody was able to explain why. Anyway, after doing some taxes math I saw it was still more convenient for me to charge the bill to my personal expenses account I hold with my employer so I went for it.

These is more or less my experience today at the Apple Store in Valencia.

Apple rep1 (at the door): How can we help you today Sir?
Me: I would like to buy a MacBook Air.
Apple rep1: Do you have it reserved?
Me: No, I don't. But I do know the model I want. Do you mean I need to make a reservation for a unit?
Apple rep1: No Sir. I did not say you need to book a unit in advance.
Me: I just heard you asking me if I have it reserved ... (odd awkward moment)

Apple rep1: Please come with me so my colleague will handle your order. (We are guided to the MacBook Pro 15" area and we are told to wait there for the representative that will help us next, and so we do).

Apple rep2: (After asking as our names) What can I do for you?
Me: I want to bay an i7 MacBook Air with 8GB of RAM and 512 GB of storage. I want the order to be invoiced to my [employer who is a local] university.
Apple rep2: We'll let me check if we can do that, as the university counter should come here for such an order with the tax number.
(After a while he is back and confirms we can proceed) But now the discount will be much lower than if you were buying the unit personally.
Me: I acknowledge that but insist I want the invoice to be to the name of my university:
Apple rep2: Unfortunately my colleagues that handle business accounts are already off and I am afraid I won't be able to get your university taxes number. Do you have it by any chance?
Me: Of course I have it, it is ******.
Apple rep2: Ah, ok then. Please give company name and address. Would you pay credit card or cash?
Me: I would pay credit card.
Apple rep2: Ok, hold down and I'll be right back.
... one minute or two later ...
Apple rep3: Congratulations on your new MacBook Air. Now my colleague will proceed with the billing.
(Some conversation about the university takes place now, as Apple rep2 is a graduate from my university but I do not think that is relevant to this post).
Apple rep3: These are the details of the transaction (and repeats all my company data). Is that ok?
(he has been frantically using the inch-thick iphone-based device they use as a terminal for a while and eventually produces a ticket which hands over to me).
Me: What is the invoice number? (nothing like invoice number text appears on the ticket).
Apple rep2: (pointing at a barcode) That is it.
Me: Ok thank you very much.
Apple rep2: Would you like to configure your new laptop in the shop?
Me: No, thanks, I will do that at home.
We both say good-bye and I leave the shop with my laptop in a bag.

I really did not like the feeling in the shop. It felt like unless you make clear you know nothing about computers and they do not know how to handle you. I do own several Apple computers I have bought over the years though I am not an Apple fanboy. I think next time I am buying an Apple computer I will buy it online as half of my interactions with the shop representatives were kind of telling me that buying might not be possible at all. I am not asking for pushy salesman but a bit of a profesional salesman would help, even if they are not in their twenties but know the ropes.

As for the Air, what can I say? It is a light but powerful beautiful machine.

Friday, August 15, 2014

Reading binary STL files in Java

I liked the source code from MaskedRetriever user on github and I assumed it would work ok. It seems it was an attempt to create a self-contained tool for 3D printing but somehow it was left at an early stage. But I liked the fact that source code was documented.

My needs were to read STL files and to slice them. As usual you need to get familiar with the different classes involved but it took me not much to get it going. I stripped most of the classes as I was not interested on 3D printing nor on having a GUI for such a tool. However, as soon as I draw the output of the slice process I noticed something was wrong.

While the proper slice looked smooth like the image below:

What I was obtaining was a bit of a mess, though the general ideal was correct. As it was my first attempt I was not sure where the error might be.
After a few tests I realized that the same code would work ok if STL file was ASCII-based but it will fail as above if a binary STL was used instead. 

I had a look at the code and I could see reading a binary STL file involved obtaining float values out of 32-bit IEEE 754 encoded values on the file. It seemed it was there when error was made in the conversion.

Googling around I realized there was a library function in Float class called intBitsToFloat that will turn the 32 bit number into the proper float value. I only needed to get that 32 bit number out of the four bytes read from the file, easy peasy ... or not?

Well, if that were C, and you have b[0..3] unsigned chars, the expression could be something like 
 b[3]<<24 | b[2]<<16 | b[1]<<8 | b[0]  

However Java language considers byte data-type as a signed 8-bit field (-128 to 127). And there is a nasty effect with the sign extension that may make you waste a fair amount of time. In summary, the C version won't work if negative values are involved, and they are most of the time (half of the time I would say). So one way of obtaining the proper 32 bit number in Java would be a bit longer : 

 (b[3]<<24)&0xff000000 | (b[2]<<16)&0xff0000 | (b[1]<<8)&0xff00 | b[0]&0xff