Motor Shield V1 Library [Working]

I’ve a project which needs a motor shield to drive 4 DC motors. I’m using the Adafrut Motor Shield (http://www.adafruit.com/products/81) which is then connected to the SparkCore via the Spark Core “Shield Shield”.

In order to use the Adafrut Motor Shield, I need to used the firmware for this board (https://github.com/adafruit/Adafruit-Motor-Shield-library), question is, how can I include these libraries when I’m using the Cloud IDE?

Thanks heaps in advance,
Tim

A quick look reveals they are using 4 PWM’s for the Adjustable Frequency Motor output (1 for each motor), 2 PWM’s for servos, and reusing the 4 PWM’s for stepper control, along with some I/O for shifting data to relay’s for direction control on the motors.

You would need to port the library by combining the .h and .cpp files all together with your application.cpp (the sketch), and put that into the Sparkulator (Spark Build, aka Cloud IDE, aka Web IDE aka Spark IDE). This would be possible!

The Spark Core has 8 PWMs, which can be used to control Servos or just used as general purpose PWM outputs. By default the gen purp PWM is fixed at 500Hz. I came up with a way to set them to whatever you want (tested up to 64kHz).

This, coupled with a bunch of chopping to the #defines AVR / PIC32 / etc… and the library should be working.

That said, you need the pins that have this hardware functionality to couple to the pins of the shield that need that functionality. Since the Core has more PWM’s than the Arduino, you should be in good shape, just need to check.

If you want to take a stab at it… please do! Otherwise I could try to port it maybe this weekend. Wish I had a motor shield though to test it :wink:

@BDub, you must already know this, but you are awesome. A giant high-five from the Spark team in China (and everywhere, I’m sure) for being so active and helpful on the community.

2 Likes

Thanks @will for the kind words! I envision the Spark Core as being the next big thing since Arduino or Raspberry Pi, and to make that happen a thriving community is a very integral part of the equation! Doing what I can to help it along :smile:

2 Likes

Wooh BDub! Seriously awesome. People like you make communities like this flourish :slight_smile:

I’m going to have a go at what you’ve suggested over the weekend… and I’d also like to shout you a few motor shields if you feel included to do some testing - let me know the easily way for me to do this :smile:

Sounds good Tim! Shout away :wink: Send me an email at technobly a t gmail d o t com and we can exchange info.

Just a shout out to @BDub for his awesome work on this, he’s managed to port the Adafruit library so we can use these motor shields (which you can buy clones of via ebay for around $6). If you see him in person be sure to shout him a beer, and make sure your Sparkcore robot armies remember who their creator is :smile:

@BDub, over to you share the code

1 Like

Thanks @timcopeland! Ok guys here we go… Let’s see those motor control projects start popping up! :smile:

https://github.com/technobly/SparkCore-MotorShield-V1

Hey @BDub, I’m trying to get this working in the web ide, and I keep getting compile errors. I created two new files in the ide for the project named Spark_MotorShield_V1.h and Spark_MotorShield_V1.cpp and pasted the contents from your github files into them. Then I pasted the contents from the example into the main app file. Compiling produces the following errors:

the_user_app.cpp:65:1: error: 'AF_DCMotor' does not name a type
the_user_app.cpp:66:1: error: 'AF_DCMotor' does not name a type
the_user_app.cpp:67:1: error: 'AF_DCMotor' does not name a type
the_user_app.cpp:68:1: error: 'AF_DCMotor' does not name a type
the_user_app.cpp: In function 'void setup()':
the_user_app.cpp:78:3: error: 'motor1' was not declared in this scope
the_user_app.cpp:79:3: error: 'motor2' was not declared in this scope
the_user_app.cpp:80:3: error: 'motor3' was not declared in this scope
the_user_app.cpp:81:3: error: 'motor4' was not declared in this scope

So I thought maybe I needed a

#include "Spark_MotorShield_V1.h"

at the top of my main file, but then I get the following errors:

the_user_app.o: In function `setup':
/spark/compile_server/shared/workspace/worker_1/core-firmware/build/the_user_app.cpp:79: multiple definition of `TIM4_ARR'
Spark_MotorShield_V1.o:(.bss+0x6): first defined here
the_user_app.o: In function `setup':
/spark/compile_server/shared/workspace/worker_1/core-firmware/build/the_user_app.cpp:79: multiple definition of `TIM3_ARR'
Spark_MotorShield_V1.o:(.bss+0x4): first defined here
the_user_app.o: In function `setup':
/spark/compile_server/shared/workspace/worker_1/core-firmware/build/the_user_app.cpp:79: multiple definition of `TIM2_ARR'
Spark_MotorShield_V1.o:(.bss+0x2): first defined here
the_user_app.o: In function `setup':
/spark/compile_server/shared/workspace/worker_1/core-firmware/build/the_user_app.cpp:80: multiple definition of `TIM1_ARR'
Spark_MotorShield_V1.o:(.bss+0x0): first defined here
the_user_app.o:(.data+0x2): multiple definition of `TIM_ARR'
Spark_MotorShield_V1.o:(.data+0x12): first defined here
the_user_app.o: In function `setup':
/spark/compile_server/shared/workspace/worker_1/core-firmware/build/the_user_app.cpp:80: multiple definition of `TIM_OCInitStructure'
Spark_MotorShield_V1.o:(.bss+0xa): first defined here
collect2: error: ld returned 1 exit status
make: *** [49b1ca310b0760fa9000e04d4f91c25eb3a819a999d3c8aa54dbaa4fc526.elf] Error 1

Any ideas?

Bah, yes I know exactly why that happens! I had the same problem when I tested it in the web IDE… and fixed it. But I guess it never made it back to my source files before I pushed them to github.

Check my latest commit for the fix! Thanks for trying this out :wink:

And you will need "#include “Spark_MotorShield_V1.h” in your application.cpp

It works great now! Thanks!

1 Like

@BDub I thought you’d like to see what the kids and I were able to accomplish today with your Motor Shield library port. Thanks again!

3 Likes

Whoa that is way cool @Hypnopompia :slight_smile: I think I need to order some extra lego wiring for my legos… so I can hack it up and play as well!

And @Hypnopompia continues the efforts here! Thanks for porting the V2 Motor Shield library: https://community.spark.io/t/adafruit-motor-shield-v2-library-ported/5218

:slight_smile:

1 Like