RoboClaw Library Port

Hey guys,

Im working on getting an Orion robotics motor controller working, but first I have to change a few things in the library to get it to work with Spark. All documentation and the original Arduino libraries can be found at http://www.orionrobotics.com/

The original library RoboClaw.h & RoboClaw.cpp included another library <BMSerial.h> which is a modified software serial library built on top of “Streams.h”. I want to port the modify the library to run on Spark’s Serial1 port, but am having trouble replacing the timeout functionality that came from Streams.h

So far I made my own read() function that I believe will work the same way Steams.h does Does this seem like it will work/ Does anyone have a better suggestion?

Any help on this is greatly appreciated I’m sure It’d take someone who knows what their doing less than an hour to port read_n( ), read4_1( ), read4( ), read2( ), and write_n( ) (The communication functions) and the whole library would work.

Hi @chad_bean

In Spark’s Stream implementation there is a private method that looks close to what you want but you would have to copy the code to call it – here it is:

// private method to read stream with timeout
int Stream::timedRead()
{
  int c;
  _startMillis = millis();
  do {
    c = read();
    if (c >= 0) return c;
  } while(millis() - _startMillis < _timeout);
  return -1;     // -1 indicates timeout
}

I am not sure why this is not brought forward into the concrete classes for USART serial. I am not sure how this would work for USB serial either.

2 Likes

You could have a look at the Core open source firmware repos

https://github.com/spark/firmware/blob/master/src/spark_wiring_stream.cpp
and
https://github.com/spark/firmware/blob/master/src/spark_wiring_usartserial.cpp

For instance in the Stream one you’ll find a protected function timedRead() which does pretty much the same as your own read(uint8_t), which you could use in a derived class.
On the other hand I don’t know if you actually need the timed version, since Serial1 does the actual reading in an interrupt while Serial1.read() only passes bytes back to you that have already been placed in the ring buffer by this interrupt routine - but to really know if it is needed or not would demand for a deeper look into the whole project.

But I hope this did give you some pointers to build on.


And too slow again, @bko was there, before I could press the “Reply” button.
But just as a side note: timedRead() is not private but protected

1 Like

@chad_bean, both @bko and @ScruffR have given you great advice. The code you posted will work as well though you need to declare startMillis() as an unsigned long. Also, you cannot return -1 if your function is cast as uint16_t! Make it int or int16_t instead. Using a modified timeRead() code makes sense since it adds some sanity check (c >= 0) around the read().

@ScruffR, both Serial1 and Serial2 have a receive buffer to allow functions like peek() and available() to work. The user code still needs to wait on data from the sender, regardless of buffering so a timeout makes sense.

Interesting that the code does not actually test for a timeout (-1) returned from read(timeout). I guess that will just cause the crc to not match and the code will dump the entire message. I could not readily find the library code on the site you linked. Can you provide a direct link? :smile:

1 Like

Thanks guys! The ideas are flowing! I think it might make sense to leave RoboClaw.cpp as is and port BMSerial.cpp (which is the library which uses Streams and handles other communication protocols) to work with the spark streams library. This sounds much simpler in theory I was just afraid because BMSerial seems like it contains a lot of micro dependent code (for the teensy i think?) that I didn’t want to have to dig through.

@peekay123 http://www.orionrobotics.com/RoboClaw-2x30A-Motor-Controller-with-USB_p_331.html
there is a link “Arduino Libraries and Examples” That downloads a zip of the libraries

2 Likes

Hello.

I am trying to port the latest RoboClaw library. which is not using BMSerial any more.
As it uses HardwareSerial.h and SoftwareSerial.h, I thought I could replace them with USARTSerial.h and ParticleSoftSerial.h
I have imported the ParticleSoftSerial library into my project and replaced Arduino.h with application.h in my .cpp file
The problem is that it doesn’t compile, because USARTSerial header file can’t be found.
I am new to this kind of library porting and I can’t really figure out what I am missing.
Thank you for your help.

Here is the modified header in ParticleRoboclaw.h

        #ifndef RoboClaw_h
	#define RoboClaw_h

	#include <stdarg.h>

	#include <inttypes.h>
	// #include <Stream.h>
	#include <USARTSerial.h>
	#ifdef __AVR__
		#include <ParticleSoftSerial.h>
	#endif

You don’t need to include any header file for USARTSerial. That’s already part of the firmware and would be called spark_wiring_usartserial.h
And ParticleSoftSerial is not compatible with AVR, so it should not be inside that #ifdef block.

Thank you very much ScruffR for your quick reply. These two modifications solved the problem.