Library for MX1508 Dual H-Bridge by Cheng Saetern will not compile

tspringer

Member
I am trying to employ an MX1508 dual H-bridge motor controller in a project that I am working on. I have no problem getting my Teensy 4.1 to actuate a motor attached to the H-bridge, but the only library that I can find that is set up to work with PWM speed control with the MX1508 (https://github.com/Saeterncj/MX1508) fails to compile.

My hope is that someone on the forum will be able to confirm that the library will (or will not) compile on their computer. I have never had problems with compiling libraries until recently, but over the last few days, I had problems compiling several, so either I am experiencing a run of bad luck, or there is something partially awry with the compiler (or the user).

To keep things simple I attempted to compile the following minimal code:

-------------------------

#include <MX1508.h>

#define PINA 36
#define PINB 37
#define NUMPWM 2
#define PWM 100

MX1508 motorA(PINA,PINB, FAST_DECAY, NUMPWM);

void setup() {}

void loop() {
motorA.motorGo(PWM);
}

-------------------------

And here are the voluminous error messages generated by the compiler:

-------------------------

C:\Users\Michael\AppData\Local\Temp\.arduinoIDE-unsaved2023327-548-19k8st8.wur5\sketch_apr27a\sketch_apr27a.ino:5:16: warning: invalid conversion from 'int' to 'NumOfPwmPins' [-fpermissive]
5 | #define NUMPWM 2
| ^
| |
| int
C:\Users\Michael\AppData\Local\Temp\.arduinoIDE-unsaved2023327-548-19k8st8.wur5\sketch_apr27a\sketch_apr27a.ino:8:38: note: in expansion of macro 'NUMPWM'
8 | MX1508 motorA(PINA,PINB, FAST_DECAY, NUMPWM);
| ^~~~~~
In file included from C:\Users\Michael\AppData\Local\Temp\.arduinoIDE-unsaved2023327-548-19k8st8.wur5\sketch_apr27a\sketch_apr27a.ino:1:
c:\Users\Michael\Documents\Arduino\libraries\MX1508/MX1508.h:21:78: note: initializing argument 4 of 'MX1508::MX1508(uint8_t, uint8_t, DecayMode, NumOfPwmPins)'
21 | MX1508(uint8_t pinIN1, uint8_t pinIN2, DecayMode decayMode, NumOfPwmPins numPWM);
| ~~~~~~~~~~~~~^~~~~~
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp: In member function 'void MX1508::setResolution(unsigned int)':
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:32:25: error: 'ICR1' was not declared in this scope
32 | if(_useAnalogWrite16) ICR1 = pwmResolution;
| ^~~~
In file included from C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/wiring.h:39,
from C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/WProgram.h:46,
from C:\Users\Michael\AppData\Local\Temp\arduino\sketches\6D542608C537E51812A9D89560841D14/pch/Arduino.h:6,
from c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.h:4,
from c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:1:
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp: In member function 'void MX1508::setPWM16(uint8_t, unsigned int)':
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:37:22: error: 'PB1' was not declared in this scope; did you mean 'B1'?
37 | DDRB |= _BV(PB1) | _BV(PB2); /* set pin 9and 10 as outputs */
| ^~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:37:33: error: 'PB2' was not declared in this scope
37 | DDRB |= _BV(PB1) | _BV(PB2); /* set pin 9and 10 as outputs */
| ^~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:38:5: error: 'TCCR1A' was not declared in this scope
38 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); // non-inverting PWM, mode 14 fastPWM, TOP =ICR1
| ^~~~~~
In file included from C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/wiring.h:39,
from C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/WProgram.h:46,
from C:\Users\Michael\AppData\Local\Temp\arduino\sketches\6D542608C537E51812A9D89560841D14/pch/Arduino.h:6,
from c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.h:4,
from c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:1:
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:38:18: error: 'COM1A1' was not declared in this scope
38 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); // non-inverting PWM, mode 14 fastPWM, TOP =ICR1
| ^~~~~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:38:32: error: 'COM1B1' was not declared in this scope
38 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); // non-inverting PWM, mode 14 fastPWM, TOP =ICR1
| ^~~~~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:38:46: error: 'WGM11' was not declared in this scope
38 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); // non-inverting PWM, mode 14 fastPWM, TOP =ICR1
| ^~~~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:39:9: error: 'TCCR1B' was not declared in this scope
39 | TCCR1B = _BV(WGM13) | _BV(WGM12) | prescaler ; // rescaler must be 1->5, 1,8,64,256,1028 respectively
| ^~~~~~
In file included from C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/wiring.h:39,
from C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/WProgram.h:46,
from C:\Users\Michael\AppData\Local\Temp\arduino\sketches\6D542608C537E51812A9D89560841D14/pch/Arduino.h:6,
from c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.h:4,
from c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:1:
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:39:22: error: 'WGM13' was not declared in this scope
39 | TCCR1B = _BV(WGM13) | _BV(WGM12) | prescaler ; // rescaler must be 1->5, 1,8,64,256,1028 respectively
| ^~~~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:39:35: error: 'WGM12' was not declared in this scope
39 | TCCR1B = _BV(WGM13) | _BV(WGM12) | prescaler ; // rescaler must be 1->5, 1,8,64,256,1028 respectively
| ^~~~~
C:\Users\Michael\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy4/core_pins.h:45:38: note: in definition of macro '_BV'
45 | #define _BV(n) (1<<(n))
| ^
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:40:9: error: 'ICR1' was not declared in this scope
40 | ICR1 = resolution;
| ^~~~
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp: In member function 'void MX1508::analogWrite16(uint8_t, uint16_t)':
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:50:26: error: 'OCR1A' was not declared in this scope
50 | case 9: OCR1A = val; break;
| ^~~~~
c:\Users\Michael\Documents\Arduino\libraries\MX1508\MX1508.cpp:51:26: error: 'OCR1B' was not declared in this scope
51 | case 10: OCR1B = val; break;
| ^~~~~

exit status 1

Compilation error: exit status 1

-------------------------


I have reinstalled the Arduino 2.1.0 IDE without effect, but perhaps reinstalling the IDE does not touch the compiler if is found to already exist.

Thanks for your help.

Tim
 
The library you chose is written for AVR, so it needs to be modified for use with Teensy. If you use the 3 functions below to replace the originals in MX1508.cpp, I think it will work. One more thing I saw that caused an error in exampleMX1508 was the use of macro NUMPWM is incompatible with the constructor and must be changed to this

Code:
MX1508 motorA(PINA,PINB, FAST_DECAY, PWM_2PIN);

Code:
void MX1508::setResolution(unsigned int pwmResolution) {
    _pwmResolution = pwmResolution;
#if defined (TEENSYDUINO)
    analogWriteResolution( pwmResolution );
#else
    if(_useAnalogWrite16) ICR1 = pwmResolution; 
#endif
}

void MX1508::setPWM16(uint8_t prescaler, unsigned int resolution) {
#if defined (TEENSYDUINO)
    analogWriteResolution( resolution );
#else
    if(prescaler > 5 || prescaler == 0) prescaler = 3;   // default to 64 if not in range.
    DDRB  |= _BV(PB1) | _BV(PB2);                       // set pin 9and 10 as outputs
    TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);    // non-inverting PWM, mode 14 fastPWM, TOP =ICR1
    TCCR1B = _BV(WGM13) | _BV(WGM12) | prescaler ;	// rescaler must be 1->5, 1,8,64,256,1028 respectively
    ICR1 = resolution;
#endif

    _pwmResolution = resolution;
    _useAnalogWrite16 = true; 
}

void MX1508::analogWrite16(uint8_t pin, uint16_t val)
{
#if defined(TEENSYDUINO)
  analogWrite( pin, val );
#else
  if(_useAnalogWrite16){
    if(val < 5) val =5;
    switch (pin) {
	case  9: OCR1A = val; break;
	case 10: OCR1B = val; break;
	default: analogWrite(pin,val);
    } 
  } else {
    analogWrite(pin, val);
  }
#endif
}
 
Awesome! Thanks joepasquariello. I am away from my workbench at the moment. I will give your code a try tonight. Thanks for the explanation too.

Is there a quick way to differentiate a library that is compiled for AVR from one that will work with the Cortex M7 of the Teensy 4.1?

Also, at some point in time I am likely to need PID control of the motor in my project. Are you aware of a library that might support PID control using the MX1508?

Thanks again,

Tim
 
Is there a quick way to differentiate a library that is compiled for AVR from one that will work with the Cortex M7 of the Teensy 4.1?

Each Arduino library has a library.properties file with a field labeled "architectures". It's good to look through available libraries and see which ones seem to be actively updated/supported.

Are you aware of a library that might support PID control using the MX1508?

This is a general-purpose PID library with an impressive set of blog posts describing its development.

https://github.com/br3ttb/Arduino-PID-Library
 
joepasquariello, the MX1508 library did indeed compile after I replaced the three functions in the original MX1508.cpp file with your code. Also, after doing an initial examination of the PID motor control library that you recommended, I can see that it should be possible to use the output from it to feed input into the MX1508 driver with minimal fuss. I am playing around with motorized faders, and it is already abundantly clear that getting anything like reasonable control over them is going to take a lot of trial and error. Still, you have given me the tools to do it, and your help is very much appreciated.

Tim
 
Back
Top