Roombaduino – part 21 – New joystick module

Next part to get converted to I2C was the joystick module. This time I needed to use an ATTiny84 because I needed 2 pins for X and Y, 1 pin for the button and 1 pin for the LED plus 2 for I2C which means there are not enough pins available in the ATTiny85.

Soldering was a little easier because there are not so many connections required.

I also cut down the size of the module so that I could build 2 and put one either side of the main remote. This makes it a more compact unit.

The obligatory video below shows it working.

 

Roombaduino – part 20 – Soldered LED Module

After breadboarding the LED module, I started on soldering the components. It was after I’d soldered everything and was finishing wiring the connections that I discovered that some joker had replaced my 16-pin sockets with 14-pin sockets and I was missing 2 pins for the last 2 LEDs. There was much wailing and gnashing of teeth as I started again.

This time I made sure I counted the pins and was able to complete the module in a couple of hours.

The ATTiny85 was still running the sketch to count in binary from 0 to 255 so that acted as a great test that the micro controller could control the shift register.

All that was left was to change the sketch so that it could read the data from the main MCU and output a byte to the LEDs. As luck would have it, the value of the button state can be directly applied to the LEDs so that the corresponding LED lights up without any need for translation.

WARNING: There are flashing LEDs in this video.

I build a simple bus with 8 ports where 1 port is connected to the main MCU and the other modules can just plug their 4 pins into one of the other connectors. I’ll make up some simple cables so that I can stop using my breadboard wires.

Next step is to build a joystick module with an ATTiny84 ( with more pins ).

Roombaduino – parts 17, 18 and 19 – Redesigning the remote

After building the new IO board, I started to realize that my idea to have the remote control in modular sections needed a redesign. Each module needed at least 2 17-pin connectors to provide communication from the previous module to the next and that was taking up valuable space on the small perfboard and that required components to be squashed together ( ultimately exposing my lack of fine soldering skills ).

I was looking at controlling multiple OLED screens and found a technique that used a multiplexer to allow the controller to talk to OLEDs with the same address. The multiplexer switches the I2C data line between each screen.

Using I2C got me thinking about how I could redesign the remote control modules with just 4 pins for the bus ( 2 for power and 2 for data ).

To communicate with each module, I needed a smaller microcontroller that would respond to I2C requests from the main MCU ( the ATmega328 ) and send data back. This meant that I could have the module MCU talk to a shift register to determine button presses or to light LEDs.

I chose the ATTiny85 because it was much smaller and didn’t require a separate oscillator for timing and also didn’t need associated smoothing capacitors for power. However, with fewer pins comes less flexibility. The shift register technique needs 3 pins for control ( data, shift and latch ) but I also needed an input pin for the button module. The ATTiny85 doesn’t have enough pins available to do that. By happenstance, I found that I had a solitary Johnson counter that has 10 pins for output and a one pin for clock. Each time the clock pin is taken high, the output pin is incremented so, for example, if the output pin is pin 1, a clock pulse moves it to pin 2 and so on from 0 to 9. When it reaches 9, it rolls back to 0 again. Additionally, there is a pin to reset the counter back to 0. This is important if you want to poll buttons sequentially. The reset allows you to start from a known state.

So, the module uses 2 pins ( clock and reset ) plus the data input line instead of 3 + input. That makes the Johnson counter perfect for the button input module.

I needed to make sure that the ATTiny85 could talk over the I2C bus to the main MCU.

With that testing done, I could then solder up a new Nerduino and button module. I was also able to test that the ATTiny85 could light an LED module. Given that I don’t care about any input on the LED module, I can go back to using a shift register for that module and still only use 3 pins on the module MCU.

Roombaduino – part 16 – Working IO board

In soldering up the new IO board, I had to make some changes to the joystick panel to make coding for the button presses easier. For each shift register, I have 4 pins for LEDs and 4 pins for buttons. If I press a button, I can now use some simple bit shift logic to light up the corresponding LED. Unfortunately, the joystick panel wasn’t wired up like that so I had to de-solder and then re-solder some connections to fix that.

Also, I had the shift register Output Enable pins pulled permanently to ground which meant that every bit that got written is immediately copied to the output pins. Now that I’m shifting data across 2 shift registers, I could see the LEDs flicker. So, I used up one of the 2 remaining pins on the MCU “bus” to be the Output Enable line for all the shift registers.

Now, when I’m writing data, I make OE high, write all the data and then make OE low to copy the data to the output pins. Problem solved.

Next step is to integrate all these changes into the remote control sketch and then work on the Roombaduino code to act on the new commands.

Roombaduino – part 15 – New Input-Output board

After giving it some thought, I realized that using the joystick buttons to cycle through the brushes wasn’t optimal and I have some extra space on the remote control in the form of the T-connector piece that connects that MCU, Wireless transceiver and Joystick input.

 

I have to finish soldering up all the points between the connectors but the key piece is that the pin which is the bit from the shift register as the MSB drops off the end, on the T-connector, should now be connected to the input data pin on the Joystick input. Which means that there are now 2 shift registers to manipulate from the MCU. The first one is on the T-connector and has 4 LEDs and 4 buttons. The second one is on the Joystick input and has 2 LEDs and 2 buttons. I also need to make sure that the pin which indicates a button press is connected across both boards.

I’m also planning to send the status of all the inputs ( joystick position, joystick buttons, 4 input buttons ) as a packet to the Roombaduino.

You may have seen that The Ben Heck Show is also putting together a Roomba project. I’m glad that he went through the same steps I have done and I have a couple more ideas.

Firstly, the Roomba has a command to seek its dock. I’ve built the T-connector IO board so that one of the buttons has a red LED. This will be the one that activates that command. Also, he uses a different set of commands to drive the wheels. I’m going to change the code on the Roombaduino to use the same commands because then I can change how the joysticks operate. I’m planning to control the left and right wheels with their own joysticks so that the Roomba is driven like a tank. It also means that I can send the relative positions of both joysticks and map them to forward and reverse directions. This makes turning while moving forward easier.

Roombaduino – part 14 – Working OLED

The code that I had working on the attiny85 for the OLED didn’t want to work on the ATmega328P even though I made sure that the new pins I’d soldered were fully connected and could light LEDs when sent high.

So, I did some internet searching and found http://blog.oscarliang.net/arduino-oled-display-library/ which does the trick. The Adafruit library seems to be jam-packed with everything and the Arduino IDE complains that there’s a shortage of memory when it’s compiled so any chance to decrease the memory footprint is a good thing. The fact that this OzOled library works is the reason I’m using it. One thing I did notice is that it seems to start the display upside down when compared to the Adafruit and ssd1306 library. I’ll dig into that to see if there’s a simple logic change I can make.

Here’s the obligatory video of the OLED screen working:

 

 

Roombaduino – part 13 – Full Remote Control

With the Roombaduino circuit soldered together, it was time to test the remote control. I found a connection to the left joystick button that had come undone so I re-soldered that. I also move the Roombaduino LED pin and DD connection to different pins on the Nerduino so that I could use the I2C pins for the OLED display at a later time.

With that all done, the software needed some tweaking to allow a joystick button to switch between Passive and Full mode on the Roomba to give me a chance to turn the Roomba off if necessary. Also, the main brush and side brush on the Roomba needed to operate so the other joystick button toggled their movement.

Some improvements to the software, needing some new push buttons on the remote control, will be to allow single-use buttons for each of the functions that are currently being toggled by the joystick buttons. Also, the joystick direction commands should allow the combination of forward + (left or right) and backward + (left or right).

Roombaduino – part 12 – Display

I decided that I needed more visual feedback from the Roombaduino so I can obtain sensor information and display voltage and mode data.
A while ago, I bought a small 128×64 OLED screen from Amazon ( http://www.amazon.com/gp/product/B00O2LLT30) that uses just 4 pins ( 2 for VCC/GND and 2 for I2C [ SDA/SCL ] ). As I’m using the SPI pins for the wireless transceiver and things have been known not to play well with a shared SPI bus, using I2C from the Nerduino seems like a good idea.

Roombaduino – part 11 – Wireless Shield

After proving that the breadboarded circuit worked as expected, I charged up the Roomba battery and soldered up the Roombaduino circuit.
This time, I was using double-sided perfboard which has its pros and cons ( for me ).
The pros are that the perfboard is better quality than the paper single-sided board and that the pads are less likely to melt and detach when I solder.
The con ( for me ) is that because the pads are double-sided, the solder flows through to the other side. So, if I’m not soldering a component and I want to lay down a power rail, the solder forms in a bump on the other side of where I’m soldering the rail.
I’m sure other people are perfectly happy with it but it doesn’t work for me so I’m going back to single-sided.

I soldered up 3 parts to the circuit.
Firstly, the connection from the Roomba to the circuit through a 5v power regulator. This allows the Nerduino to be powered from the Roomba without needing a separate power input.
Secondly, the 5v to 3.3v power regulator to provide power to the wireless transceiver.
Thirdly, the connection from the Nerduino to the wireless transceiver using the SPI pins.

While the circuit looks compact on the front, I’m still not happy with my wiring ( but I don’t want to take it apart again – maybe in the future ). I need to be more methodical about how I wire stuff to make it more tidy.

What I *am* pleased about is the shield-like design so that the connection to the Nerduino is done through pins and the Roombaduino circuit can stack on top.

Roombaduino – part 10 – A moving Roomba !

With working transceivers, the next step was to get the Nerduino talking to the Roomba itself.

I had breadboarded a Nerduino, the transceiver and the 5v voltage regulator to manage the Roomba power and serial connections.

IMG_4012

 

The circuit worked but it was a royal pain to get the Roomba to respond. I verified that the serial cable I made was correctly soldered to the right pins of the 7-pin DIN connector and that none of the connections were bridged with other connections if they weren’t supposed to be. I had already demonstrated that I could power a Nerduino from the Roomba itself there was a possibility that the RX and TX pins were not getting data from the Roomba.

I connected them up to my USB-to-Serial converter and opened up a terminal session. When the Roomba is charging, it sends some plain text data and I could see that being sent correctly. That meant the TX pin was working at least. However, whatever command I sent to the Roomba was ignored.

Serial communication can be tricky when an incorrect baud rate is used. If data is transmitted at a different rate, things get garbled. By default, the Roomba 565 is expecting 115200 8-N-1. That can be reduced to 19200 by powering on the Roomba and holding the Clean button for at least 10 seconds. The Roomba will emit a series of descending tones to confirm.

However, there’s no way to set it back to 115200 other than through serial commands or by removing the battery for a moment.

I chose option B and the Roomba played a tune to indicate it was reset.

Once I had done that, it started responding to commands.

Additionally, I had read older posts that indicated it’s helpful to specify the baud rate at startup. So, I included that in my setup code.

The Roomba Receiver part of the code is below. If anyone reads this, please note that it doesn’t work as well as it can. The Roomba responds and will move in the correct directions but there are a couple of issues:

  1. At some point, it stops responding to commands. I don’t know if this is battery-related. I’ve read that the interface “shuts down” if the battery is below a certain level. At the time of writing, the battery *was* low so I have it on charge overnight. I’ll test it later.
  2. When the Roomba is in Full Mode, it doesn’t respond to button presses. In particular, it doesn’t respond to the power button. Because of point #1, the Roomba can be stuck in a spin or moving forward or backward. There’s no way to reset the Roomba other than to pick it up and unscrew the bottom plate to remove the battery. I tried to implement a fail-safe so that I made a digital pin active HIGH and, if it ever went LOW, the Nerduino should transmit a RESET command to the Roomba. If the Roomba isn’t listening for commands, that approach won’t work. I’m thinking now that I have to monitor the battery level to understand when the interface might stop listening and reset if the battery is just above that level.

The video below shows the Roomba responding to commands for a short while:

 

 

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

#define RADIO_CSN_PIN 9
#define RADIO_CE_PIN  10

RF24 radio(RADIO_CSN_PIN, RADIO_CE_PIN);

#define DD_PIN 2
#define LED_PIN  A5
#define SAFETY_PIN A1
#define SAFETY_OUTPUT_PIN A4

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

void setup(void)
{
   pinMode( SAFETY_PIN, INPUT );
   pinMode( LED_PIN , OUTPUT );
   pinMode( DD_PIN , OUTPUT );

   Serial.begin( 115200 );

   digitalWrite( LED_PIN, HIGH );

// Wake up Roomba
   digitalWrite( DD_PIN, HIGH );
   delay( 100 );
   digitalWrite( DD_PIN, LOW );
   delay( 500 );
   digitalWrite( DD_PIN, HIGH );
   delay( 2000 );

// Initalise ROI
   Serial.write( 128 ); // START
   delay( 50 );
   fullMode();

// Set baud to 115200
   Serial.write( 129 ); // BAUD
   Serial.write( 11 ) ; // 115200

// Indicate that setup is complete
   int i=0;
   for( i = 0 ; i < 10 ; i++ )
   {
      digitalWrite( LED_PIN, HIGH );
      delay(50);
      digitalWrite( LED_PIN, LOW );
      delay(50);
   }

   radio.begin();

   radio.setRetries(15, 15);
   radio.setPayloadSize(8);
   radio.openWritingPipe( pipes[1] );
   radio.openReadingPipe(1, pipes[0]);

   radio.startListening();
}

void resetRoomba()
{
   Serial.write( 7 );
   delay( 50 );
}

void loop(void)
{
   int command;

// Check the safety pin
   if( digitalRead( SAFETY_PIN ) != HIGH )
   {
      digitalWrite( SAFETY_OUTPUT_PIN, LOW );
      resetRoomba();
   } else {
      digitalWrite( SAFETY_OUTPUT_PIN, HIGH );
   }

   if ( radio.available() )
   {
      digitalWrite(A5, HIGH);

      bool done = false;

      while (!done)
      {

// Fetch the payload, and see if this was the last one.
         done = radio.read( &command, sizeof(int) );
         delay(20);
      }

      digitalWrite(A5, LOW);

      if( command=='F' ) goForward();
      if( command=='B' ) goBackward();
      if( command=='L' ) spinLeft();
      if( command=='R' ) spinRight();
      if( command=='S' ) halt();
      if( command=='1' ) safeMode();
      if( command=='2' ) fullMode();
   }
}

void goForward() 
{
   Serial.write(137);   // DRIVE
   Serial.write((uint8_t)0x00);   // 0x00c8 == 200
   Serial.write(0xc8);
   Serial.write(0x80);
   Serial.write((uint8_t)0x00);
}

void goBackward()
{
   Serial.write(137);   // DRIVE
   Serial.write(0xff);   // 0xff38 == -200
   Serial.write(0x38);
   Serial.write(0x80);
   Serial.write((uint8_t)0x00);
}

void spinLeft()
{
   Serial.write(137);   // DRIVE
   Serial.write((uint8_t)0x00);   // 0x00c8 == 200
   Serial.write(0xc8);
   Serial.write((uint8_t)0x00);
   Serial.write(0x01);   // 0x0001 == spin left
}

void spinRight()
{
   Serial.write(137);   // DRIVE
   Serial.write((uint8_t)0x00);   // 0x00c8 == 200
   Serial.write(0xc8);
   Serial.write(0xff);
   Serial.write(0xff);   // 0xffff == -1 == spin right
}

void halt()
{
   Serial.write(137);   // DRIVE
   Serial.write((uint8_t)0x00);
   Serial.write((uint8_t)0x00);
   Serial.write((uint8_t)0x00);
   Serial.write((uint8_t)0x00);
}

void fullMode()
{
   Serial.write( 132 ); // FULL MODE
   delay( 50 );
}

void safeMode()
{
   Serial.write( 131 ); // FULL MODE
   delay( 50 );
}