Sunday 12 November 2017

Tropical Pallet Wood Carpentry



When the MRT project finished last July I had been almost three years continuously on-site and needed a change. A couple of new houses were being built near us and I noticed a lot of lumber were being burnt. Some of it had been used as concrete molds but most of it were used pallets.

I dragged some of the lumber home bought some cheap hand tools and dismantled the pallets.
It is quite a satisfying experience ripping apart stuff with crowbars and sledgehammers but I quickly found that extracting nails reliably required some finesse. The claw hammer and the crowbar broke the nail heads off too often, and all too often in the best pieces.

I bought an old-fashioned pair of pincers and by the third pallet could get every nail out. Even when the claw hammer broke the heads off the pincers could still save the day. This meant I could work the pallets quickly with the crowbar and hammer and revert to the pincers when I had a stuck nail.

When the wood was planed, much to my surprise some were hardwood: keruing, meranti and nyatoh. Some were almost scrap wood (and rightly so given most are burned), part-bark or knotty but a lot were usable. Meranti is endangered; it seems almost criminal to burn it.

In secondary school (high school) I did three years of Industrial Arts. It is a little like trade school- we did electrical wiring, engine maintenance, metalwork and woodwork. I loved the wood work and became really good at it. It helps if your family has a hardware store and you can get your lumber replaced when you mess your project up.

I approached my new hobby like I do my design: I searched the Internet. It was a real eye-opener. Back in the 1970s we used hand tools: jack planes, mitre gauges, set squares, handsaw, chisels. Now there are power tools for everything and you can work really fast. There are power jointers, table saws, power sanders, jigsaws, circular saws. For measurement you now have lasers which actually trace out a true line on the workpiece itself without marking it.

And youtube now has a video for nearly any aspect of woodwork, some by master carpenters. Instructables has some really good designs with complete instructions. It is good to be alive.

I bought a power jigsaw- I thought I would start small in case I lost interest in it. That enabled me to repair a long-broken shed door and seal up the shed so that rats could no longer set up there.
Power Jigsaw

Next I got a power handplane. That was less satisfying- youtube had not reckoned on tropical hardwoods. In no time the blade was blunt and fairly skipped over the hardwood like a water skier. The jigsaw suffered less - it was capable of cutting through metal.
Power plane
That dented my confidence in the new power tools. I had planned to get a circular saw next, but the circular saw looked like a very dangerous bit of kit (danger is relative - I have a chainsaw for over 20 years now) and I had visions of it jamming on tropical hardwood, kicking back and amputating some critical piece of anatomy.
Circular saw can kick back
Remembering my success with the pincers, I went old-school. I bought a tenon saw instead.
Old school: tenon saw
I practiced the youtube master carpenters' techniques on pallet wood with some success, starting on the foreign temperate soft woods. The hardwood was extremely tough going, especially an antique piece of chengal (chengal is nearly extinct in Peninsular Malaysia. Our family store used to sell it but I have not seen it for 30 years). The chengal was so hard the tenon saw was heating up like it was sawing metal. I was not going to give up on it. 

Razor sharp: Bahco Superior 2600

I got out the sharpest saw I have, a Bahco Superior 2600 which for 20 years I have been using to trim hardwood trees in the garden. That did the trick: between the youtube sawing techniques and the Bahco I could make true cuts without getting too tired.Because the Bahco saw groove was quite big, I needed to be very careful marking the wood- it is easy to end up with a piece that was too small.

I needed something to replace that power plane, and was looking for the only thing I was familiar with, a Jack Plane. But the power planes are all the rage these days and the jack plane was more expensive (over RM300) than its powered replacement! And it took two weeks to arrive.
Jack Plane

Happily I found an extremely cheap and suspect pair of hand planes from my favorite neighborhood hardware store. They were so cheap and nasty (RM40 for two) that the proprietor was reluctant to sell it, fearing I would have a bad experience with it.

Cheap and cheerful
True enough the handle was too small and skinned my right hand after a day's use. And the depth setting would not hold when in heavy use, i.e., when used on hardwood. Maybe it was because I did not expect much from the planes, but they did the trick and finished up where the power plane failed. I had to sharpen it a few times a day, but I was already good at sharpening (another youtube skill). The constant dismantling also made the wandering depth setting a little more tolerable.

I was all set. First I needed a workbench so the first batch of reclaimed wood became just that.

Palletwood workbench

Next I made a little stool to go with the bench. This time instead of building it with butt joints and holding it together with steel screws I got a little more ambitious and went for lap joints and wood glue. The glue was a revelation. It was incredibly strong, stronger than the wood and hardwood at that.

I had to relearn my jointing and sawing; the stool ended up a little squiffy but was very strong.



That was August. It is now mid-November and the rainy season means I am indoors a lot and pallet woodwork is starting to look inviting again. I thought I would try for another table, but this time better finished, even stained and polished. I put in an online order for that Jack Plane.

The tabletop is mostly 4"x0.5" pallet planks planed true by hand and glued together. It is a little rough in spots because of the odd knot and because I had failed to match the grain when I lined up the boards to be glued. But it is mostly there and I figure it can be smoothed with a sander.

Palletwood table top. Note the magic glue. You can never have too many clamps
 But I would get to try out my new sash clamps. Another revelation: the old ones were very heavy and expensive. These new clamps are a dream.

Because of the size of the top, the plane was to hold it in place with glue, then reinforce it with a few steel screws. Fastening hardwood with screws with precision was unexpectedly difficult. The holes need to be drilled in advance or the wood might split especially at the ends.
More power! The battery powered drills do not play well with hardwood.

More often the wood was so hard the screw heads simply stripped or the screw got stuck. There are very few thing more disheartening than stuck screws. Another unpleasant surprise was a stuck power drill (used as a power screwdriver) can blow the house mains fuse, but mostly you just released the magic smoke.

Going back to basics was not an option this time. Using a hand drill on hardwood is far too exhausting.
One for the museum: a hand drill

After the glue was applied, aligning the holes became difficult, after which the screw might again get stuck. Sometimes a screw does not penetrate the second piece properly and when forced in can act like a little crowbar and pry the pieces apart.

Then there is the drilling. Power drill bits for woodwork was unexpectedly difficult to buy, especially the smaller sizes. And the smaller bits are the ones that tend to break. After breaking all the bits in a single session I ended up using bits meant for metalwork. These went in readily enough, but the flutes do not excavate the waste wood well enough, resulting in a stuck bit. Even the carpenter's pincers were no good on a stuck bit- the high-speed steel is too brittle and snaps off.

Eventually, I found the waste wood can be excavated by repeatedly withdrawing the bit. The advantage of the metalwork bit is that if the wood is too hard to drill properly, I can simply set the speed to high and the resulting friction simply burns the bit through the hardwood. This should be done sparingly to minimize the burnt area. Adding small amounts of WD-40 also seems to help. This is very hard on the power drill so you need a good one.
WD-40: your magic cure for all things stuck

The glue dries in 24 hours, so wish me luck. In the meantime, in the spirit of Tim Allen's Home Improvement, "More power!" to you.

Wednesday 8 November 2017

Controlling the Raspberry Pi Robot Platform from an Android Smartphone

It is sometimes more convenient to control the raspberry pi robot platform from the smartphone.
Raspberry Pi robot platform with smartphone HTML RC control

Perhaps it is because I am now lugging around a 17" Asus X751L laptop.

There are several options. The quickest way is to ssh into the Raspberry Pi using an app like Juicessh. I can then reuse the C programs forward, left and right.

This turned out to be quite limiting, especially at my age. The smartphone keyboard and screen are really too small for the Linux command line interface to be used for a useful length of time.

But that's what rapid prototyping is all about. We build another one as fast as possible, preferably with available resources. Time for the next iteration.

Rather typing a separate command (in this case a bash script) perhaps it is better to make a program to issue direction commands on a single key press. This takes us to the C program arrowkeys.c

The process is really quite quick and simple: I just googled for the code, starting with general terms lile 'linux', 'keypress', 'C language'. C just happened to be the language I am most familiar with. 'python' or your favoorite language should work just as well.

This search leads me to ncurses, the library that I need. We refine the search further by adding 'sample' and 'code', and this time google leads us to to the function I need, wgetch. This in turn leads us to the article 'A Simple Key Usage Example'. I copied the code wholesale, and just had to add the C library stlib.h so that the keypress code can use system() to invoke my direction programs.

            There is the program. You compile it thus:
gcc -lncurses -o arrowkeys  arrowkeys.c

/******************************************************************************
 *                                                                            *
 * Simple program to issue motor commands in response to keyboard arrow keys  *
 * Copied from:                                                               *
 * http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html                  *
 *                                                                            *
 * 2017-10-24 CM Heong                                                        *
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>

#define WIDTH 30
#define HEIGHT 10

int startx = 0;
int starty = 0;

char *choices[] =
{
  "Forwards",
  "Back    ",
  "Left    ",
  "Right   ",
  "Exit",
};

int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);

int main()
{
  WINDOW *menu_win;
  int highlight = 1;
  int choice = 0;
  int c;

  initscr();
  clear();
  noecho();
  cbreak();     /* Line buffering disabled. pass on everything */
  startx = (80 - WIDTH) / 2;
  starty = (24 - HEIGHT) / 2;

  menu_win = newwin(HEIGHT, WIDTH, starty, startx);
  keypad(menu_win, TRUE);
  mvprintw(0, 0, "Use arrow keys to go up and down, Any other key to exit");
  refresh();
  print_menu(menu_win, highlight);
  while(1)
  {
    c = wgetch(menu_win);
    switch(c)
    {
      case KEY_UP:
         highlight=1;
         system("/home/heong/piface/piface-master/c/forward 100");/*2017-10-31*/
      break;
      case KEY_DOWN:
         highlight=2;
      break;
      case KEY_LEFT:
        highlight=3;
         system("/home/heong/piface/piface-master/c/left 100");/*2017-10-31*/
      break;
      case KEY_RIGHT:
        highlight=4;
         system("/home/heong/piface/piface-master/c/right 100");/*2017-10-31*/
      break;
      default:
        highlight=n_choices; /* exit */
      break;
    }
    print_menu(menu_win, highlight);
    if(highlight==n_choices)  /* Exit chosen - out of the loop */
      break;
  } 
  mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]);
  clrtoeol();
  refresh();
  endwin();
  return 0;
}


void print_menu(WINDOW *menu_win, int highlight)
{
  int x, y, i; 

  x = 2;
  y = 2;
  box(menu_win, 0, 0);
  for(i = 0; i < n_choices; ++i)
  {  if(highlight == i + 1) /* High light the present choice */
    {  wattron(menu_win, A_REVERSE);
      mvwprintw(menu_win, y, x, "%s", choices[i]);
      wattroff(menu_win, A_REVERSE);
    }
    else
      mvwprintw(menu_win, y, x, "%s", choices[i]);
    ++y;
  }
  wrefresh(menu_win);
}

Now arrowkeys worked great on the laptop. But on the smartphone, I had to take my eyes off the robot and look at the screen, and this gets wearing very quickly as there a a lot of motion commands.

What I need is just 3 very big buttons, for forwards, left and right. The obvious way is to use an App, perhaps even write one using the MIT App Inventor.

At this point my smartphone,a Nexus 5 failed. I quickly reverted to my trusty Nexus 1, but in my haste to install the SIM card, I broke the phone's SIM connector. The second backup is a Leonovo A390, which did not have much disk space left after installing Whatsapp.

But we can work around this: if I ran a website from the Raspberry Pi, I can invoke the C motion programs from php, via the function exec(), very similar to the trusty system() function.

This means however I have to come up with the button program in html. The added advantage is it will work both in the laptop and the smartphone.

As you suspected, it is back to google. 'html', 'button' and 'system' eventually led me to the 'form', 'exec' keywords. From there the search narrows down to w3schools.

Not knowing web programming should not stop you: it didn't stop me. If you have a background in at least one computer language you should get by. The result would not be pretty code, but you will have a workable (well, sort of) prototype you can show as plausible progress.

From there on, a full day's frobbing to get the buttons screen just right, in the process discovering CSS 'style' in the process. The resulting program, robot.html and upbutton.php:

$cat robot/piface/piface-master/robot2/robot.html
   <!-- \/ starthtml -->
<html>
 <head>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="keywords" CONTENT="Heong Chee Meng electronics engineer Android Raspberry Pi Robot Platform Control">
   <META NAME="description" CONTENT="Heong Chee Meng's robot control website.">
   <META NAME="author" CONTENT="Heong Chee Meng">
   <TITLE>Heong's Robotic Control Website</TITLE>
 </head>
Heong's Robot Platform Control Website
<p style="float: left; width: 33.3%; text-align: center">
<form action="upbutton.php" method="post">
  <button type="submit" name="buttonu" value="Connect"><img width="180" height="180" alt="Connect" src="./upbutton.svg" align="center"></button>
<! </form>
<p>
<p style="float: left; width: 33.3%; text-align: left">
<form action="leftbutton.php" method="post">
  <button type="submit" name="buttonl" value="Connect"><img width="180" height="180" alt="Connect" src="./leftbutton.svg" align="left"></button>
<! </form>

<p style="float: right; width: 33.3%; text-align: right">
<form action="rightbutton.php" method="post">
  <button type="submit" name="buttonr" value="Connect"><img width="180" height="180" alt="Connect" src="./rightbutton.svg" align="right"></button>
<form action="bleh.php" method="post">
</form>

<p>
</BODY>
</html>
<!-- /\ end html  -->

$cat robot/piface/piface-master/robot2/upbutton.php
<html>
<body>
<article>
<?php
  if (isset($_POST['buttonu'])) {
    $result = shell_exec('ls -l upbutton.svg');
    echo "Done!<pre>$result</pre>";
  }
  if (isset($_POST['buttonl'])) {
    $result = shell_exec('ls -l leftbutton.svg');
    echo "Done!<pre>$result</pre>";
  }
  if (isset($_POST['buttonr'])) {
    $result = shell_exec('ls -l rightbutton.svg');
    echo "Done!<pre>$result</pre>";
  }
?>
</article>
</body>
</html>

You can get the button images from here. Put the files into a subdirectory 'robot' in your apache directory, which in Slackware should be:

/var/www/htdocs/robot/

You start the webserver with

chmod +x /etc/rc.d/rc.httpd

/etc/rc.d/rc.httpd restart


The next step would be to have the phone and the Raspberry Pi log into the same wifi hotspot. This is straightforward if you are at home, just have both your Pi and smartphone log into your wifi hotspot.

If you are on the move (say you need to demo your new prototype at a client's place), just set your smartphone to be a wifi hotspot and have Raspberry Pi connect to it.

If you use static IP (say your Pi is 172.16.1.1) or if you use a DHCP server, the command

dhclient -v wlan0

will display the Pi's IP.

From there it is just a matter of typing in the link into your favourite browser - I used Firefox:

http://172.16.1.1/robot/robot.html

By now I have a spanking new Samsung Note 5, which unhappily was slimmer than the stack of RM50 bank notes needed to buy it.

After playing RC car with the robot (I've always wanted one!) for a while it would be nice to be able to do this remotely, like, over the Internet. This would have telepresence features. Happily, you just need to configure your home wifi modem router to host the Raspberry Pi website, and the same setup should work.

Now that I have the Note 6 I just needed to complete my search for the Android App to do ssh commands with big buttons. The first hit for 'ssh', 'button', 'app' turned up HotButton SSH. Now this should be a lot less painful to get working than html and the only damage would be to my pride.

 
The HTML version's advantage is that it is the same whether you used a desktop, laptop or a smartphone. Best of all, it is easy to link up as an Internet of Things (IoT) device. If you were deploying an interface for, say an MRT SCADA system for both the Station Control Room as well as mobile workers this would be an advantage, especially with training costs.

The moral here is rapid prototyping simply uses the resources on hand. Time is the essence- often it is much easier to find out the faults if you (or the customer) have some form of the prototype to work on. The individual motion programs were not suitable for repeated use, and arrowkeys.c although easy to do (with my skill-set) proved OK for laptop use, but using a laptop proved inconvenient.

It sounds trendy. Indeed DevOps uses a similar approach for its 'Continuous Delivery'  portion, but this approach would sound familiar in the US during the Great Depression.


Happy trails.