# Arduino Software



## dimitri (May 16, 2008)

Can you be more specific regarding the part you need help with?

Atmel chip in Arduino has 10 bit A/D inputs, which means digital values in range of 0-1023, so when you read 1023 that means 5V max input on the analog pin. If you go over 5V you may burn the input.

Please phrase your questions more specifically so we can better help you.

I wrote bunch of stuff for Arduino, including a version of BMS and EV Display, both of which monitor current from hall effect sensor.


----------



## dimitri (May 16, 2008)

Actually your code doesn't make too much sense. Are you trying to actually create PWM square wave with variable on/off times? You don't need to do this at all. Arduino lets you write a value to PWM output and it will automatically generate appropriate PWM signal based on the value.

I haven't looked at datasheet for your LEM sensor, is it bidirectional? i.e. does it have a zero point somewhere in the middle? If so, you need to calibrate it at first, by reading the input while there is no current and then use that value where you have ( -28 ) now.


----------



## Jan (Oct 5, 2009)

dimitri said:


> Actually your code doesn't make too much sense. Are you trying to actually create PWM square wave with variable on/off times? You don't need to do this at all. Arduino lets you write a value to PWM output and it will automatically generate appropriate PWM signal based on the value.
> 
> I haven't looked at datasheet for your LEM sensor, is it bidirectional? i.e. does it have a zero point somewhere in the middle? If so, you need to calibrate it at first, by reading the input while there is no current and then use that value where you have ( -28 ) now.


I think he wants to see the effect of the hall sensor on the led pin. It wil blink faster or slower depending on the measured value. And only if the value is below 950. For some reason.


----------



## Snakub (Sep 8, 2008)

This is just the PWM code I am not using the LEM Sensor at all here


> If so, you need to calibrate it at first, by reading the input while there is no current and then use that value where you have ( -28 ) now.


 From what I can tell on the print line the LEM is linear. The -28 is for the calibration of the pot.


> Are you trying to actually create PWM square wave with variable on/off times?


 Why wouldn't I need to create PWM on off times is it because somewhere in there library they have this code or something like it written because if they do I have looked all through it and didn't see anything like this to control a motor which is odd because every other micro controller that uses basic already has a library with a code for pwm for a motor 

My thoughts are that I am going to have to hook up the lem sensor and just run the car up to about 100 amps on jack stands see what measurements I get when I reach 100 amps. Let's say its a digital value of 150 on the print line that means if I were consuming 500 amps then the digital value would be 750. So if I wanted to limit to 500 then I would decrease the PWM High every time the sensor read that value just a little enough to keep me under 500 amps. I wonder how to know just how much to decrease the throttle so that it limits it just the right amount? and if I do so will there be any herky jerkiness of the throttle. Under normal driving the car barely ever reached 250 amps but the initial amperage is much higher maybe I just need some soft start code for when I just take off. I have a few of ideas I just don't know how to put them into the code.


----------



## Snakub (Sep 8, 2008)

Or maybe if it says that 500 amps is the max that this lem sensor will read then I won't have to put it up on jack stands because 500=1023.


----------



## jackbauer (Jan 12, 2008)

Arduino has pwm libraries. For example the command:

analogWrite(pin, 124);

Would set the pwm value on "pin" to approx 50%. 0 being off and 255 being 100%. Arduino pwm defaults to 500hz which is usless for motor control but can be set to any frequency. I have some code to effect this on my laptop so let me know and i'll post it up.


----------



## Snakub (Sep 8, 2008)

By all means post up the code and 500 hertz must be what I am running at during my short trips down the street and if you could post a link to their motor control library I couldn't find it anywhere.


----------



## Overlander23 (Jun 15, 2009)

Running the vehicle on jack stands is essentially running without a mechanical load. You could very well over-rev your motor without even getting close to pulling 100 amps.


----------



## samborambo (Aug 27, 2008)

Overlander is right; with no load on the wheels you won't get much torque at all. Unless, of course, you put your foot on the brakes.

Your biggest challenge will be the power stage. If I were you, I'd concentrate on studying power electronics. Knowing how your power stage works will give you a better idea on what the CPU needs to do. There's all sorts of signals that the CPU needs to watch and act upon, eg gate driver undervoltage, gate desaturation, motor overcurrent, etc.

The program code also needs to be built very resiliently. For example, in real world applications, you can't even trust that the CPU is processing everything correctly 100% of the time. 

Add checks in your program code to see if your register values are sane.
Double, or even triple check your complex math operations with different methods and compare the answers to ensure they are the same.
A hardware watchdog timer is a necessity in this role.
Your program code should be in the form of a finite state machine.
As others have suggested, use interrupt service routines for time dependent or important actions. The PWM library for the Arduino uses ISRs. This will allow your main program code to run asynchronously from timed tasks such as PWM. Critically fast actions such as shutting down the gate in the event of an overcurrent or gate desat should be done in hardware with logic gates or via an interrupt-on-change pin.

Lastly, your PWM control shouldn't be directly proportional to the throttle input (as you have done). PWM duty cycle represents the ratio of voltage from input to output of the controller. The throttle should control torque (current), not voltage. 

The correct operation would be to read the pot value, scale it, read the current sensor, scale it, compare the current to the pot. This will give you an error value. Use this error value to feed into a PID control block. The output of the (correctly configured) PID block will be your PWM reload value. repeat this process ad infinitum.

The best place for the throttle control loop would be in an ISR, serviced by a timer every 100ms or so. You'll then be able to enable or disable throttle control by means of setting or clearing the respective interrupt enable flag.

Hope this helps.

Sam.


----------



## samborambo (Aug 27, 2008)

Cancel what I said about current control from the pot. Thinking about it, you'd need to know speed of the motor in order to decouple the BEMF voltage from the PWM. Current control for torque only really applies to AC controllers.

Then again, it may have some benefit in a DC setup with speed feedback. Tesseract or Qer may have some insight into this.

Sam.


----------



## Dave Koller (Nov 15, 2008)

It is not Arduino but same processor (Atmel) source code is in C.. but might find it interesting...

http://ecomodder.com/wiki/index.php/ReVolt


----------



## Qer (May 7, 2008)

samborambo said:


> Current control for torque only really applies to AC controllers.


Nope. Works the same for all motor types I know of, including series wound. Increase current and you'll increase the torque, no black magic involved.



samborambo said:


> Then again, it may have some benefit in a DC setup with speed feedback.


Na, it just complicates things. 



samborambo said:


> Tesseract or Qer may have some insight into this.


Not much that hasn't been said already. The AVR is powerful enough to run a DC-controller and it's a reasonably simple little microcontroller to understand and write safe code for if you know what you're doing. There's a lot of code on the net to study and there's the avrfreaks-forum that's usually very friendly and useful if you run into problems.

Constructing the power electronics' gonna be a bitch though. Not that it's theoretically complicated, but in real life weird things start to happen when you try to harness high power. Going for tens of kW, like in a golf cart controller, isn't very hard (and that's why those controllers aren't very expensive) but when you aim at hundreds of kW or the glorious MW you'll start to blow things up. A lot. See the threads about constructing the ReVolt in the Ecomodders forum or the Soliton 1 here. They have one thing in common, they blew up a lot of transistors in the prototype runs...

Combining software and hardware is gonna make things even more interesting. A small software error can easily blow up a few hundred dollars or more (been there, done that) a hardware problem is likely to blow up more or less all of the electronics (at least the expensive parts, Tesseract knows all about that from empirical experience) and in the end building your own controller from scratch and just making a single one is probably going to be more expensive than you think.

I've seen a few people boost here in the forum that they're gonna build their own controllers. In most cases we hear a lot about it until they start to run tests and blow up their first set of transistors, then they usually give up and go quiet. A few do actually finish their controllers, but that's usually guys that (excuse me if I'm blunt here) do have experience from similar constructions already so they're not getting stuck at the basics.

Personally I think you have two realistic options:


Go for ReVolt and help improving that controller rather than trying to reinvent the wheel.
Buy something that's been proven to be reliable and spend your energy at converting a car rather than blowing up silicon.
There's a reason it took EVnetics a year to literally blow up all the obstacles on the path to a sellable product, you know...


----------



## Dave Koller (Nov 15, 2008)

Qer said:


> Personally I think you have two realistic options:
> 
> 
> Go for ReVolt and help improving that controller rather than trying to reinvent the wheel.
> ...


I second that Qer ..........


----------

