Sunday, February 13, 2011

High Current Motor Controller Shield Schematic

Good news, folks! I've decided to post the schematics for the high current motor controller shield with some knowledge on how to use the VN2SP30 IC. The schematic is taken almost directly from the datasheet from ST. I left out the reverse voltage diode because I have a polarized connector, but I still managed to accidentally reverse voltage and fry it. The next rev will include reverse voltage protection.


Here is some sample code to run the motor controller. Disclaimer: This code was written in minutes and may not be the best code in the world

Example Sketch for the Motor Controller Shield

/*
    High current motor controller shield example sketch
    ilektron-x.blogspot.com
*/
const int pwm1Pin = 5;   // the pin that the pwm1 is attached to
const int pwm2Pin = 6;   // the pin that the pwm2 is attached to
const int ina1Pin = 7;   // the pin that the ina1 is attached to
const int inb1Pin = 8;   // the pin that the inb1 is attached to
const int ina2Pin = 9;   // the pin that the ina2 is attached to
const int inb2Pin = 10;  // the pin that the inb2 is attached to

#define timestep 20
#define kscale 11370     // Calculated from datasheet, used to convert ADC reading to current
#define res 1500         // Current sense resistor value

void setup()
{
  // initialize the serial communication:
  Serial.begin(57600);
  
  // initialize the motor controller pins as an output:
  pinMode(pwm1Pin, OUTPUT);
  pinMode(pwm2Pin, OUTPUT);
  pinMode(ina1Pin, OUTPUT);
  pinMode(inb1Pin, OUTPUT);
  pinMode(ina2Pin, OUTPUT);
  pinMode(inb2Pin, OUTPUT);
  
  // Make sure everything is low, set up the motors to spin in opposite directions
  digitalWrite(pwm1Pin, LOW);
  digitalWrite(pwm2Pin, LOW);
  digitalWrite(ina1Pin, HIGH);
  digitalWrite(inb1Pin, LOW);
  digitalWrite(ina2Pin, HIGH);
  digitalWrite(inb2Pin, LOW);
}

void loop() {
  static byte speed1 = 0;
  byte rxdata = 0;
  byte cs1 = 0;
  static int countdown = timestep;
  
  static long current1 = 0;
  static long current2 = 0;
  float cval1 = 0;
  float cval2 = 0;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    rxdata = Serial.read();
    
    // 'u' key press ups speed, 'd' decreases it. 0-9 set the speed to 0-90% of speed
    switch (rxdata)
    {
        case 'u':
          speed1 = (speed1 < 250) ? speed1 + 5: 255;
          break;
        case 'd':
          speed1 = (speed1 > 5) ? speed1 - 5: 0;
          break;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case ':':
          speed1 = (255*(rxdata-'0')/10);
          break;
        default:
          speed1 = 0;
          break;
    }
    
    // Display what the new speed setting is
    Serial.print('\n');
    Serial.print('s');
    Serial.print(speed1, DEC);
    
    // set the motor speed:
    analogWrite(pwm1Pin, speed1);
    analogWrite(pwm2Pin, speed1);
    
  }
  
  current1 += analogRead(0);
  current2 += analogRead(1);

  delay(1);
  if (!countdown) // Simple countdown timer to space out frequency of current reading output 
  {
    // Calculate current for motor 1
    Serial.print("M1:\0");
    cval1 = current1;
    cval1 = (cval1*kscale/timestep)/res*5/1024;
    Serial.print(cval1, 3);
    // Calculate current for motor 2
    Serial.print("\tM2:\0");
    cval2 = current2;
    cval2 = (cval2*kscale/timestep)/res*5/1024;
    Serial.print(cval2, 3);
    Serial.print('\n');
    countdown = timestep;
    current1 = 0;
    current2 = 0;
  } else {
    countdown--;
  }
}

Arduino - Perspective from someone with embedded experience

Recently there have been some articles about why the Arduino is here to stay and how to kill it.

It concerns me that people want to kill the Arduino. Why kill a project that meets a definite need? Time to rant a little bit.

In order to give a little background, I have experience with PICs, AVRs, LPCs, DSPs, and more. I've put these microcontrollers into all sorts of applications. I've used all sorts of development tools. Guess what? As you can gather from my blog, I own an Arduino. And I play around with it.

Recently, I recommended Arduino platforms to two of my Mechanical Engineer friends. Both projects they were working on needed a microcontroller. Both projects contain components more complicated than blinking LEDs. One project hopes to assist a sibling with automating a home business, the other project is a complicated hobbyist robotics project. Why did I steer them towards the Arduino?

Have I betrayed my hacking roots by steering them to the Arduino when I could have pointed them in any direction? Let us look at the options.

PICs

The Good
PICs are the original hobbyist microcontroller. PICs come in many forms, from motorcontrollers to RF transceivers. A friend of mine opened his car's key fob only to see an pic inside. Used in major industry, PICs definitely have a lot to offer. To add to PIC's credit, they are robust well designed chips that can take a lot of abuse.
The Bad
As great as PICs are, even for an experienced engineer, it can take a couple of days before you get code running on one. Like a lot of microcontrollers, it requires the user to pour through datasheets looking for registers and bit values.
Almost all PIC projects require a programmer. The programmer is PICs Achilles's heel. They don't work 100% of the time. I've fried a couple of them. The simplest one, the PICkit, my favorite of the programmers, cost more than I'd like to spend on a programmer. One last gripe, Microchip's IDE for the PIC, MPLAB, doesn't exude love or freedom.

AVRs

The Good
I am not sure why, perhaps because I love linux, I have a crush on AVRs. You can develop and program AVRs from any platform with a multitude of IDEs thanks to gcc-avr. Even Atmel's development suite provides a multitude of features. AVRs have some unique features, one in particular makes it easy to implement bootloaders on the AVR. AVRs, like PICs are difficult to damage, and come in many varieties and flavors.
The Bad
Although you can pick up an AVR programmer for $20, they still require one to get started. You still have to pour through 300 page datasheets looking for registers to modify to set up PWM or UARTs. Users coming from little or no programming background face the same large learning curve like with the other microcontrollers.

LPC

The Good
One of my personal favorites, the LPC series chips provide a large selection of peripherals, and power. They include ARM cores, allowing a user to utilize several of the open tool chains, or even proprietary ones if they choose. LPCs ship with a serial bootloader, eliminating the need for a JTAG or programmer. A simple TTL serial cable is enough. The LPC1343 comes with a USB bootloader that shows up as another drive containing firmware.bin!
The Bad
Setting up the tool chain can be frustrating. Getting the serial bootloader working may not be as easy as it sounds, and often doesn't work for some reason or another the first time. No different than the others, long datasheets threaten to slow you down as you look for the right register bit value.

Arduino

The Good
$30 complete package. Incredibly simple IDE that just works. Figuring out which serial port to use is the most difficult part of setup. Arduino includes simple libraries for otherwise overly complicated tasks. Huge community support and very open user base.
The Bad
The Arduino is not hardcore because anyone can do it. Arduino specific functions suffer performance hits due to overhead. Pin functions are somewhat locked in to specific functions, for example, analog pins. Arduino stackups can become quite bulky compared to a custom solution.

The Ugly Comparison

What does Arduino offer that the others do not? Watered down programming and a dumbed down development environment? TI offers a development board for $4.30 while the Arduino cost $30. Other chips offer boot loaders, more power, more control, and raw C goodness?
Back to my friends to whom I recommended the Aruduino to. Both of them set up the IDE and had code running within half an hour. By themselves. One of them had text up on an LCD within the night. The other was reading sensor values the same night. One had worked with LabView, the other Matlab, but neither had any embedded experience. Guess what? Neither would have tasted such quick success using another platform. I doubt they wanted to set up a compiler or figure out a programmer. I imagine they will never care about the finer points of a UART, peripheral clocks, ADC timing, or power options. Do you have to understand calculus to calculate interest or know control theory to use cruise control? Sure, it would be nice if everyone knew calculus, but it will never happen. The Arduino provides a platform that provides the power of C without a lot of challenges of getting things to work. Arduino is the personal computer of the embedded world. Somebody else already got everything to work. If people using the Arduino want eventually 'upgrade' to something more barebones, great! If not, great! Not everyone enjoys the embedded aspect of their projects.

Look at Sparkfun. The whole company works by wrapping raw products into nice friendly functional packages. Sure, I could design my own widget that they sell, but purchasing widget X gets me to my goal faster and easier and helps me avoid design aspects I don't care about if my goal is simply a completed project.
I will continue recommending Arduino even though I, myself, may not use it for more than just prototyping. I hope to see the Arduino continue to grow and continue to meet the demand that it fills.

Monday, February 7, 2011

Time to work build a new motor controller

I finally got around to testing out the repaired motor controller after the reverse voltage fiasco.  No dice.  The thing ist kaput.  I threw a quick tutorial on removing SMT components from PCBs to show how easy it can be to quickly do some hot air rework with a $25 heat gun.  Here is the video, enjoy!



(PS, while it uploads I will be updating the high current motor controller shield and working on V2)

Saturday, February 5, 2011

Evalbot has arrived!

Despite doubting that TI would honor the $125 off coupon, guess what arrived at my door last week?


Amazing!  I promptly put it together and let it wander around the living room while my 2 year old chased it, stepping on it repeatedly while trying to trigger the bump sensors with his foot.

Hopefully I'll find some time to play with it and try to write some code for it in linux, thanks to this post, Evalbot: Hello World!, linked from Hackaday.

Although wheels and motors are great, what I would really like to swap the motors for some LEDs, plugging this into my home network, and using this as a home automation server.  Or sell it for a profit.  Either way...