[re-printed from PSoC Creator News and Information]
Well, I finally finished my Barc project. The design is attached to this blog. I think I learned a lot. Mostly that I should never pick up another soldering iron, always use battery packs with on/off switches, and that blogging takes a lot longer than programming. More usefully I got quite efficient at tuning CapSense and have a very tidy and reliable motor control circuit to use in new designs.
Barc works pretty well now but he tends to be a little excitable. He is quite obedient most of the time and faithfully follows my hand even though I am not holding any treats at all. But I did build a little bit of "personality" into him by breaking all the rules of good CapSense sensor design. To name a few of my engineering crimes:
It is hardly surprising, then, that after following me for a while he has a habit of running off into the distance or chasing his tail like it has been wrapped with bacon. It is all good fun but I could never take him for a walk in San Jose traffic.
To train him a little bit I built a little thinking time into his main loop. According to the CapSense component each scan takes 512 microseconds. You can see this in the dialog. Each scan slot takes 171us for a total of 512us.
That means I am detecting just less than 2000 times per second. I can give Barc some rest time by counting the number
of times my main loop executes and forcing a delay, with CapSense and the motors both turned off, if he does the same thing for more than a second. It's just a simple loop counter that increments if the motors are running (compare value not equal to zero) and resets otherwise. If the counter gets to 2000 then Barc stops for a quarter of a second. That seems to be long enough for the noise to die down enough for much improved behavior.
I feel the need to point out, one last time, that this is deliberately doing very naughty things with CapSense. I am ignoring the excellent guidelines set out in our best practices document so that Barc will behave badly, and then using extremely crude measures to limit the "error" rate. If you are making a serious CapSense design please don’t use this project as an example of robust sensing! But, that said, he's my dog and I'll train him however I like!
Here is a short video of Barc chasing me around the floor.
And here is the snippet of code that senses my hand, adjusts the motor speeds, and, if necessary, takes a short breather to let the noise dissipate.
CapSense_Start(); // Turn on CapSense
for(;;)
{
/* Start a scan and wait for it to complete */
CapSense_ScanAllWidgets();
while( CapSense_IsBusy() )
/* Busy loop */
}
CapSense_ProcessAllWidgets(); // Read all sensors and determine widget status
if( CapSense_IsWidgetActive( CapSense_LEFT_WDGT_ID ) )
/* Hand to the left, follow it */
PWM_Speed_WriteCompare1( 40 );
PWM_Speed_WriteCompare2( 0 );
loopcount++;
else if( CapSense_IsWidgetActive( CapSense_RIGHT_WDGT_ID ) )
/* Hand to the right, follow it */
PWM_Speed_WriteCompare1( 0 );
PWM_Speed_WriteCompare2( 40 );
else if( CapSense_IsWidgetActive( CapSense_FRONT_WDGT_ID ) )
/* Run toward the hand in front of your nose */
PWM_Speed_WriteCompare1( 60 );
PWM_Speed_WriteCompare2( 60 );
else
/* No hand close so stop */
loopcount = 0;
/* Each loop takes ~500ns so check for 1s of unchanged state */
if( loopcount > 2000 )
/* Turn off the motors */
/* Turn off CapSense and wait 250ms for noise to settle */
CapSense_Stop();
CyDelay( 250 );
CapSense_Start();
#ifdef TUNING
CapSense_RunTuner(); // Send data to tuner (I2C)
#endif
I have attached my final Barc project to this blog. Feel free to try it out, fix it, add to it, whatever. But do let me know if you do something interesting!
It took me longer than I expected to finish this project - just ask my wife/videographer who got really bored of the whirring motors and runaway dog in the living room - but now I think it is time to move onto the next one. I feel like I actually know what I am doing at last. Next time I am going to use Sparkfun robot/motor parts and see if I can make a Bluetooth-enabled dog…