Arduino command protocol

Arduino,C/C++ — squarism @ 11:40 am

UPDATE: Use CmdMessenger instead of Messenger.

Here’s my IRC Arduino Bot. It uses a regular Arduino 328 and an Ethernet Shield both from sparkfun. As for software, I’m using the Ethernet2 library (see my previous post about this), the WString library and a homerolled IRC protocol parser. The breadboard’s power is connected to arbitrary pin 5 and some resistors to keep the LED from burning out.

arduino_irc_light

Basically, my bot joins an IRC channel and then listens for PRIVMSG commands starting with a password. It takes those commands and controls an LED. For example, I’d send this privately to the Arduino:
command password LEDON

And then the red LED comes on. I tell it “LEDOFF” and it turns off. Ok, it’s not a new RFC spec worthy of IEEE recognition and international adoption. But it got the job done in a human-readable manner. Previously on my facebook status light project, I had done much of the processing on my laptop and only send hex codes to the Arduino to light up LEDs. The difference now is that the Arduino is doing the processing and no computer is needed.

While I was working on this little project, I had the bot join the channel and announce itself.
irc_log

At one point, I was working on code and then my bot would disconnect. I checked the serial monitor and the server seemed to drop me after a few minutes. The channel would say that I timed out. I realized that I wasn’t responding to the PING from the server. So I threw in some code that checks for anything from the server that starts with “PING :”. I then respond with “PONG”. I remember seeing PING?/PONG! messages in mIRC back in the day. Now it makes sense why mIRC was doing that in the console window.

It works great and I was excited about how much this little board could do in 14KB. And then I kept testing it. After about 7 or 8 “turn on” and “turn off” commands, the Arduino wouldn’t do anything anymore. It’s like it just froze. If I typed 5 commands, it’d stay connected for a long time. But every time I’d send it 7 to 8 commands, it would lock up. And by lock up, I mean the commands wouldn’t work anymore and it would time out from the server. WTF. So close!

So I figure that it’s something to do with pointers and memory. I really don’t have a solid grasp on pointers and C. I got a lot of this working by iterative experimentation over many days. So I was looking for a better way to send human readable commands to my bot. By human readable I mean something that works like a unix command “command arg1 arg2″. Of course this human readable bit introduces strings which is tricky enough in C (for me) and even worse on the Arduino. I figured this was a problem that someone smarter than me had solved.

I found a library called Messenger. It’s pretty simple to install, just throw it in your ~/Documents/Arduino/libraries folder on Mac and um … the equivalent on Windows? There are examples in the Messenger folder that you can checkout. HOWEVER the whole point of me posting this big long thing is the following.

The example checkString really threw me for a loop. It did exactly what I need it do to in a much cleaner way. I uploaded to the Arduino and then broke out to a shell.

$ screen /dev/tty.usbserial-A9005bCr 115200

Substitute your virtual usb device for the /dev/tty path. Note that the sketch uses 115k serial speed. You won’t see anything when you type but if you hit “enter” (to clear the buffer) “on[enter]” in screen LED 13 will turn on. Type “off[enter]” ([enter] means the enter key) and it will turn off. Great! Exactly what I need. But then I tried typing “on” then “off” then “muffins” then “on” and the light stayed off. Any garbage gets the Arduino stuck like my sketch. Ok, is what I’m trying to do impossible or is this just coincidence?

I modified the checkString example to look like this:

I added the break and it’s able to deal with garbage. I tested more than 20 commands with banging on the keyboard in between and it seems pretty solid. Now I just need to integrate this with my IRC bot and I might have something that can stay online for a while.

By the way, after you use screen to connect to a serial port, hit “Ctrl+A, k” to kill the window and break out of screen.

Update: People have asked for the code. It’s posted after the break.
(more…)

Parabola

C/C++,GameDev — squarism @ 7:44 pm

parabola sketch

My goal was to create the curves above. I knew if I could draw it then I could move a box or game object along that path. It took me about two weeks of casual time and many math questions posted to yahoo answers.

The problem is, implementing a math formula in C++. It’s just not as pretty as the equation and any algebra tricks are hard to express in code. Not to mention remembering algebra period. ?

Eventually, I ended up with this. I specified the starting point, the ending point and how tall I want the curve. A series of horrible equations builds the rest into a vector of x,y point structs. There’s an LOD thing too that says how pretty the curve should be.

parabola3

Of course, it’s pixilated and kinda ugly. I tried anti-aliasing it but it doesn’t look much better. Also, I might have some math issues because in some places there seems to be small humps. I might be running into precision problems again. ?

Overall, I’m pretty exhausted. I don’t know what my next project will be, I don’t know if I want to make more of a game that’s interactive instead of these little graphics tests.

Also, C++ is pretty ugly imo. It’s used everywhere, I understand but I might pick up an actionscript book and see how much of this graphics stuff could be wrapped up in flash. I bet you can do some cool low-level drawing in flash; and then it’d be more ‘portable’ than an OSX app.

Anyway … it’s a thing of goddamn beauty for now. Except the code, which is ugly, untidy and probably doesn’t compile by itself. I’ll update it so it’s stand-alone.

lstat test

C/C++,Unix — squarism @ 5:38 pm

Following an interview question that was extremely hard I went to `man lstat’ and tried to code up a test just based on system documentation. It was not entirely successful, however after a tip-off from an online resource I came up with this.

(more…)

Animated Box

C/C++,GameDev — squarism @ 11:11 am

I’m very, very early on in the effort of this graphics/game test. But essentially I wanted to write down where I’m at with Xcode, OpenGL and learning C++.

Learning C++ by starting with OpenGL is very stupid. I admit this. It’s the wrong way to start out. It’s like learning how to walk by jumping out of a Dodge Viper. But I want to get beyond the Hello World books and after trying for two or three years in my limited spare time, I’m finding that being thrown into the fire is somewhat motivating. I learned Java the same way (not that I’m a master of that either), I gave myself a goal that I really wanted to accomplish and the rest just fell into place because I couldn’t think about anything else.

Such is my animated box. I want to move a box in a really smart way. Not just some Box.setX(i++); Box.setY(j++) in the main() method but something smarter that would enable me to move two, four or one-thousand boxes in the future.

animated box

More coming…

xcode glclearcolor

Chapter 4

C/C++ — squarism @ 5:08 pm

Going through the Oreilly book Practical C++ Programming, which may be an arguable title, and I’m just trying to go through all of the examples and save them somewhere so I know where I left off the next time I’m feeling recklessly productive.

Specifically, Chapter 4 asks

Exercise 4-2: Write a program to print a block E using asterisks (*), where the E is 7 characters high and 5 characters wide.

Of course the easy way would be to just print out a bunch of strings. But I consider that cheap. So I played around with struct and typedefs but I don’t know what the hell I’m doing with those so I instead forced myself to learn about passing by value vs. passing by reference.

First, the function makeE() creates an integer array which in my world of goals could be a game model, or x,y,z points for some game object. Then printE() prints out the array, but only an array of 7. If I wanted to do this whole thing much better I’d use a vector.

It’s rough shifting gears back to C++. I don’t know if I’ll ever get really good with it and be able to get to the fun stuff. You can see below that another version just printed out way too many *’s.

e messedup

But then finally, it worked suddenly after I made some adjustments.

e success

This is all just beginner stuff. :(

distcc is cool.

C/C++ — squarism @ 5:11 pm

Distcc is a distributed C/C++ compiler. You can easily use a bunch of machines’ power to compile stuff. The setup isn’t bad at all. It’s just a non-root network service.

Gentoo.org has some nice instructions on setting up distcc for builds. Here, we have 2x Pentium 3 machines and 1x dual xeon helping me build nethack. Don’t have numbers on how much faster it was but it seemed faster. Most makes/compiles are annoyingly slow, this seemed ok.

distcc_thumb.jpg

Some OpenGL progress

C/C++ — squarism @ 11:24 am

I finally got the FTGL library built. It was quite annoying to do. In the end, I had a libfreetype.a file (looks like an assembly binary) and a libftgl.a file. I added those to the XCode project. Then I double clicked on the target icon to pull up a properties pane where I could add the Freetype2 headers I installed from source.

I made freetype2′s prefix path /sw/freetype2 with `configure –prefix=/sw/freetype2′. With that path, I included /sw/freetype2 and /sw/freetype2/include in the XCode project.

I’ll post screenshots of this whole process because I couldn’t find jack on the Internet about this.

After futzing around with some FTGL demos that didn’t work I finally modified an example to the point that it loaded a true type font and displayed the string “hi” on the screen. I was very happy. I was then able to move the texture and change the alpha channel to make it fade away. What I’m looking for is a little popup of arbitrary text to appear over someone’s head when they get hit to represent damage.

Right now, I’m stuck trying to figure out how to trigger this whole drawing thing off of a keystroke instead of just being called with by the main function. I’ll post the entire example with pics soon. [placeholder]

RPG project

C/C++ — squarism @ 9:48 pm

A few ideas detailing a first bold attempt at game programming.

Of note, the game will try to be of a Final Fantasy 1 feel while having attributes and statistics similar to Final Fantasy 7 and 11. Features and goals include:

- Some sort of world map
- Possibly a shader to make the world look like a snow globe (see Doom3′s glass post-processing effect)
- Special effects based on hits / actions
- Eventually some sort of story and plot

These are on the upper end of the TODO list. Read on for more immediate tasks.
(more…)

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2013 SQUARISM | powered by WordPress with Barecity