This forum is in archive mode. You will not be able to post new content.

Author Topic: XChat meets Arduino  (Read 2592 times)

0 Members and 1 Guest are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
XChat meets Arduino
« on: June 09, 2012, 10:54:03 AM »
Sine my last hardware post, I decided to give you guys Arduino. I will admit it is a lot easier and will get you writing firmware faster than full AVR dev. So, this will be based on the Arduino bootloader and IDE. But don't worrie, I'll get you up and running faster than with core AVR because Arduino abstracts a lot of things and cuts compiling to pressing a button.


Reasons
I leave my XChat client running all the time, and like to know when my name is mentioned in le room. So, not messing with my Arduino in a while, I decided to create a physical notifier of when I am mentioned. Kind of like a mail box you find outside. This doesn't have to be Arduino specific, you can modify the code and log your name mentions without the Arduino. But, regardless, here it is.



A few things you need
Arduino (I'm using Duemilanove not Uno)
Ubuntu 10.04
XChat2
Arduino IDE


I would recommend doing a [sudo apt-get update] (would have used code tags, but they where not working, all text up till the next code tag was being highlighted no matter what I did, meh.) after the IDE install. If not, you may run into errors with xchat and the python plugin. These are also what I used, your install of *Nix could differ and you will find more here.  Windows users, you pretty much just have to grab the Arduino .exe from the site and are ready to go.


After What I need
You will need to know what serial port your Arduino is on. Windows, it will be a COM port. starting from COM1 to COMn. You can find what port your Arduino is on by looking in the device manager. *Nix people, you can do a
Code: (bash) [Select]
dmesg | grep tty and look for either the /dev/ttyUSBn active or /dev/ttyACMn for the Uno boards because they don't use FTDI anymore. If you have the Arduino plugged in, and have le IDE, you can check under Serial Port and see which one your board is on. You will see the relevance shortly.


Hardware
We will start with servos. Servos are a special kind of motor that can turn to specific degrees of rotation. Well, pending on quality and mechanics, roughly 110 in degrees of freedom. Kinda roughly 90 degrees in either direction, pending on the make and model.


Servo's work by sending them a series of PWM or highs and lows, zeros and ones TTL. Duty cycle does play a factor and the signal requires to be sent at 20ms intervals. Basically, it works as an almost analog PID system using a potentiometer and other discrete components to correct the error levels of rotation.


You can hack a standard servo and create a torqued continues rotation motor, which is a base for a lot of DIY robotics locomotion (wheel based anyway).


Let's build it!
Okay, you need three things. The Arduino, 3 wires, and a servo.... well and the USB cable that came with the Arduino and a computer.


All servos have three leads. Power, Ground, and Signal. Ground is always black or really dark like brown. Power is usually red or orange. The signal is either yellow or white. You might have to check your datasheet that came with the servo, because companies don't always conform to standards. Plug power into the +5 on the Arduino, located to the bottom of the board, ground to ground bottom or top labeled GND, and signal to digital pin 11 (also has PWM labeled with it).


Firmware
Fire up your newly installed Arduino IDE. I placed it in my home dir. This IDE is based on Processing and java, food for thought.


Make sure the proper board is selected. I am using the Duemilanove with the ATMega328 chip. This does make a difference because like mentioned earlier, Uno doesn't use FTDI anymore, and there are more flavors of Arduino out there, like the Nano and boards using the atmega128.


Under Tools->Board check the one your using.
Under Tools->Serial Port check the one your using. *Nix is \dev\ttyUSBn or \dev\ttyACMn; Windows it will be COMn, Mac will be fuck you  .

This would be where to get started with just servos and PWM with core AVR dev. It only goes over PWM and not serial communications. To get the example below working, you will need too look into the serial protocol on the hardware level. Google 'AVR serial read' to learn more how to do it without Arduino because I am not going to cover anything non-Arduino in this.

Le code:
Code: (cpp) [Select]

//Yes, this is how simple hardware can get.
//Firmware to physically notify me when someone has mentioned my name
//    in le IRC channel. There is an accompaning xchat2 plugin to send said
//    serial data that this will read in.


#include <Servo.h>
 
Servo leflag; 
int pos = 0;   
int buffbyte = 0;


void setup()
{
    //baudrate/clock for comunications
    Serial.begin(9600);
    leflag.attach(11);
    leflag.write(0); 
}


void loop()
{
    //If we have something to read, then read it
    if (Serial.available() > 0)
    {
        buffbyte = Serial.read();
        Serial.println(buffbyte);
        //If ascii A, move servo
        if (buffbyte == 65)
        {
            leflag.write(0);
        }
        //If ascii B, move servo
        else if (buffbyte == 66) //ascii B
        {
            leflag.write(90);
        }
    }
}


XChat2
I have to give it to XChat, great client. I used Irssi for the cli interface cause I was a fanboy, but once I decided to try XChat out, I never looked back. I kinda have to give credit to Kulverstukas on this too because he gave turned me on to plugins.


Anyway, if you want to load this on XChat startup, place it in the .xchat2 floder located in
~.


We will first open the serial port thanks to pyserial, if I'm not mistaken SL4A has pyserial support, if not it has pybluez which is the bluetooth equivalent to le hardwire and could be used for mobile bluetooth hacking. Or if you want to take this project wireless and bring in a xbee or something  .


The rest is pretty much self explanatory. I do add an unload hook just to release the handle to the serial port. I noticed this matters more in Windows than *Nix. In windows. if the port was active and being used by my program or the Arduino IDE, I could not use it. Like if pyserial had a handle still in memory using the port, nothing else could touch it, but in *Nix I was never thrown any exceptions stating the port was in use. I could detach and reatach without closing the connection to the serial port with out any hiccups. So windows users, this might need some refining. Be sure to close all handles to the serial socket before closing or else you'll get errors with testing or down the road.


The rest
The rest of the plugin has a command for me to reset the Flag, by sending ASCII "B", which lowers the flag to show I have seen what has been said. I also log what has been said to a text file, and have a command to show said text. I was going to use Tkinter for a GUI to integrate with XChat but cam across too many errors and it was crashing XChat (sorry admins for le un needed activity on le IRC). For testing I adapted a bot I wrote a few years ago to say my name in sentences on command on FreeNode in a private channel which is where all testing should be held and not in the IRC rooms of evilzone (tis privately hosted).


The script will open gEdit with what has been said if the given command is supplied, and there is even a rm if the logs get to be too much. I know you can do this while gEdit is open or at your own free will, which is why I added a check to see if le file still exists.


Wrap up
All in all, this was meant to get you guys more familiar with the hardware aspects of breaking and building things. At the least you learned about PWM and serial comunications which is a big part of EE. But really, for further information on PWM, they also apply to LEDs ever wonder where the 'breathing'(first 10 seconds to see what I'm talking about, don't let the rest throw you off, it's PWM via a 555 timer) effect comes from? Also, PWM controls regular DC and AC motor speeds by turning the motor on an off really quick. Although motors and rotary encoders are a whole new thread.


Also, Arduino bashers will be telling me that this is completely overkill using an ATMega328 for just a damn servo when the parallel port and a 555 timer could yield the same results, to them I say go suck one. I will be adding an LCD display telling me in real time who said what when, along with other features, such as a mood lamp displaying the frequency of my name, to this once my broke ass has some money; upon other endeavors to add to le Arduino that is going to stay plugged into my box. When I have the cash, I'm going to make a speedometer to clock how fast my dog can run through the house as an example and this setup will always be online and could clock any given time my dog decides to have ADD.


So........ profit? Or discuss, or not. This love spreader is going to bed.
« Last Edit: June 09, 2012, 11:11:17 AM by techb »
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: XChat meets Arduino
« Reply #1 on: June 09, 2012, 01:34:09 PM »
Lol! You're seriously taking the xchat plugins a bit further xD
I would love to see how it actually looks :P keep on updating!

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: XChat meets Arduino
« Reply #2 on: June 09, 2012, 06:14:33 PM »
Lol! You're seriously taking the xchat plugins a bit further xD
I would love to see how it actually looks :P keep on updating!


I plan to have a video of it shortly.
>>>import this
-----------------------------

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: XChat meets Arduino
« Reply #3 on: June 12, 2012, 05:23:24 AM »
Video found here. Sorry about le quality, was shot with my phone because I'm too poor to afford a proper video camera :'( .
http://www.youtube.com/watch?v=YEiN6Dk2Jbw


Not shown: Using the /gotit command will put the flag back down.
/check will open Gedit with a log of things said with my nick
/rm will remove the file.
The commands weren't shown because it is pretty self explanatory.


TODO:
Add LCD to scroll messages containing my nick.
Voice recognition.
Idk, this is overkill anyway, but meh.
« Last Edit: June 12, 2012, 05:27:41 AM by techb »
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: XChat meets Arduino
« Reply #4 on: June 12, 2012, 08:08:25 AM »
ahahaha you actually raise a physical flag xD I thought you use some screen or something... epic shit dude :D
But I imagine it could get annoying at times. And how does it handle multiple highlights at once?

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: XChat meets Arduino
« Reply #5 on: June 12, 2012, 08:15:49 AM »
It just stays up. I can look at the backlogs with the /check command. Won't go down until the /gotit.
>>>import this
-----------------------------

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: XChat meets Arduino
« Reply #6 on: June 12, 2012, 08:22:43 AM »
I can't imagine what your house looks like....I just picture random things going off and making noise as one walks around your house.

Nice work
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: XChat meets Arduino
« Reply #7 on: June 12, 2012, 08:27:31 AM »
I can't imagine what your house looks like....I just picture random things going off and making noise as one walks around your house.

Nice work

I do have a speedodmeter in the works to clock how fast my pets can run. And once I get my room mates Kinect up and running under Linux it will be all over lol.

On the same note, wifey thinks I have a hording problem with all the circuit boards laying around.
>>>import this
-----------------------------

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.