My two cents (take with salt, as I'm a math Phd student, not CS):
A grad program absolutely CAN motivate you, IF you are motivated by failure and feelings of inadequacy. In math there is a real divide between being book smart and being able to do research (to put it mildly, it's more like a chasm). Most students have been validated their whole lives by being better than the rest, and finally coming to an obstacle that doesn't yield can be life changing.
For me, my self identity was absolutely tied up in proving myself as a mathematician, and my first few years of research were HELL. Total self sacrifice, total commitment. I came out the other side of the blaze a new person, and better for it, I think. I've lost the desire to do math, but I've gained the self confidence and mental integrity to disappear into a cave for years to work on a project with no sense of validation from others.
There were other motivating factors: wanting to compete with other students, wanting to impress my advisor, etc. NONE of those motivations survived the pain of research. And, to be honest, now that I've proven myself, not even that desire exists anymore. As of now, I can't fathom doing a PhD again. The only thing that can motivate me now is working on a really interesting problem, and to be honest academia ISN'T necessarily the place for this. At least in math, there really isn't that much freedom in what you can research.
If you can find a good research project that involves enough people to keep you motivated from social pressure, I say go for it, as it sounds like you're really jonesing for interesting work and simply need the motivational factor. Otherwise, if you can find some other way to stay motivated, I'd say do research on your own.
(As a side note, I have a Mech Eng friend that just got his PhD. He released much of his research as an open source library which apparently has attracted a pretty substantial user/contributor group. So it is possible to write quality code in an academic setting, especially if you've already have commercial experience and a desire to hold yourself to that level. Most academics have simply never been in a position where they Needed to write commercial quality code).