LeJOS PIDController not working, output is zero

June 5th, 2013

This problem had me baffled for more than a few minutes: I wrote a leJOS PIDController loop for a LEGO NXT project that used one motor’s rotation encoder as the input to control another motor’s set point. It didn’t work: the output from the PIDController’s doPID(int) was reliably zero. My loop seemed quite reasonable: read encoder from motor A, apply it to setpoint of motorB’s PIDController, read motorB’s encoder, apply it to motorB’s PIDController as the ‘process variable’, get controller output, apply it with motorB’s setPower() method. It didn’t work!

I found the answer in lejos.util.PIDController’s source, in setPIDParam(int paramID, float value):

case PIDController.PID_SETPOINT:
  this.setpoint = (int)value;
  this.cycleTime = 0;

and in doPID(int processVariable):

if (this.cycleTime==0) {
  this.cycleTime = System.currentTimeMillis();
  return 0;

Setting the setpoint resets the cycle time and the next invocation of doPID returns zero! I don’t understand why it’s necessary to reset cycleTime on change of setpoint, but that’s what the leJOS code does. I kludged around it in my code by only invoking setPIDParam if the setpoint has changed and doPID works as expected.

Save on winter heating with cheap DIY four poster.

December 31st, 2012
With the sides knotted up during the day

With the sides knotted up during the day

I’m not keen on central heating but I don’t like feeling cold in bed. The house we’re currently renting never seems to get very warm: turning up the heating seems only to increase the bills and the ‘musty old house needing thorough renovation’ odours. I made a couple of temporary four poster canopies (faux posters?) to go over the beds, and I’m pleased with the effect. The kids (they call them ‘bed tents’) love theirs too.

The basic idea was to do something inexpensive and quick that would give us the heat retention / draught exclusion of a four poster bed canopy which we could also pack away into storage for the summer. Being easy to wash the fabrics at home was important to us too.

The frame is made from cheap softwood from a local DIY store. It’s held together at the corners with smooth-headed bolts threaded through from the outside into wing nuts on the inside.

I priced up some ideas for a sewn, fitted canopy but nothing came close in price to some ready made flat sheets for the sides and an elasticated fitted sheet for the roof. The canopy is made with 3 flat double sheets: one for ‘head’ of the bed and two to form the sides and opening at the foot of the bed.

Two doubles side by side

Two doubles side by side

The dimensions of ready made double flat sheets available in the UK are such that the opening formed between the head sheet and the two side sheets are almost exactly where you’d want to get into bed / reach out of bed at the sides. The centre foot opening is a handy bonus for us.

Our colour choices were largely determined by what was available at our nearest out-of-town supermarket, so the bed tent on the left in the pictures has a brown sheet at the head and two cream sheets forming the sides / foot. The bed tent on the right has three purple sheets going all the way around the bed and a blue fitted sheet on top.

The sheets for one bed tent cost us about £20, the wood another £25 and fixings a few pounds. The side sheets drape over the ‘roof’ frame so that a little hangs down at the top on the inside. You can see it quite clearly in the top image.

To keep the side sheets hanging in place I use a metre of plastic trunking cut into six 15cm pieces as clips. You can see the short piece of white plastic clip in the top image in the middle between the brown head sheet and red top sheet. The sheets can be thrown over roughly, the clip pops over the sheet and wooden frame, and then you can pull the sheets into tidy position without them sliding to the floor.

The fixings I used were M6 “roofing” bolts and wingnuts, with the rectangular frame made of wood about 100mm x 18mm in cross section. Two holes at each end of each roof member is enough to keep the construction standing up fairly solidly on legs around 34 mm square.

The holes are deliberately over-sized. The feet of the uprights go outside the plan area of the double mattress. The roof members go outside the upright legs, but must fit inside the fitted ‘roof’ sheet. That means the legs are slightly splayed: they’re further apart at the bottom than at the top. I put the frame up with the wingnuts only loosely done up, then adjust the corners by eye until it looks not too badly skewed in any direction, then tighten the wingnuts.

The rectangular roof frame must be cut smaller than the nominal dimensions of a mattress. Fitted sheets fit very tightly onto a squashy mattress, but the wooden roof frame will not deform at all when you try to put on the fitted roof sheet. I had to rework my first frame a couple of times before I learned that lesson!

For such a simple job, it feels luxurious inside compared to sleeping in the large, draughty room. We have somewhat unusual sleeping arrangements while our two children are still young: we all sleep in the same room, mum and dad on one double bed, two kids on the other. The kids love their bed tent. We don’t heat the bedroom and keep a window ajar but their immediate environment is very snug by the time bedtime story / nursery rhyme is over.

I bought a couple of super-cheap USB temperature sensors and collected some data that shows the temperature inside the bed tent on a cold night is a few degrees above the room temperature as it dips into the small hours. I’ll post something about that soon. The small temperature difference was disappointing given the sensation of improvement. Perhaps humidity retention / draught reduction plays a part too?

Update: I hooked up those USB sensors to a web-based logger and recorded data similar to the three days shown here. The two sensors report similar temperatures during the day when the bed tents are knotted up, with about 1° of difference at night-time. The inside-the-tent temperature sensor was about half-way down the bed, so it could also be that temperature around the head-end shows a greater difference. You’ll have to wait for next autumn / winter for that data!

London most polluted capital in EU. 1952 Great Smog. A child dies.

December 5th, 2012

I was surprised to see in an article at the BBC today that London has the worst air pollution in the EU. I remember working in the Square Mile in the mid 80s and having permanently black bogies and a sore throat which only abated when I went on holiday. I thought things had moved on.

In another story a child died after the car she was in was hit by a train on a level crossing. The article quotes people saying that level crossings should be replaced by tunnels or bridges but that the cost of doing so is prohibitive.

I wish we could lower the unladen weight limit for vehicles in built-up areas to somewhere in the region of 100kg, with appropriate power-to-weight restrictions for the 30-40mph speeds in such places.

Air pollution would be reduced by the drastically lowered engine outputs of such vehicles. I suggest air quality would be further improved by the reduction in apparent danger from such vehicles helping more people to consider cycling and walking as modes of transport.

Bridges over level crossings in built-up areas could be much less substantial – and consequently cheaper – structures if they had only to accommodate lightweight vehicles.

What do you think?

Twitter status search / retweet with twitter4j at movethefuckon.org

November 29th, 2012

Just a quickie – I spotted a recurring comment on the web, usually shortly after someone expressed upset at some past event (I think the recent examples were outrage that Mitt Romney lost the US presidential election and / or that Obama won). Readers were apparently convinced that sufficient time had passed since the event for the original poster to get over their upset in more than one reply that said just:


“Cool” I thought “let’s see what’s there!”, expecting to find a Web meme nugget like gt.fo or 5318008.com, but the domain wasn’t registered. So I registered movethefuckon.org, wrote a short, helpful message in a large font, and thought it didn’t really live up to the genre.

The most popular source of “movethefuckon”, according to a popular search engine, is Twitter. I wrote a little code in Java to fetch statuses from Twitter’s JSON-formatted search, extract the data with the Argo JSON library and incorporate a list of recent tweets in the page source.

I’m not sure it’s quite the right thing to do, but on the off-chance that it would encourage people to visit movethefuckon.org, I added a little extra code to retweet new #movethefuckon statuses using the MoveThe_Fuck_On Twitter account. For that I replaced Argo with the authentication, search and retweet methods from Twitter4J, which seemed to JustWork™ (except for some getURL weirdness that was easily worked around).

Enjoy, briefly – and then you know what to do.

Vodafone topup, html character entity problem, proxy error

November 19th, 2012

What do you do when you can’t communicate with a web-based organisation using their own ‘Contact Us’ method? It’s frustrating, so I’m going to copy this here. I’ve just topped up my prepaid mobile phone using a debit card on Vodafone’s website. I got a confirmation text saying

Thank you for your order.TopUp Amount:£10.You bought this on …

It’s not easy to read with that Numeric character reference in there – it should say

Thank you for your order.TopUp Amount:£10.You bought this on …

In a helpful mood, I thought I’d use Vodafone’s email contact form to let them know they are sending mangled texts. I filled in the form (including the surprisingly large number of compulsory personal detail fields) clicked the ‘Next’ button and got this:

Besides pointing out that not being able to communicate with a communications company has in the past convinced me to change telecom provider, that’s all I have to say.

Update: I got an email confirmation 20 minutes later that they have received the contact form.