Saturday, June 27, 2009

iTunes COM API C#: Playlists

After a comment on my original iTunes COM API post inquiring about Playlists, I decided to revisit that part of the project. I had originally intended to include this in the first post, but it was more difficult than I had first though. Here are some code snippets to get you started:

1. Create a ComboBox listing the names of all the playlists:

foreach (IITPlaylist pl in app.LibrarySource.Playlists)
            {
                comboBox1.Items.Add(pl.Name);
            }
Notes:
Retrieves all playlists in iTunes

2. Playing One:
private void button1_Click(object sender, EventArgs e)
        {
            IITPlaylist pl = app.LibrarySource.Playlists[comboBox1.SelectedIndex + 1];
            pl.PlayFirstTrack();
        }
Notes:
Could also be done on ComboBox1.SelectedIndexChanged()
+1 Accounts for differences in indexes


This is just some basic examples, for tons more information, check out this link. You have to login in with your iTunes account, but look for "iTunes COM for Windows SDK (ZIP)" If you have trouble locating this file, email me and I can get it to you. It includes iTunesCOM.chm, which is the de-facto resource for all of this information. Between my example code (Here and here), you should be able to preform many of the iTunes functions. As always, comment if you have any troubles.


Tuesday, June 16, 2009

Getting an Angle

So probably the biggest part of constructing a balancing robot, or scooter, is knowing what angle it is in relation to the ground. From this, you can determine how much you need to compensate. The balancing system in my Segway consists of two sensors:

ADXRS614 Gyro 50 °/s

ADXL203CE Accelerometer +/- 1.5g

As I have explained in a previous post, both of these sensors are required to determine an accurate angle measurement. In my code so far, these are combined using a Complementary Filter. A great resource for coding a complementary filter on the Arduino can be found here. The hardest part that I found using this code is the Coefficients, which are not explained. I have determined, however, how to calculate these and get a working angle output. The 2 Coefficients in the program are 28.783 for the Gyro and 10.78 for the Accelerometer.

To calculate this for the Gyro:
1. Get the mv/°/s value for your Gyro from the Datasheet
-----25
2. Divide by 1000 (Milli volts in a volt)
-----0.025
3. Divide by 5 (Voltage of the Analog Reference)
-----.005
4. Divide by .0174532925 (Radians in a Degree)
-----0.2864789
5. Take the Inverse of that (1/x)
-----3.49
And there is your answer, 3.49. You would replace the 28.783 in the formula for this.

To calculate this for the Accelerometer:
1. Get the mv/g value for your Accelerometer from the Datasheet
-----1000
2. Divide by 1000 (Milli volts in a volt)
-----1
3. Divide by 5 (Voltage of the Analog Reference)
-----0.2
4. Take the Inverse
-----5
I'm not so sure on this one, but it seems to work. You would replace the 10.78 with this.

You can also replace the Voltage, in this case 5v, or the 1024 in the Formula, depending on your supply voltage or Analog Port Resolution.

When I put this all together in my test code, I get a solid, seemingly accurate angle measurement.

Next up - PID!