Wednesday, March 24, 2010

[matlabjoystick] Use a Joystick in Matlab

Have you ever wanted to use a joystick in Matlab? Yeah, I didn't think so. But just in case you did I wrote a Mex file to interface with SDL's joystick subsystem. By the way, SDL is a great easy to use library, I recommend it.

I wrote this for the "remote control wheelchair" my lab is working on. At first I tried using keyboard control, but the binary nature of a keypress made it difficult to set a constant speed. I can't image many people using this, but it could be useful in some situations.

Basically a joystick object gets initialized, and sends back an array of doubles, one for each axis, hat, and button. Optionally a mask can be set to filter buttons. For example my joystick has 14 axes/buttons, but I only care about the first two axes and the trigger button.

The following Matlab session demonstrates the use of a joystick object.

>> j = Joystick(0)
j =
Joystick handle

Properties:
joystick_number: 0
state_mask: [1 1 1 1 1 1 1 1 1 1 1 1 1 1]
Methods, Events, Superclasses
>> j.state()
ans =
Columns 1 through 11
0 0 0 0 0 0 0 0 0 0 0
Columns 12 through 14
0 0 0
>> m = j.state_mask
m =
Columns 1 through 11
1 1 1 1 1 1 1 1 1 1 1
Columns 12 through 14
1 1 1
>> m(5:end) = zeros; m(3) = 0;
>> m
m =
Columns 1 through 11
1 1 0 1 0 0 0 0 0 0 0
Columns 12 through 14
0 0 0
>> j.mask(m)
>> j.state()
ans =
0 0 0
>>

Tuesday, March 2, 2010

[pymex] Python in Matlab

So I've been pretty busy on a few different things. One of those things happens to be pymex. I often need to extend Matlab to do something

To extend Matlab's capabilities it's necessary to write mex files in C/C++. Python is quick to code and it already has a bunch of available modules. With pymex a programmer can embed Python code inside of Matlab and avoid the whole mex file thing.

As an added benifit I also wrote a python module called 'matlab'. This module does two things. First, it provides a simple print function which prints to Matlab's command window (like mexPrintf). Second, it allows Python variables to be 'pushed' to Matlab.

matlab.mex_print(str, ...)
'''Like Pythons built-in print function, takes a variable
length of objects, converts them to strings using str(),
and prints the result to the Matlab command window.'''

matlab.push(varname, var, ...)
'''Using the Matlab function 'assignin', creates a Matlab
variable in the caller's scope from 'var' named 'varname'.
The 'varname' must be a valid Matlab variable name.
The 'var' must be a scalar; lists, tuples, and objects are
not supported yet.'''


Here's a sample script to show what pymex can do.

pyscript = sprintf([...
'import matlab\n', ...
'x = 7\n', ...
'for i in range(x):\n', ...
' matlab.mex_print(i)\n', ...
' matlab.push("i_%%d" %% i, i)\n', ...
'matlab.mex_print("demo complete!")\n', ...
]);

pymex(pyscript);

Monday, January 11, 2010

[sidhack] Commodore 64s and MSSIAH

So I bought a few Commodore 64s. I should have a few SIDs I can work with. They are mostly 6851 R4s, which is good, but unfortunately I didn't get a single 8580.

[I'll add a picture in here later...]

I've decided to try and learn more about the chip by actually using one, so I've ordered a MSSIAH. MSSIAH is a C64 cartridge which basically turns a C64 into a synth. It can also process MIDI input, which is pretty cool. I think looking at MSSIAH should give me a good idea of what my eventual target will be. It also gives me an excuse to have some fun with the C64s I just got!

Thursday, November 5, 2009

[sidhack] SID Pin Descriptions

I finally found some pin descriptions, I thought I would share...

6581 / 8580 Signal Description
------------------------------

Pin(s) Signal Dir Description

1-2 CAP ? Capacitor #1 for the programmable filters.
1A/B 6581: 470pF, 8580: 22nF*. Note that both
capacitors (see pin 3-4) should have the same
value. [1] recommends 2200pF for normal
operation, which was AFAIK never used in the
C64.

3-4 CAP ? Capacitor #2 for the programmable filters. See
2A/B pin 1-2.

5 /RES in RESet. If this line remains LOW for at least
10 ø2 cycles, all internal registers reset to
zero and the audio output is turned off.

6 ø2 in Phi2**. System clock cycle. All data bus
action takes place only when ø2=1.

7 R/-W in Read/Write. 0=write access, 1=read access.

8 /CS in Chip select.0=data bus valid, 1=data bus high
impedance state (tri-state).

9-13 A0-A4 in Address bits 0-4 for selecting one of the 29
registers.

14 GND - GrouND (0V). To reduce noise, this line should
be connected separately to the power supply.

15-22 D0-D7 in / out Data bits 0-7.

23 POT Y in Potentiometer Y. Analog input #2 for paddles.
(ADC#2)

24 POT X in Potentiometer X. Analog input #1 for paddles.
(ADC#1)

25 Vcc - Supply voltage. +5V.

26 EXTERN in External Input. Max. input 0.6V.
IN

27 AUDIO out Audio output of all voices.
OUT

28 Vdd - Secondary voltage. +12V (6581) or +9V (8580).

* You might find 330pF in some boards, too.
** The 'ø' is used as Greek 'phi' here.

-------------------
[1] C64 Programmer's Reference Guide (excerpt)


Author: Marc-Jano Knopp <mjk@c64.org> on 97/11/21
Source: http://mjk.c64.org

[sidhack] The MOS Technology SID

Unfortunately I can't find a datasheet for a 6851 (or 8580). Even if I could find a datasheet, the specifications for the 6851 were apparently written before the first chip had even been fabricated. This is according to an interview with the designer Bob Yannes. So given the circumstances, I've resorted to sorting through any information I can find on the net.

Good information can be found in the documentation of past SID projects. The SID module of the MIDIBox project provides a good example of how to use a SID chip. However, all the code is in assembly (and hard to read) and the documentation isn't as complete as it could be.

The best source of information is the Commodore 64 Programmer's Reference Guide. This book is full of critical information, for example chip characteristics, specifications, registers, etc. The code examples in BASIC are somewhat useful as well, just to get a better idea of what's going on.

Wednesday, November 4, 2009

[sidhack] Where to get SIDs

The MOS Technology SID comes in two flavors, the 6581 and the 8580. The 6581 came first and can be found in Commodore 64s. The 8580 came later and can be found in Commodore 64Cs and Commodore 128s. The main difference between these two chips is the distortion and noise on the output. The 8580s have better isolation of analogue and digital circuits, and thus have a cleaner output. This is not necessarily a good or bad thing, some people may argue that noise gives the 6581 more character.

The only place to look is eBay. Commodore 64s seem to sell for around $20 (plus shipping). Usually C64s being sold 'as is' are ok, but use caution. Commodore 64Cs and 128s seem to be more rare. Occasionally someone will sell just a chip, but it may still cost you about the same. Watch out for fake chips too, there are a few floating around out there.

Tuesday, November 3, 2009

[sidhack] My New Project

Not too long ago I came across this little piece of information. I found it to be pretty funny, Nelly Furtado ripped off some really old chiptune song. At the same time, I became interested in the MOS SID chip. I mean, it must be pretty cool if they use it in Dot It. I realized that these chips are very awesome, but very rare, which makes them even more awesome. In fact, these chips can only be found by taking apart old Commodore 64s.

To me Commodore 64s are living history. I remember Professor Decker talking about them once in embedded systems, it's interesting that my generation all but missed these early computers and now we take modern computers for granted. These early computers were incredibly limited, but people still managed to use them and do fun things with them, and that's what I want to experience with this project.

I've been looking for an excuse to start a serious electronics project. I just finished my undergraduate study, and I want to test out the skills I picked up. I want this to be a real engineering solution. Not an arduino wired to a breadboard which seems to be the popular way to do thing these days. I think it's more fun to do things from scratch anyway.

The other thing on my mind is music. I recently decided I want to learn how to make music. I like many types of music, but in particular I like electronic music. I like electronic music because of its close relation to electronics, my first hobby. So I'm going to do the obvious, make something to make noise!