Makin’ those servos work!

January 23, 2011

I’ve kind of taken a break with pyton and pygame, and I’ve started doing things with the Arduino board. So I now own an Arduino Diecimila board. So, of course, I did many of the arduino tutorials on the arduino website.

I did that until I thought I had  basic understanding of Arduino. So I moved on to a slightly bigger project. I was going to build a simple robot using the Arduino.

To start I took the main part of my Parallax Boe 2 off of the body part, so I had something to build with. And, as most robots need SOMETHING to do I hooked up the servos used for the wheels to my Arduino/solderless breadboard using the following schematic:

 

For those who don’t know: Vdd is power, Vss is the ground, and obviously the I/O pin is what gets conneted to the Arduino boards so that the servo actually knows what to do.

After a long afternoon of exploring PWM (Pulse Width Modulation) and how to use it, we tried to actually get the sevos moving. As with most projects I do, it didn’t work at first. So after extensive research online I found this code:

int servoPin = 7;
void setup()
{
pinMode(servoPin,OUTPUT);
}
void loop()
{
int temp;
for (temp = 0; temp <= 200; temp++)
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(1500); // 1.5ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms
}
for (temp = 0; temp <= 200; temp++)
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(1800); // 1.8ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms
}
for (temp = 0; temp <= 200; temp++)
{
digitalWrite(servoPin,HIGH);
delayMicroseconds(1200); // 1.2ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms
}
}

Basically, what this code was doing was using PWM in a “braindead way”. It made The servos soin in one direction until the time got slow enough that it stopped and started spinning in the opposite direction. “Wow. That seems a bit too tedious to be the real way to control the servos,” I thought. Research. Research. Research.

After all of that research and stuff, I found one of the weird things the guy who wrote the previous code did. He was using digitalWrite. It is much easier to us analogWrite, so that you can use a number value instead of just LOW/HIGH. That’s important because the way that the servos work is based on the PWM coming from the Arduino board. So if It’s at either extremes, the servos won’t move very fast, if at all. That’s why, in the following code, the analogWrite sets the wheels to a space in between HIGH and LOW.

#define CLOCK 52
#define COUNTER 200
#define NOTMOVING 128
#define RIGHT 6  //sets pin 6 to the right wheel
#define LEFT 5  //sets pin 5 to the left wheel

void forward() {  //the “function” used to refer to moving the servos forward
  analogWrite(RIGHT, CLOCK);
  analogWrite(LEFT, COUNTER); 
}
void stopped() {  //the “function” used to refer to the servos being stopped
  analogWrite(RIGHT, NOTMOVING);
  analogWrite(LEFT, NOTMOVING);
}
void backwards() { //the “function” used to refer to the servos
  analogWrite(RIGHT, COUNTER);
  analogWrite(LEFT, CLOCK);
}
void setup() {
  pinMode(RIGHT, OUTPUT);  //sets pin 7 as an output
  pinMode(LEFT, OUTPUT);  //sets pin 6 as an output
}
void loop() {
  forward();
}

This code, as it is, will just make the little BOE move forward. However, I added the function of moving backward, and staying still. So all you would have to do to make it move backward or stay still is add it it to the loop. So as soon as I get around to doing other stuff to the robot, like adding sensore, I can still tell it which ways it should move easily. :D

Random Fact Generator

May 28, 2010

I thought it would be a fun idea to write some code that generated some random fun facts when I ran it. So I started working on it eary this morning. I was going to use a lot of stuff similar to what we did when we made the number guessing game, because it seemed the best way to make it random. However after telling my brother about the project he suggested that instead of doing it the stupid way that I was gonna do it, (like below) 

import random
randomfact =  random.randint(1,  3)
if randomfact == 1:
    print ‘There are square watermelons in Japan!’
if randomfact == 2:
    print ‘Philo Farnsworth (the man tht invented the television) had a complete undderstanding of the the theory of relativity when he was 15!’
if randomfact == 3:
    print ‘crwth is an english word with no vowel!’
if randomfact == 4:
    print ‘Horses do not yawn!’

he suggested we do it with a list. So we talked for a while about lists. Then we talked about indexs. After a lot of stupid questions from me I finally under stood why we did things like ‘random.randint (0, len(facts)-1)’. And it’s because the highest index in the list is always gonna be one less than the number of items on the list because it uses zero as a number, rather than starting t one. In the end we ended up with the finished random fact generator like the on below.

import random
facts = [
'there are square watermelons in Japan!',
'Philo Farnsworth (the man tht invented the television) had a complete undderstanding of the the theory of relativity when he was 15!',
'crwth is an english word with no vowel!',
'Horses do not yawn!',
'Elephants are the only mammal that can not jump',
'The English alphabet used to have 24 letters.',
'Matt Hummer was unanimously voted to be the coolest person ever!',
'It is more likely tht you are raped in Canada than it is for you to win the lottery',
'California has a bigger population than Canada (2009)',
'You can not hum with your nose pinched shut.'
]
indexnumber = random.randint (0, len(facts)-1)
print facts[indexnumber]

monkey game

February 3, 2010

    Today we started to learn some pygme from a pygame tutorial with a monkey(http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html).  All we really did was learn about loading images. It was really confusing t first, but once I had my brother explain a lot of stuff to me I could feel myself starting to learn.

    Most of the python that I actually wrote today came almost straight from the tutorial, but i still think i accomplished a lot.

Getting back into it.

February 2, 2010

    My old laptop broke. So I got a new one. Sadly, this one runs windows. I had to download a bunch of new programs so that we could begin writing Python again.

    First i downloaded Xchat so my brother and I could communicate as we wrote our games. Then we downloaded Wing IDE pro.

    Now this is where it gets complicated(we all knew it was coming). We couldn’t use our verification code because it as for Wing IDE personal. So we switched it easy enough. However, now Wing couldn’t find python. So we downloaded that as well as pygame.

    For some odd reason Wing could still not find python so we had to save it to a weird file. Then it found it. Now it couldn’t find Pygame though. So we had to move that around a bit. And this whole process involved a bit of installing, uninstalling, and reinstalling. It made me wish I could just use Synaptic again. 

    Then we finally figured out where to put everything, Wing could find it and we would finally work on our games again.

June 23, 2009

So we decided to go from a number guessing game to an animal guessing game. We wanted it to be somewhat like 20 questions. The computer is supposed to guess the animal you are thinking of. We needed to figure out how the computer will know which animal you are thinking by the questions it asks. So we started working on classes and sub-classes.

We broke animals into two basic groups-vertebrate and invertebrate. Then we broke that into Mammels, fish, etc.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'''Animal types.'''

class Animal:
    '''A Base Animal type.'''

class Vertebrate(Animal):
    '''An Animal with a backbone.'''

    def has_backbone(self):
    	return True

class Invertebrate(Animal):
    '''An Animal without a backbone.'''

    def has_backbone(self):
    	return False

class Fish(Vertebrate):
    '''A Vertebrate with no fur.'''

    def has_fur(self):
    	return False

class Mammal(Vertebrate):
    '''A Vertebrate with fur.'''

    def has_fur(self):
    	return True

class Human(Mammal):
    '''vertebrate with fur.'''

class Lion(Mammal):
    '''vertebrate with fur'''

class monkey(Mammal):
    '''vertebrate with fur'''

class jellyfish(invertebrate):
    '''not sure, but cool'''

class bear(Mammal):
    '''hairy monster!!!'''

class Matt(vertebrate):
    '''We're not sure exactly...'''

My First Game!

April 4, 2009

I have  finally written my first game. It is not the funnest game you will ever play. It is a simple number guessing game. I made it because my brother said “It’s just a few easy lines of code”. It turned out to be a two-and-a-half-day project. We started out with learning the raw_input builtin. That made a few things MUCH easier. Then we added a few if statements to tell you whether you should guess higher or lower or  you got it right. When we finally figured all of that out we decided to put in a loop so that you don’t have to keep running the code until you got it right. We used a while loop and talked about the differences between the while loop and the for loop. When we figured that out then we had to do a lot of editing because my indentions suck so it was causing problems with the code. Then we noticed that it didn’t tell you when you won, it just stopped. So we fixed that by taking the if statement out of the loop and put it after the loop. Now it works!

import random
compnumber =  random.randint(1,  100)
usrnumber = 0
while usrnumber != compnumber:
usrnumber = raw_input(‘Your number please:’)
usrnumber = int(usrnumber)

if usrnumber > compnumber:
print “Too high!”
if usrnumber < compnumber:
print “Not quite high enough…”

if usrnumber == compnumber – 1:
print “So close…”
if usrnumber == compnumber + 1:
print “So close…”
print “Yay! You did it!”

Technical Difficulties

January 5, 2009

So we have recently upgraded my laptop (even though it is still a slow piece of crap) and during that process we ran into some technical difficulties. For some reason the new upgrade changed my mouse pad settings. It had turned it off.(Of course at the time we didn’t know it was that simple). So we tried to plug in an external mouse and that worked so for a while I just used that. Then for a cure we switched me from Gnome to Xfce. I was on that for a while. One day my computer was being really slow so i rebooted it. When i logged back in i had no task bar. So i was without internet for a while and then i got idea. If I could get an external mouse then i could log back into Gnome and change my settings from there and log out then log back into Xfce then my internet would work again. I couldn’t find a mouse so I stole the one from off of my dad’s computer. Curiosity got the best of me and i went into my mouse settings to see if there was anything i could do in there to help my mouse problem. So I disabled the mouse pad then turned it on again and it worked. I was so relieved. I don’t much like Xfce as much as I like Gnome. Of course I had to hide while my dad tried to figure out what was wrong with his mouse. At least now my computer is back to normal again.

June 7, 2008

So I recently finished chapter two of my book. This is the chapter where things are starting to get a little more complicated.

I have been learning about boolean logic, statements, and operators. It’s pretty much a lot tougher now. But the cool thing is I’m actually writing stuff that looks like python now. It’s a lot more lengthy. It’s not just the simple stuff I used to do, like making strings and adding them together.

It’s especially cool cause In the Python in Practice section of the book we actually wrote a program that makes a bunch of tanks fight each other. I changed the numbers around a little so that the tank Bob would win.

I also wrote a little program that would pretty much do the same thing as rolling a die. It would randomly pick a number from one through 6. Yet again I played with the numbers a little so that it would randomly pick a number one through 100.

That’s pretty much everything I’ve been doing. I change a lot of the code around so I can learn it better. If I didn’t I would probably drive my self crazy with the word FUGU!

PyGame!

June 5, 2008

the book

Okay so today I started working on making a game with my brother. It’s fairly easy, except for some of the little details. It is going to be a game we make using python. We’re using a book to help me start learning python. It’s called Beginning Game Development with Python and Pygame.

So today by brother quizzed me on chapter one. It was pretty easy. Then we started chapter two. That’s when all the problems started. I was having problems with extra spaces at the end of the things that you can’t even see when the books give you examples of what it looks like. But it’s okay.

I learned about loops. We also talked about the differences between lists and tuples. Tuples load faster, but are immutable. It is pretty much cool!


Follow

Get every new post delivered to your Inbox.