This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.
The great thing with the Freedom FRDM-KL25Z board is its compatibility with Arduino Shields: a great set of board available at very reasonable prices. I had my hands on the Adafruit Data Logger shield, and now it was time to use the original Arduino Motor Shield R3.
Freedom FRDM-KL25Z with Arduino Motor Shield and Arexx Chassis
I already had an Arexx Robo Chassis available: a simple platform with two DC motors. So I added the FRDM-KL25Z with the Arduino Motor Shield R3 on top of it:
Top View with Arduino Motor Shield on top of Freedom board and motor chassis
Power Supply
The battery pack (4 AAA NiMH rechargeable batteries) provide 5V to the motor shield. That 5V is powering the FRDM-KL25Z through trace on the Motor Shield. There is an outstanding issue: the Motor Shield expects that the CPU board provides both 3.3V and 5V. 3.3V is provided, but the FRDM-KL25Z only provides 5V is either the KL25Z or the K20 is USB powered. So for now I need to power the system as well with a USB cable until I find a different solution.
Current Sensing
The shield provides two analog pins for motor current sensing. According to the documentation 3.3V should be full-scale for 2A. However, I’m measuring around 60 mV even with no current. It is not clear to me from the shield schematics if the analog signal depens on the AREF signal or not: the problem could be because the Freedom board does *not* route that signal to the header without hardware modification. So not sure where I am now with this, as I’m measuring the wrong values.
Console/Shell Interface
To manually control the motor, I have added a simple shell interface:
Shell Interface
That way I can manually control the motors and get status information:
Shell Motor Commands and Status
Processor Expert
The CodeWarrior for MCU10.3 project is using Processor Expert components to abstract from the hardware, and runs FreeRTOS:
Arduino Motor Processor Expert Components
Motor Driver
The motor driver functionality is in Motor.c and Motor.h. The interface is as below:
01 /*
02 * Motor.h
03 *
04 * Author: Erich Styger
05 */
06
07 #ifndef MOTOR_H_
08 #define MOTOR_H_
09
10 #include "PE_Types.h"
11 #include "FSSH1.h"
12
13 typedef enum {
14 MOT_DIR_FORWARD, /*!< Motor forward direction */
15 MOT_DIR_BACKWARD /*!< Motor backward direction */
16 } MOT_Direction;
17
18 typedef int8_t MOT_SpeedPercent; /* -100%...+100%, where negative is backward */
19
20 typedef struct MOT_MotorDevice_ {
21 bool brake; /* if brake is enabled or not */
22 MOT_SpeedPercent currSpeedPercent; /* our current speed in %, negative percent means backward */
23 uint16_t currPWMvalue; /* current PWM value used */
24 LDD_TError (*SetRatio16)(LDD_TDeviceData*, uint16_t);
25 LDD_TDeviceData *PWMdeviceData; /* LDD device handle for PWM */
26 void (*DirPutVal)(LDD_TDeviceData *, bool); /* function to set direction bit */
27 LDD_TDeviceData *DIRdeviceData; /* LDD device handle for direction */
28 void (*BrakePutVal)(LDD_TDeviceData *, bool); /* function to enable/disable brake */
29 LDD_TDeviceData *BRAKEdeviceData; /* LDD device handle for brake pin */
30 uint16_t currentValue; /* current AD current sensor value */
31 uint16_t offset; /* current AD sensor offset value */
32 LDD_TDeviceData *SNSdeviceData; /* LDD current AD device handle */
33 } MOT_MotorDevice;
34
35 /*!
36 * \brief Sets the PWM value for the motor.
37 * \param[in] motor Motor handle
38 * \param[in] val New PWM value.
39 */
40 void MOT_SetVal(MOT_MotorDevice *motor, uint16_t val);
41
42 /*!
43 * \brief Return the current PWM value of the motor.
44 * \param[in] motor Motor handle
45 * \return Current PWM value.
46 */
47 uint16_t MOT_GetVal(MOT_MotorDevice *motor);
48
49 /*!
50 * \brief Change the direction of the motor
51 * \param[in] motor Motor handle
52 * \param[in] dir Direction to be used
53 */
54 void MOT_SetDirection(MOT_MotorDevice *motor, MOT_Direction dir);
55
56 /*!
57 * \brief Returns the direction of the motor
58 * \param[in] motor Motor handle
59 * \return Current direction of the motor
60 */
61 MOT_Direction MOT_GetDirection(MOT_MotorDevice *motor);
62
63 /*!
64 * \brief Shell command line parser.
65 * \param[in] cmd Pointer to command string
66 * \param[out] handled If command is handled by the parser
67 * \param[in] io Std I/O handler of shell
68 */
69 uint8_t MOT_ParseCommand(const unsigned char *cmd, bool *handled, const FSSH1_StdIOType *io);
70
71 /*!
72 * \brief Initialization function.
73 */
74 void MOT_Init(void);
75
76 #endif /* MOTOR_H_ */
Summary
It was very easy to use the Motor Shield with the help of CodeWarrior for MCU10.3 and Processor Expert. The basic functionality with the exception of current sensing works and with the shell interface it is easy to explore and add further functionality. I still have an ultrasonic sensor to integrate .
The CodeWarrior project and sources are available from this link.